A series of simple shell scripting tests for msh. 1. Variables and Parameters 1.1 Variable Substitution $ cat >test.sh < P="hello world" > echo \$P > EOF $ msh test.sh hello world $ cat >test.sh < FOO=bar > echo "\$FOO" > EOF $ msh test.sh bar $ msh -c "FOO=bar echo \$FOO" $ msh -c "FOO=bar; echo \$FOO" bar $ msh -c "FOO=~/foo; echo \$FOO" /home/patrick/foo $ msh -c "FOO=~/foo env | grep FOO" FOO=/home/patrick/foo $ cat >test.sh << EOF > VAR1=hello > VAR2=world; VAR_UNSET=check echo \$VAR1 > echo "VAR2: \$VAR2 (\$VAR_UNSET)" > EOF $ sh test.sh hello VAR2: world () $ msh test.sh hello VAR2: world () $ sh -c "FOO=abc env | grep FOO; env | grep FOO" FOO=abc [1] $ msh -c "FOO=abc env | grep FOO; env | grep FOO" FOO=abc [1] $ sh -c "FOO=bar; env | grep FOO" [1] $ msh -c "FOO=bar; env | grep FOO" [1] $ sh -c "FOO=bar echo hello | env | grep FOO" [1] $ msh -c "FOO=bar echo hello | env | grep FOO" [1] $ sh -c "export FOO=bar; env | grep FOO" FOO=bar $ msh -c "export FOO=bar; env | grep FOO" FOO=bar Trying a common pattern for path-like expansion: $ cat > test.sh << EOF > export TPATH=/usr/bin > export TPATH=/bin:\$TPATH > echo \$TPATH > EOF $ sh test.sh /bin:/usr/bin $ msh test.sh /bin:/usr/bin Some variable expansions: $ cat > test.sh << EOF > echo "Got \$# arguments: [\$@]" > for arg in "\$@"; do > echo "[\$arg]" > done > EOF $ sh test.sh hello world "from the shell" Got 3 arguments: [hello world from the shell] [hello] [world] [from the shell] $ msh test.sh hello world "from the shell" Got 3 arguments: [hello world from the shell] [hello] [world] [from the shell] $ cat > test.sh << EOF > BASEDIR=/home/bactrian > f () { > echo "\$1" > } > f \$BASEDIR/documents > EOF $ sh test.sh /home/bactrian/documents $ msh test.sh /home/bactrian/documents 2. Pipelines with And|Or 2.1 Simple Or $ cat >test.sh < sh -c "exit 1" || echo hello > EOF $ msh test.sh hello $ msh -c "lssssss" msh: command not found: lssssss [127] $ sh -c "lssssss 2>&1 > /dev/null | echo 'all good'" all good TODO: We need to also redirect error messages from the shell, for example the command not being found here :/ $ msh -c "lssssss 2>&1 > /dev/null | echo 'all good'" all good 2.2 Simple And $ cat >test.sh < sh -c "exit 1" && echo hello > EOF $ msh test.sh [1] 2.3 Simple And and Or $ cat >test.sh < echo first || sh -c "echo never && exit 1" && echo second > EOF $ msh test.sh first second $ cat > test.sh < set -e > echo "hello" > false && echo "nope" > echo "done" > EOF $ msh test.sh hello done 2.4 Simple Pipeline $ msh -c "echo hello | rev" olleh $ msh -c "echo hello | rev | rev | rev | rev" hello 2.5 Input redirection Simple example of redirecing stdin to an open file. $ cat >hello.md < # Hello, World! > --------------- > EOF $ msh -c "cat < hello.md" # Hello, World! --------------- Using `<&-` to close stdin $ msh -c "cat <&-" cat: -: Bad file descriptor cat: closing standard input: Bad file descriptor [1] Using `<&` to copy a file descriptor $ msh -c "cat 3 [2] 2.6 Output Redirection Simple example of redirecting some output $ msh -c "echo hello > hello.txt && cat hello.txt" hello $ stat --format="%A" hello.txt -rw-r--r-- Simple appending example $ msh -c "echo world >> hello.txt && cat hello.txt" hello world Redirection of fds DISABLED $ msh -c "echo hello 789>out.txt >&789" $ cat out.txt hello NON-POSIX redirection of stderr and stdout. $ msh -c "ls -j &> /dev/null" [2] Clobbering files and stuff. $ cat > test.sh << EOF > echo hello > file.txt > set -o noclobber > echo world > file.txt > echo world >| file.txt > cat file.txt > EOF $ sh test.sh test.sh: line 3: file.txt: cannot overwrite existing file world $ msh test.sh msh: cannot overwrite existing file world Also errexit examples from the specification. $ sh -c "set -e; (false; echo one) | cat; echo two" two $ msh -c "set -e; (false; echo one) | cat; echo two" two $ sh -c "set -e; echo $(false; echo one) two" one two $ msh -c "set -e; echo $(false; echo one) two" one two 2.7 Sequences A simple, semicolon sequence. $ msh -c "echo hello; echo world; echo 'that is all'" hello world that is all $ cat > test.sh < echo hello > echo world > echo 'that is all' > EOF $ msh test.sh hello world that is all 3. Field splitting $ cat > test.sh << EOF > IFS=":" > ARGS="apple:banana" > echo \$ARGS > ARGS="apple:banana:" > echo \$ARGS > ARGS="apple::banana" > echo \$ARGS > ARGS=":apple:banana" > echo \$ARGS > EOF $ sh test.sh apple banana apple banana apple banana apple banana $ msh test.sh apple banana apple banana apple banana apple banana