Skip to content

Add && (AND-list) operator to shell for command chaining #458

@LSantha

Description

@LSantha

Feature: Add && (AND-list) operator to the JNode shell

The JNode shell currently does not support command chaining with &&. Commands like cd /tmp && java Test fail because the shell attempts to parse && as part of the first command's arguments or rejects it as invalid syntax.

Use case

Running multiple dependent commands in a single line is a common shell pattern. Without &&:

  1. Idempotent command sequences: Many tasks require "if this succeeds, then do that" logic. Examples:

    • mkdir /tmp/work && echo ready — create dir, then confirm
    • ping 10.0.2.2 && echo network up — test connectivity, then report
    • wget http://example.com/file && echo "download complete" — fetch then confirm
    • device --scan && ifconfig — rescan devices then list interfaces
  2. Batch scripts: A future shell scripting feature would naturally build on && support.

Current behavior

[JNODE_AGENT_READY]
> cd /jnode/tmp && java HelloWorld
Command syntax error(s):
Usage: cd ...

The shell either treats && as part of the cd arguments or rejects the entire line as invalid.

Desired behavior

[JNODE_AGENT_READY]
> ping 10.0.2.2 && echo "network ready"
Reply from 10.0.2.2: ...
network ready
[JNODE_AGENT_READY]

The shell should execute the left-hand command, check its exit status, and only execute the right-hand command if the exit status indicates success (zero).

Required semantics (POSIX shell compatible)

  • command1 && command2: Execute command1. If its exit code is 0, execute command2.
  • Chaining: a && b && c executes left-to-right, stopping at the first failure.
  • || (OR-list) is a natural companion but could be deferred to a separate issue.
  • Commands on both sides must support all existing shell syntax (arguments, redirections, pipes).

Implementation considerations

  1. Parser changes: The shell's command parser (org.jnode.shell.*) needs to recognize && as an operator rather than a regular argument. This likely requires changes to the tokenizer/lexer.

  2. Shell execution model: The shell's command runner must evaluate exit codes and conditionally skip subsequent commands. JNode's CommandShell or equivalent execution loop needs to handle AND-list / OR-list AST nodes.

  3. Exit code propagation: All commands must consistently return meaningful exit codes. Currently many commands may not set a proper non-zero exit code on failure. This may need to be fixed as a prerequisite or bundled.

  4. Backward compatibility: Existing single-command invocations must not be affected. echo hello && world should still echo hello and then fail on world (rather than printing hello && world).

Affected source files (likely)

  • shell/src/shell/org/jnode/shell/ — parser, tokenizer, command line splitting
  • shell/src/shell/org/jnode/shell/command/ — command execution and exit code handling
  • cli/ — CLI shell implementation (may also need updates)

Verification

Test script to run inside JNode:

true && echo one
false && echo two
echo three

Expected output: one then three (no two because false prevents chaining).

Test with javac + java:

javac /tmp/Test.java && java Test

Expected: compiles, then runs.

Agent script test (from host):

python3 jnode_agent_cmd.py "ls /jnode && echo found"

Expected: ls output followed by found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions