shell编程

http://wiki.ubuntu.org.cn/Shell%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80#while.2Ffor_.E5.BE.AA.E7.8E.AF

简单的测试:
正确的:
第一段
#!/bin/sh
if [ ${SHELL}="/bin/bash" ];then
echo "your login shell is the bash"
else
echo "your login shell is not bash but ${SHELL}"
fi
第二段
#!/bin/sh
if [ ${SHELL} = "/bin/bash" ];then
echo "your login shell is the bash"
else
echo "your login shell is not bash but ${SHELL}"
fi
第三段
#!/bin/sh
if [ ${SHELL} = "/bin/bash" ]; then
echo "your login shell is the bash"
else
echo "your login shell is not bash but ${SHELL}"
fi
错误的:
第一段

#!/bin/sh
if [${SHELL} = "/bin/bash"]; then
echo "your login shell is the bash"
else
echo "your login shell is not bash but ${SHELL}"
fi
错误提示:
./shtest: line 2: [/bin/bash: 没有那个文件或目录
your login shell is not bash but /bin/bash
第二段
if [${SHELL} = "/bin/bash"]; then
echo"your login shell is the bash"
else
echo"your login shell is not bash but ${SHELL}"
fi
第三段

#!/bin/sh

if [${SHELL}="/bin/bash"]; then
echo"your login shell is the bash"
else
echo"your login shell is not bash but ${SHELL}"
fi
错误提示
./shtest: line 3: [/bin/bash=/bin/bash]: 没有那个文件或目录
./shtest: line 6: echoyour login shell is not bash but /bin/bash: 没有那个文件或目录
第四段
if[ ${SHELL}="/bin/bash"];then
echo"your login shell is the bash"
else
echo"your login shell is not bash but ${SHELL}"
fi

错误提示
./shtest: line 2: syntax error near unexpected token `then'
./shtest: line 2: `if[${SHELL}="/bin/bash"]; then

错误提示:
./shtest: line 2: [/bin/bash: 没有那个文件或目录
./shtest: line 5: echoyour login shell is not bash but /bin/bash: 没有那个文件或目录

能看出上面的细微区别么?

主要是空格问题,if后必须空格,[后空格,]前空格,echo后空格
初设shell编程遇到的问题。

发现自己看贴很不认真,“[color=red]通常用" [ ] "来表示条件测试,注意这里的空格很重要,要确保方括号前后的空格。[/color] ”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值