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] ”
简单的测试:
正确的:
第一段
#!/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] ”