下面是一个简单的shell程序。(来自 expert shell scripting一书 后面没有特殊说明也是一此书中的一些代码示例。为基础修改而来)
Shell代码
#!/bin/sh
#set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer = ‘echo $answer | tr [a-z] [A-Z]’
if[ $answer = Y] then;
echo "Wow,you must be very skilled!"
esle
echo "Neither can I,I'm just an example shell script"
fi
#!/bin/sh
#set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer = ‘echo $answer | tr [a-z] [A-Z]’
if[ $answer = Y] then;
echo "Wow,you must be very skilled!"
esle
echo "Neither can I,I'm just an example shell script"
fi
[color=darkred]上面存在一些错误。
其一:
'echo $answer | tr [a-z] [A-Z]'
中的单引号应该为tab键上面的反单引号才会把里面的字符串当成是代码,否则就是一个字符串。
空格啊空格啊:
其二:
if[ $answer = Y]
中一句中,"if["三个字符中,没有空格会引起下面的错误报告。
./set_x_disable.sh: 6: if[: not found
[/quote]
显然Y之后也需要有空格。
所以[]之前必须有空格。
其三: answer = 'echo ...' 一句中还有一处错误就是。
“answer”和 “=” 及"'echo'"号之间不能有空格。
否则报错如下:
引用
./set_x_disable.sh: 5: answer: not found [/color]如果在等号右边有空格的话还会把空格当成answer字符串开头值的第一个字符。
坑爹有空格啊。
开始"set -x"运行几次之后。修改之后的脚本代码如下:
Shell代码
#!/bin/sh
set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer=`echo $answer | tr [a-z] [A-z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
#!/bin/sh
set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer=`echo $answer | tr [a-z] [A-z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
下面是开启了"set -x"之后的两次运行结果:
Shell代码
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
n
+ echo n
+ tr [a-z] [A-z]
+ answer=N
+ [ N = Y ]
+ echo Neither can I,I'm just an example shell script
Neither can I,I'm just an example shell script
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
y
+ echo y
+ tr [a-z] [A-Z]
+ answer=Y
+ [ Y = Y ]
+ echo Wow,you must be very skilled!
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
n
+ echo n
+ tr [a-z] [A-z]
+ answer=N
+ [ N = Y ]
+ echo Neither can I,I'm just an example shell script
Neither can I,I'm just an example shell script
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
y
+ echo y
+ tr [a-z] [A-Z]
+ answer=Y
+ [ Y = Y ]
+ echo Wow,you must be very skilled!
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
关于"set -x"命令的一些附加说明:
使用set命令其实就是使用shell选项来显示脚本运行时的冗余输出。
set命令的一个功能就是打开和关闭shell中的各种选项。在这种情况下,设置的选项是-x或xtrace,这样在运行脚本时,除了正常的输出之处。还会显示代码运行前每一行代码的扩展命令和变量。
如果在脚本文件中加入了命令"set -x"(xtrace)那么在set命令之后执行的每一条命令以及加在命令行中的任何参数(包含变量和变量的值)都会显示出来。每一行之前都会加上加号(+)提示它是跟踪输出的标识。在子shell中执行的shell跟踪命令会加两个加号即"++"。
查看扩展变量值另一个选择就是启用"set -v"命令。
启用"set -v"选项之后的一次运行如下:(没有启用"set -x")
Shell代码
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
echo -n "Can you write device drivers(y/n)?"
Can you write device drivers(y/n)?read answer
y
answer=`echo $answer | tr [a-z] [A-Z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
echo -n "Can you write device drivers(y/n)?"
Can you write device drivers(y/n)?read answer
y
answer=`echo $answer | tr [a-z] [A-Z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
命令的打开与关闭:
和所有的shell选项一样。x(xtrace)和v它们都可以关闭。关闭和打开的语法是相反的。
除了如前面所述使用如-x的减号(-)来打开一个选项,也可以使用加号,例如+x来关闭某个选项。这样就可以在某个点关闭某个选项。这在只需要调试一小段代码时非常有用。
这个时候只需要在代码的错误区域之前打开选项,在错误区域之后关闭选项就可以完成调试了。
关于上面用到的将用户输入的小写转变为大写的
'tr [a-z] [A-Z]的说明如下:
banxi1988@banxi:~/work/shell$ tr --help
用法:tr [选项]... SET1 [SET2]
从标准输入中替换、缩减和/或删除字符,并将结果写到标准输出。
仅在SET1 和SET2 都给出,同时没有-d 选项的时候才会进行替换。
仅在替换时才可能用到-t 选项。如果需要SET2 将被通过在末尾添加原来的末字符的方式
补充到同SET1 等长。SET2 中多余的字符将被省略。只有[:lower:] 和[:upper:]
以升序展开字符;在用于替换时的SET2 中以成对表示大小写转换。-s 作用于SET1,既不
替换也不删除,否则在替换或展开后使用SET2 缩减。
Shell代码
#!/bin/sh
#set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer = ‘echo $answer | tr [a-z] [A-Z]’
if[ $answer = Y] then;
echo "Wow,you must be very skilled!"
esle
echo "Neither can I,I'm just an example shell script"
fi
#!/bin/sh
#set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer = ‘echo $answer | tr [a-z] [A-Z]’
if[ $answer = Y] then;
echo "Wow,you must be very skilled!"
esle
echo "Neither can I,I'm just an example shell script"
fi
[color=darkred]上面存在一些错误。
其一:
'echo $answer | tr [a-z] [A-Z]'
中的单引号应该为tab键上面的反单引号才会把里面的字符串当成是代码,否则就是一个字符串。
空格啊空格啊:
其二:
if[ $answer = Y]
中一句中,"if["三个字符中,没有空格会引起下面的错误报告。
./set_x_disable.sh: 6: if[: not found
[/quote]
显然Y之后也需要有空格。
所以[]之前必须有空格。
其三: answer = 'echo ...' 一句中还有一处错误就是。
“answer”和 “=” 及"'echo'"号之间不能有空格。
否则报错如下:
引用
./set_x_disable.sh: 5: answer: not found [/color]如果在等号右边有空格的话还会把空格当成answer字符串开头值的第一个字符。
坑爹有空格啊。
开始"set -x"运行几次之后。修改之后的脚本代码如下:
Shell代码
#!/bin/sh
set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer=`echo $answer | tr [a-z] [A-z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
#!/bin/sh
set -x
echo -n "Can you write device drivers(y/n)?"
read answer
answer=`echo $answer | tr [a-z] [A-z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
下面是开启了"set -x"之后的两次运行结果:
Shell代码
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
n
+ echo n
+ tr [a-z] [A-z]
+ answer=N
+ [ N = Y ]
+ echo Neither can I,I'm just an example shell script
Neither can I,I'm just an example shell script
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
y
+ echo y
+ tr [a-z] [A-Z]
+ answer=Y
+ [ Y = Y ]
+ echo Wow,you must be very skilled!
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
n
+ echo n
+ tr [a-z] [A-z]
+ answer=N
+ [ N = Y ]
+ echo Neither can I,I'm just an example shell script
Neither can I,I'm just an example shell script
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
+ echo -n Can you write device drivers(y/n)?
Can you write device drivers(y/n)?+ read answer
y
+ echo y
+ tr [a-z] [A-Z]
+ answer=Y
+ [ Y = Y ]
+ echo Wow,you must be very skilled!
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
关于"set -x"命令的一些附加说明:
使用set命令其实就是使用shell选项来显示脚本运行时的冗余输出。
set命令的一个功能就是打开和关闭shell中的各种选项。在这种情况下,设置的选项是-x或xtrace,这样在运行脚本时,除了正常的输出之处。还会显示代码运行前每一行代码的扩展命令和变量。
如果在脚本文件中加入了命令"set -x"(xtrace)那么在set命令之后执行的每一条命令以及加在命令行中的任何参数(包含变量和变量的值)都会显示出来。每一行之前都会加上加号(+)提示它是跟踪输出的标识。在子shell中执行的shell跟踪命令会加两个加号即"++"。
查看扩展变量值另一个选择就是启用"set -v"命令。
启用"set -v"选项之后的一次运行如下:(没有启用"set -x")
Shell代码
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
echo -n "Can you write device drivers(y/n)?"
Can you write device drivers(y/n)?read answer
y
answer=`echo $answer | tr [a-z] [A-Z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
banxi1988@banxi:~/work/shell$ ./set_x_disable.sh
echo -n "Can you write device drivers(y/n)?"
Can you write device drivers(y/n)?read answer
y
answer=`echo $answer | tr [a-z] [A-Z]`
if [ $answer = Y ]
then
echo "Wow,you must be very skilled!"
else
echo "Neither can I,I'm just an example shell script"
fi
Wow,you must be very skilled!
banxi1988@banxi:~/work/shell$
命令的打开与关闭:
和所有的shell选项一样。x(xtrace)和v它们都可以关闭。关闭和打开的语法是相反的。
除了如前面所述使用如-x的减号(-)来打开一个选项,也可以使用加号,例如+x来关闭某个选项。这样就可以在某个点关闭某个选项。这在只需要调试一小段代码时非常有用。
这个时候只需要在代码的错误区域之前打开选项,在错误区域之后关闭选项就可以完成调试了。
关于上面用到的将用户输入的小写转变为大写的
'tr [a-z] [A-Z]的说明如下:
banxi1988@banxi:~/work/shell$ tr --help
用法:tr [选项]... SET1 [SET2]
从标准输入中替换、缩减和/或删除字符,并将结果写到标准输出。
仅在SET1 和SET2 都给出,同时没有-d 选项的时候才会进行替换。
仅在替换时才可能用到-t 选项。如果需要SET2 将被通过在末尾添加原来的末字符的方式
补充到同SET1 等长。SET2 中多余的字符将被省略。只有[:lower:] 和[:upper:]
以升序展开字符;在用于替换时的SET2 中以成对表示大小写转换。-s 作用于SET1,既不
替换也不删除,否则在替换或展开后使用SET2 缩减。