Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116Testing functions and position arguments.
First is simply that the original positional arguments are preserved.
$ cat > test.sh << EOF > echo "\$1 from \$0" > EOF
$ sh test.sh hello hello from test.sh $ msh test.sh hello hello from test.sh
Next, do they work inside function definitions!
$ cat > test.sh << EOF > shout () { > echo \$0 > echo \$1 | tr a-z A-Z > } > shout "hi there" > EOF
$ sh test.sh test.sh HI THERE $ msh test.sh test.sh HI THERE
Redirection and exit codes should be preserved to just like any other command.
$ cat > test.sh << EOF > goodbye () { > echo "eybdoog" > exit \$1 > } > goodbye 0 | rev > goodbye 1 | rev > goodbye 128 > EOF
$ sh test.sh goodbye goodbye eybdoog [128]
$ msh test.sh goodbye goodbye eybdoog [128]
Local variables scopes.
$ cat > test.sh << EOF > f () { > local dest > dest="hello from f" > echo \$dest > } > > g () { > local dest > dest="hello from g" > echo \$dest > f > echo \$dest > } > > g > echo "Outer dest: \$dest" > EOF
$ sh test.sh hello from g hello from f hello from g Outer dest: $ msh test.sh hello from g hello from f hello from g Outer dest:
Local variable scopes from the pipeline position.
$ cat > test.sh << EOF > f () { > echo "F: ABC \$ABC DEF \$DEF" > } > g () { > echo "G: ABC \$ABC" > DEF=bar f > echo "G: Env Grep ABC \$(env | grep ABC)" > } > > ABC=foo echo "ABC is \$ABC" > ABC=foo g > ABC=foo env | grep ABC > EOF
$ sh test.sh ABC is G: ABC foo F: ABC foo DEF bar G: Env Grep ABC ABC=foo ABC=foo $ msh test.sh ABC is G: ABC foo F: ABC foo DEF bar G: Env Grep ABC ABC=foo ABC=foo