编写自己的shell时应该如何处理shebang?

Nik*_* R. 6 shell-script shebang

我正在编写一个脚本来测试一个 shell 项目,以查看我的自定义 shell 是否具有正确的输出。

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 ***"
Run Code Online (Sandbox Code Playgroud)

这是脚本的输出

$ ./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 ***
Run Code Online (Sandbox Code Playgroud)

输出是预期的,但是如果我添加一个推荐的shebang,那么奇怪的事情就会发生。这是我正在测试的自己的 shell,那么对于应该与我的 shell 一起运行的脚本,shebang 应该是什么,我命名osh并可以/usr/local/bin像另一个 shell 一样放入或安装?

我想 shebang 不应该#!/usr/bin/env bash是我的编辑器 (CLion) 推荐的那样,而且不可能是#!/usr/bin/env osh因为它没有安装并给出奇怪的结果。

编写自己的shell时应该如何处理shebang?

Ste*_*itt 12

它应该是

#!/usr/local/bin/osh
Run Code Online (Sandbox Code Playgroud)

如果您的外壳在/usr/local/bin. 如果/usr/local/bin在你的PATH然后

#!/usr/bin/env osh
Run Code Online (Sandbox Code Playgroud)

应太......(其实这是唯一的点env在这里-它会发现osh无论它的安装,只要它在PATH,所以它并不重要,如果它在/usr/local/bin/usr/bin等第一个发现赢,如果多个副本已安装。)

如果您使用该env变体,只需确保osh您的PATH:上没有另一个二进制文件:正如schily指出的那样,可以将Thompson shell安装为osh(虽然这让我觉得不太可能,但由于您正在研究 shell,它可能会发生) ,还有 OCaml 构建系统的调试 shell(通常在一个omake包中)。