- 论坛徽章:
- 0
|
本帖最后由 时间看来 于 2011-10-24 15:50 编辑
您好,我刚开始学习写shell script,遇到如下问题:
脚本如下:- #!/bin/sh
- debug=1
- test $debug -gt 0 && echo "Debug is on"
- echo -n "Can you write device drivers?"
- read answer
- test $debug -gt 0 && echo "The answer is $answer"
- answer='echo $answer | tr [a-z] [A-Z]'
- if [$answer=Y]
- then
- echo Wow,you must be very skilled
- test $debug -gt 0 && echo "the answer is $answer"
- else
- echo Neither can I, Im just an example shell script
- test $debug -gt 0 && echo "The answer is $answer"
- fi
复制代码 可是运行结果是:- [root@localhost shell_try]# ./var_replace.sh
- Debug is on
- Can you write device drivers?y
- The answer is y
- ./var_replace.sh: line 8: [echo: command not found
- Neither can I, Im just an example shell script
- The answer is echo $answer | tr [a-z] [A-Z]
- [root@localhost shell_try]# ./var_replace.sh
- Debug is on
- Can you write device drivers?n
- The answer is n
- ./var_replace.sh: line 8: [echo: command not found
- Neither can I, Im just an example shell script
- The answer is echo $answer | tr [a-z] [A-Z]
- [root@localhost shell_try]#
复制代码 可以看到脚本代码第7行的,answer='echo $answer | tr [a-z] [A-Z]'
没有转换成功。
求正确的写法,非常感谢您的时间
=============================================================
经过您的指导:原来answer=$(echo $answer | tr [a-z] [A-Z]),这样才正确地将小写字母转换成大写。
但是看到很多用answer='echo $answer | tr [a-z] [A-Z]'的形式,这里用的是反引号(~键处的`),不是单引号(“键处的')。
所以正确的写法是 answer=`echo $answer | tr [a-z] [A-Z]` 有微小的差别。
if [$answer=Y]中[]前后需要加空格~~ |
|