您现在的位置是:网站首页> 编程资料编程资料
shell 编程中空格的使用方法_linux shell_
2023-05-26
424人已围观
简介 shell 编程中空格的使用方法_linux shell_
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.取变量值的符号'$'和后边的变量或括号不能有空格
- Linux命令行和shell脚本编程宝典 Richard Blum
- shell脚本编程之for语句、if语句使用介绍
- Linux shell脚本编程if语句的使用方法(条件判断)
- 5个Shell脚本编程入门练习例子
- Shell脚本编程中常用的数学运算实例
- Shell 编程:Bash空格的那点事
- shell脚本编程实现9*9乘法表
- Linux Shell 脚本编程入门教程
- Shell脚本编程之判断语句
- shell脚本编程之循环语句
- Shell编程中while与for的区别及用法详解
- Linux下Shell编程快捷键大全(日常整理)
- 详解Shell编程之变量数值计算(一)
- 详解Shell编程之if语句实战(小结)
- Shell编程之case语句实战(小结)
- 对Linux下shell编程之for循环的实例讲解
- Shell脚本编程30分钟入门(小结)
- shell编程入门之变量,字符串,数组,函数使用方法总结
相关内容
- shell中的各种括号的使用方法_linux shell_
- Shell脚本读取标准ini配置文件Demo_linux shell_
- shell脚本实现实时检测文件变更_linux shell_
- Shell脚本注册到Linux系统服务实例_linux shell_
- Shell脚本实现C语言代码行数统计_linux shell_
- Shell脚本中的特殊字符(美元符、反斜杠、引号等)作用介绍_linux shell_
- Shell脚本中的位置变量参数(特殊字符)实例讲解_linux shell_
- Shell脚本传递参数的3种方法比较_linux shell_
- Shell脚本传参数方法总结_linux shell_
- Shell中的变量使用小结_linux shell_
