6

I'm writing a script to test a shell project to see that my custom shell has correct output.

str="HELLO"
echo $str
echo "*** YOU SHOULD SEE HELLO ABOVE ***"
ls *
echo "*** YOU SHOULD SEE THE OUTPUT FROM ls * ABOVE ***"
who|awk '{print $1}'
echo "*** YOU SHOULD SEE THE OUTPUT FROM who ABOVE ***"
echo $((1+2*3-4/5+6*7-8/9)))
echo "*** YOU SHOULD SEE THE NUMBER 49 ABOVE ***"

This is the output from the script

$ ./shell < ../tst_exp.sh 
'PATH' is set to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin.
HELLO
*** YOU SHOULD SEE HELLO ABOVE ***
CMakeCache.txt       hello_2.7-0ubuntu1_amd64.changes  hello_2.7-0ubuntu1.diff.gz  hello_2.7.orig.tar.gz  jeff      not  script.sh  testresult.txt
cmake_install.cmake  hello_2.7-0ubuntu1_amd64.deb      hello_2.7-0ubuntu1.dsc      hello-2.7.tar.gz   Makefile  osh  shell

build-area:
hello_2.7-0ubuntu1_amd64.build    hello_2.7-0ubuntu1_amd64.deb  hello_2.7-0ubuntu1.dsc
hello_2.7-0ubuntu1_amd64.changes  hello_2.7-0ubuntu1.diff.gz    hello_2.7.orig.tar.gz

.bzr:
branch  branch-format  branch-lock  checkout  README  repository

CMakeFiles:
3.5.1          CMakeDirectoryInformation.cmake  CMakeRuleHashes.txt  feature_tests.bin  feature_tests.cxx  Makefile.cmake  shell.dir    TargetDirectories.txt
cmake.check_cache  CMakeOutput.log          CMakeTmp         feature_tests.c    Makefile2          progress.marks  shellparser.dir

hello:
ABOUT-NLS   AUTHORS    ChangeLog    config.in  configure.ac  COPYING  doc     INSTALL      Makefile.in  NEWS  README  tests   TODO
aclocal.m4  build-aux  ChangeLog.O  configure  contrib       debian   gnulib  Makefile.am  man      po    src     THANKS

hello-2.7:
ABOUT-NLS   AUTHORS    ChangeLog    config.h   config.log     configure     contrib  doc     INSTALL   Makefile.am  man   osh  README  stamp-h1  THANKS
aclocal.m4  build-aux  ChangeLog.O  config.in  config.status  configure.ac  COPYING  gnulib  Makefile  Makefile.in  NEWS  po   src     tests     TODO
*** YOU SHOULD SEE THE OUTPUT FROM ls * ABOVE ***
[21420] 
dac
dac
*** YOU SHOULD SEE THE OUTPUT FROM who ABOVE ***
49
*** YOU SHOULD SEE THE NUMBER 49 ABOVE ***

The output is expected, but if I add a shebang, which is recommended, then strange things happen. This is my own shell that I'm testing, so what should the shebang be for scripts that should be run with my shell, that I named osh and can put in /usr/local/bin or install like another shell?

I suppose the shebang should not be #!/usr/bin/env bash which is what my editor (CLion) recommends, and it can't be #!/usr/bin/env osh because that is not installed and gives strange results.

How should I handle the shebang when writing my own shell?

12

It should be

#!/usr/local/bin/osh

if your shell is in /usr/local/bin. If /usr/local/bin is on your PATH then

#!/usr/bin/env osh

should work too... (In fact that's the only point of env here — it will find osh wherever it's installed, as long as it's on the PATH, so it doesn't matter if it's in /usr/local/bin, /usr/bin etc. The first one found wins if multiple copies are installed.)

If you use the env variant, just make sure there isn't another osh binary on your PATH: as schily points out, the Thompson shell could be installed as osh (although that strikes me as rather unlikely, but since you're researching shells it might happen), and there's also the OCaml build system's debugging shell (typically in an omake package).

6
  • 1
    osh typically is the name of the Thompson shell, the shell that was in use before the Bourne Shell. If you call #!/usr/bin/env osh, there is a nice chance to get this shell as it might be installed in /usr/bin.
    – schily
    May 25 '16 at 16:22
  • 1
    @schily that's interesting — but what kind of systems would you still find that on nowadays? May 25 '16 at 16:44
  • 1
    Some people install it. It is OpenSource since a while ;-) osh really sounds more like old shell than like own shell.
    – schily
    May 25 '16 at 16:46
  • 1
    The correct name would clearly be "spiffy shell 4000 elite" which, obviously, becomes ssh4ke.
    – The Nate
    May 25 '16 at 18:58
  • 1
    @TheNate Yes. The name should be the obvious ssh4ke. Now on code review - I'm using the lemon parser to generate a grammar which practically also forces me to learn what the shell can do. I'm currently trying to write the grammar for a shell conditional.
    – Niklas R.
    May 26 '16 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.