Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306A series of simple shell scripting tests for msh.
1. Variables and Parameters
1.1 Variable Substitution
$ cat >test.sh <<EOF > P="hello world" > echo \$P > EOF
$ msh test.sh hello world
$ cat >test.sh <<EOF > 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 <<EOF > 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 <<EOF > sh -c "exit 1" && echo hello > EOF
$ msh test.sh [1]
2.3 Simple And and Or
$ cat >test.sh <<EOF > echo first || sh -c "echo never && exit 1" && echo second > EOF
$ msh test.sh first second
$ cat > test.sh <<EOF > 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 <<EOF > # Hello, World! > --------------- > EOF
$ msh -c "cat < hello.md" # Hello, World! ---------------
Using `<&-` to close stdin
$ msh -c "cat <&-"cat: -: Bad file descriptorcat: closing standard input: Bad file descriptor[1]
Using `<&` to copy a file descriptor
$ msh -c "cat 3<hello.md <&3"# Hello, World!---------------
But also, some simple errors should work too.
$ msh -c "cat <1"Fatal error: exception Eio.Io Fs Not_found Unix_error (No such file or directory, "openat", "1"),opening <fs:1>[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.txthello
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 <<EOF > 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