shell 编程中空格的使用–< 转>
2012-03-10 21:16
615 查看
from http://blog.csdn.net/panda19881/article/details/6626727
1.定义变量时, =号的两边不可以留空格.
eg:
gender=femal————right
gender =femal———–wrong
gender= femal———–wrong
2.条件测试语句 [ 符号的两边都要留空格.
eg:
if [ $gender = femal ]; then——-right.
echo “you are femal”;
fi
if[ $gender...-----------------------wrong
if [$gender...----------------------wrong.
3.条件测试的内容,如果是字符串比较的话, 比较符号两边要留空格!
eg:
if [ $gender = femal ]; then——-right.
if [ $gender= femal ]; then——–wrong.
if [ $gender=femal ]; then———wrong.
4.如果if 和 then写在同一行, 那么,注意, then的前面要跟上 ; 号.如果 then 换行写, 那么也没问题.
eg:
if [ $gender = femal ]; then——-right.
if [ $gender = femal ]
then——————————-right.
if [ $gender = femal ] then——-wrong. then前面少了 ; 号.
提示出错信息:
syntax error near unexpected token then
同理,还有很多出错信息 比如
syntax error near unexpected token fi 等都是这样引起的.
5.if 后面一定要跟上 then. 同理
elif 后面一定要跟上 then.
不然提示出错信息:
syntax error near unexpected token else
1)if 语句后面需要跟着then,同时前面要有分号;
2) 空格非常重要,shell 会认为空格前的为一个命令,如果a=3 认为是赋值操作,如果写成a = 3,那么就会认为a为一个命令 this=`ls -l |grep ‘^-’ | wc -l `
3) 操作符之间要用空格分开 ,如 test ! -d $1,其中的!和-d就要用空格分开
空格是命令解析中的重要分隔符
6. 命令和其后的参数或对象之间一定要有空格
if [ -x"~/Workspace/shell/a.sh" ];then
只有 -x 后有空格才表示紧跟其后的字符串是否指向一个可执行的文件名,否则就成了测试 -x"~/Workspace/shell/a.sh" 这个字符串是不是空。
7.取变量值的符号'$'和后边的变量或括号不能有空格
1.定义变量时, =号的两边不可以留空格.
eg:
gender=femal————right
gender =femal———–wrong
gender= femal———–wrong
2.条件测试语句 [ 符号的两边都要留空格.
eg:
if [ $gender = femal ]; then——-right.
echo “you are femal”;
fi
if[ $gender...-----------------------wrong
if [$gender...----------------------wrong.
3.条件测试的内容,如果是字符串比较的话, 比较符号两边要留空格!
eg:
if [ $gender = femal ]; then——-right.
if [ $gender= femal ]; then——–wrong.
if [ $gender=femal ]; then———wrong.
4.如果if 和 then写在同一行, 那么,注意, then的前面要跟上 ; 号.如果 then 换行写, 那么也没问题.
eg:
if [ $gender = femal ]; then——-right.
if [ $gender = femal ]
then——————————-right.
if [ $gender = femal ] then——-wrong. then前面少了 ; 号.
提示出错信息:
syntax error near unexpected token then
同理,还有很多出错信息 比如
syntax error near unexpected token fi 等都是这样引起的.
5.if 后面一定要跟上 then. 同理
elif 后面一定要跟上 then.
不然提示出错信息:
syntax error near unexpected token else
1)if 语句后面需要跟着then,同时前面要有分号;
2) 空格非常重要,shell 会认为空格前的为一个命令,如果a=3 认为是赋值操作,如果写成a = 3,那么就会认为a为一个命令 this=`ls -l |grep ‘^-’ | wc -l `
3) 操作符之间要用空格分开 ,如 test ! -d $1,其中的!和-d就要用空格分开
空格是命令解析中的重要分隔符
6. 命令和其后的参数或对象之间一定要有空格
if [ -x"~/Workspace/shell/a.sh" ];then
只有 -x 后有空格才表示紧跟其后的字符串是否指向一个可执行的文件名,否则就成了测试 -x"~/Workspace/shell/a.sh" 这个字符串是不是空。
7.取变量值的符号'$'和后边的变量或括号不能有空格
相关文章推荐
- 在编程竞赛中使用<bits/stdc++.h>
- SoapUI使用注意事项——在<!CDATA>节点之前不要有空格
- <车载物联网项目,视频采集传输部分,第二天> 使用ffserver输出视频文件流,并且使用使用命令行终端播放视频文件
- <c:forEach>标签与el表达式联合的使用
- 使用struts2的<s:param>等标签向action传值出现中文乱码的解决方法
- &lt;org manual&gt;翻译--4.5 在Org-mode外使用链接
- 使用<jsp:include>,不想写死URL,动态生成URL的解决的方法
- <<gdb使用经验技巧>>
- <总结>delphi WebBrowser控件的使用中出现的bug
- <<编程人生>>摘录
- <Linux+Qt>在Widget中使用qlabel显示图片与动画
- <html:select>的使用
- <welcome-file-list>标签作用,怎样使用
- <linux+qt>使用thread监控串口通信(二)
- <s:iterator> 中的status 使用方法
- java中使用<jsp:forward>报404错误
- AJAX跨域方法之一—使用<script>实现
- <Boost> boost::thread 多线程的使用
- 使用<span>标签为文字设置单独样式
- <cstdlib>下system函数的使用