当前位置: 技术问答>linux和unix
shell script脚本不同执行方式结果不同,求教
来源: 互联网 发布时间:2017-03-26
本文导语: 脚本内容如下: echo "$PATHn" echo "I love U." echo "I Miss U" 第一种执行方式及结果: root@sophie:/usr/bin# lily.sh /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binn I love U. I Miss U 第二种执行方式及结果: root@sophie:/usr/bin# ...
脚本内容如下:
echo "$PATHn"
echo "I love U."
echo "I Miss U"
第一种执行方式及结果:
root@sophie:/usr/bin# lily.sh
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binn
I love U.
I Miss U
第二种执行方式及结果:
root@sophie:/usr/bin# sh lily.sh
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
I love U.
I Miss U
求大鸟解答。
echo "$PATHn"
echo "I love U."
echo "I Miss U"
第一种执行方式及结果:
root@sophie:/usr/bin# lily.sh
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binn
I love U.
I Miss U
第二种执行方式及结果:
root@sophie:/usr/bin# sh lily.sh
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
I love U.
I Miss U
求大鸟解答。
|
其实答案也很简单, 就是不同shell的echo实现不一样 (注意 这里的 echo 是内置命令)
$sh
$echo "testn"
test
$bash
$ echo "testn"
testn
$
man page 清楚的写明了echo的用法不同
$man sh
DASH(1) BSD General Commands Manual DASH(1)
NAME
dash — command interpreter (shell)
...
echo [-n] args...
Print the arguments on the standard output, separated by spaces. Unless the -n option is present, a new‐
line is output following the arguments.
If any of the following sequences of characters is encountered during output, the sequence is not output.
Instead, the specified action is performed:
b A backspace character is output.
c Subsequent output is suppressed. This is normally used at the end of the last argument to sup‐
press the trailing newline that echo would otherwise output.
f Output a form feed.
n Output a newline character.
r Output a carriage return.
t Output a (horizontal) tab character.
v Output a vertical tab.
digits
Output the character whose value is given by zero to three octal digits. If there are zero dig‐
its, a nul character is output.
\ Output a backslash.
All other backslash sequences elicit undefined behaviour.
同样的对于bash
$man bash
BASH(1) BASH(1)
NAME
bash - GNU Bourne-Again SHell
...
echo [-neE] [arg ...]
Output the args, separated by spaces, followed by a newline. The return status is always 0. If -n is
specified, the trailing newline is suppressed. If the -e option is given, interpretation of the follow‐
ing backslash-escaped characters is enabled. The -E option disables the interpretation of these escape
characters, even on systems where they are interpreted by default. The xpg_echo shell option may be
used to dynamically determine whether or not echo expands these escape characters by default. echo does
not interpret -- to mean the end of options. echo interprets the following escape sequences:
a alert (bell)
b backspace
c suppress further output
e
E an escape character
f form feed
n new line
r carriage return
t horizontal tab
v vertical tab
\ backslash
nnn the eight-bit character whose value is the octal value nnn (zero to three octal digits)
xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex
digits)
UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight
hex digits)
如果不使用内置命令,那么你就可以看到同样的输出
$which echo
/bin/echo
$sh
$/bin/echo "testn"
testn
$bash
$/bin/echo "testn"
testn
$
此处echo 为linux coreutils 的实现
$sh
$echo "testn"
test
$bash
$ echo "testn"
testn
$
man page 清楚的写明了echo的用法不同
$man sh
DASH(1) BSD General Commands Manual DASH(1)
NAME
dash — command interpreter (shell)
...
echo [-n] args...
Print the arguments on the standard output, separated by spaces. Unless the -n option is present, a new‐
line is output following the arguments.
If any of the following sequences of characters is encountered during output, the sequence is not output.
Instead, the specified action is performed:
b A backspace character is output.
c Subsequent output is suppressed. This is normally used at the end of the last argument to sup‐
press the trailing newline that echo would otherwise output.
f Output a form feed.
n Output a newline character.
r Output a carriage return.
t Output a (horizontal) tab character.
v Output a vertical tab.
digits
Output the character whose value is given by zero to three octal digits. If there are zero dig‐
its, a nul character is output.
\ Output a backslash.
All other backslash sequences elicit undefined behaviour.
同样的对于bash
$man bash
BASH(1) BASH(1)
NAME
bash - GNU Bourne-Again SHell
...
echo [-neE] [arg ...]
Output the args, separated by spaces, followed by a newline. The return status is always 0. If -n is
specified, the trailing newline is suppressed. If the -e option is given, interpretation of the follow‐
ing backslash-escaped characters is enabled. The -E option disables the interpretation of these escape
characters, even on systems where they are interpreted by default. The xpg_echo shell option may be
used to dynamically determine whether or not echo expands these escape characters by default. echo does
not interpret -- to mean the end of options. echo interprets the following escape sequences:
a alert (bell)
b backspace
c suppress further output
e
E an escape character
f form feed
n new line
r carriage return
t horizontal tab
v vertical tab
\ backslash
nnn the eight-bit character whose value is the octal value nnn (zero to three octal digits)
xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex
digits)
UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight
hex digits)
如果不使用内置命令,那么你就可以看到同样的输出
$which echo
/bin/echo
$sh
$/bin/echo "testn"
testn
$bash
$/bin/echo "testn"
testn
$
此处echo 为linux coreutils 的实现
|
echo 和 echo -e 的区别?
|
估计你用sh lily.sh调的话是直接用sh解析的。用lily.sh直接运行的话,调的是/bin/bash
应该是这个差别导致的。你可以在脚本第一行加一句:echo $BASH确认一下。
你可以分别试用如下方法调用看区别:
sh lily.sh
./lily.sh
bash ./lily.sh
我记得在一本书里看过,说是直接用sh调用不建议。另外,脚本开头一般要明确指定用哪个shell调用,你可以在脚本第一行分别加如下语句看看区别:
#!/bin/bash
#!/bin/sh
应该是这个差别导致的。你可以在脚本第一行加一句:echo $BASH确认一下。
你可以分别试用如下方法调用看区别:
sh lily.sh
./lily.sh
bash ./lily.sh
我记得在一本书里看过,说是直接用sh调用不建议。另外,脚本开头一般要明确指定用哪个shell调用,你可以在脚本第一行分别加如下语句看看区别:
#!/bin/bash
#!/bin/sh