写shell关于bash 和 sh .的问题....14.04LTS

sh/bash/dash/ksh/zsh等Shell脚本
回复
dkkacceler
帖子: 26
注册时间: 2015-06-27 10:08
系统: ubuntu14.04 LTS

写shell关于bash 和 sh .的问题....14.04LTS

#1

帖子 dkkacceler » 2015-07-03 17:48

今天写shell时与遇到的问题
代码如下:
#!/bin/bash
#program:
# this program show you choice
#history:
#2015.7.3 acceler first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin:/usr/local/bin:~/bin
export PATH

read -p "please input (y/n): " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK,continue!"
exit 0
fi
if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh,interrupt"
exit 0
fi
echo "i don't konw what your choice is " && exit 0
保存为 1.sh
运行时输入:bash 1.sh 运行无误
运行时输入:sh 1.sh 出错错误如下:
1.sh: 10: [: y: unexpected operator
1.sh: 10: [: y: unexpected operator
1.sh: 14: [: y: unexpected operator
1.sh: 14: [: y: unexpected operator
i don't konw what your choice is

为什么会这样?....
bash 和 sh 有什么区别???????
poloshiao
论坛版主
帖子: 18279
注册时间: 2009-08-04 16:33

Re: 写shell关于bash 和 sh .的问题....14.04LTS

#2

帖子 poloshiao » 2015-07-03 18:13

bash 和 sh 有什么区别???????
1. sudo ls -al /bin/bash
-rwxr-xr-x 1 root root 1021112 10月 8 2014 /bin/bash

2. sudo ls -al /bin/sh
lrwxrwxrwx 1 root root 4 2月 19 2014 /bin/sh -> dash
2-1. sudo ls -al /bin/dash
-rwxr-xr-x 1 root root 121272 2月 19 2014 /bin/dash

3. 意思是 當你使用 sh
實際執行的是 dash
3-1. 注意 並不是所有的 Linux 發行版本 都是這樣

4. http://askubuntu.com/questions/141928/w ... d-bin-bash
What is difference between #!/bin/sh and #!/bin/bash?

5. http://www.linuxquestions.org/questions ... ix-912790/
difference between /bin/bash and /bin/dash and compatibility with POSIX
回复