shell--编程
来源:互联网 发布:简单视频剪辑电脑软件 编辑:程序博客网 时间:2024/06/06 05:00
0.Shell替换:Shell变量替换,命令替换,转义字符
如果表达式中包含特殊字符,Shell 将会进行替换。例如,在双引号中使用变量就是一种替换,转义字符也是一种替换。
举个例子:
- #!/bin/bash
- a=10
- echo -e "Value of a is $a \n"
Value of a is 10这里 -e 表示对转义字符进行替换。如果不使用 -e 选项,将会原样输出:
Value of a is 10\n
下面的转义字符都可以用在 echo 中:
命令替换
命令替换是指Shell可以先执行命令,将输出结果暂时保存,在适当的地方输出。命令替换的语法:
- `command`
下面的例子中,将命令执行结果保存在变量中:
- #!/bin/bash
- DATE=`date`
- echo "Date is $DATE"
- USERS=`who | wc -l`
- echo "Logged in user are $USERS"
- UP=`date ; uptime`
- echo "Uptime is $UP"
Date is Thu Jul 2 03:59:57 MST 2009Logged in user are 1Uptime is Thu Jul 2 03:59:57 MST 200903:59:57 up 20 days, 14:03, 1 user, load avg: 0.13, 0.07, 0.15
变量替换
变量替换可以根据变量的状态(是否为空、是否定义等)来改变它的值可以使用的变量替换形式:
若此替换出现在Shell脚本中,那么脚本将停止运行。${var:+word}如果变量 var 被定义,那么返回 word,但不改变 var 的值。
请看下面的例子:
#!/bin/bashecho ${var:-"Variable is not set"}echo "1 - Value of var is ${var}"echo ${var:="Variable is not set"}echo "2 - Value of var is ${var}"unset varecho ${var:+"This is default value"}echo "3 - Value of var is $var"var="Prefix"echo ${var:+"This is default value"}echo "4 - Value of var is $var"echo ${var:?"Print this message"}echo "5 - Value of var is ${var}"
运行结果:
- Variable is not se
- 1 - Value of var is
- Variable is not set
- 2 - Value of var is Variable is not set
- 3 - Value of var is
- This is default value
- 4 - Value of var is Prefix
- Prefix
- 5 - Value of var is Prefix
Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等
ash 支持很多运算符,包括算数运算符、关系运算符、布尔运算符、字符串运算符和文件测试运算符。
原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
例如,两个数相加:
- #!/bin/bash
- val=`expr 2 + 2`
- echo "Total value : $val"
Total value : 4两点注意:
- 表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2,这与我们熟悉的大多数编程语言不一样。
- 完整的表达式要被 ` ` 包含,注意这个字符不是常用的单引号,在 Esc 键下边。
算术运算符
先来看一个使用算术运算符的例子:
- #!/bin/sh
- a=10
- b=20
- val=`expr $a + $b`
- echo "a + b : $val"
- val=`expr $a - $b`
- echo "a - b : $val"
- val=`expr $a \* $b`
- echo "a * b : $val"
- val=`expr $b / $a`
- echo "b / a : $val"
- val=`expr $b % $a`
- echo "b % a : $val"
- if [ $a == $b ]
- then
- echo "a is equal to b"
- fi
- if [ $a != $b ]
- then
- echo "a is not equal to b"
- fi
a + b : 30a - b : -10a * b : 200b / a : 2b % a : 0a is not equal to b注意:
- 乘号(*)前边必须加反斜杠(\)才能实现乘法运算;
- if...then...fi 是条件语句,后续将会讲解。
注意:条件表达式要放在方括号之间,并且要有空格,例如 [$a==$b] 是错误的,必须写成 [ $a == $b ]。
关系运算符
关系运算符只支持数字,不支持字符串,除非字符串的值是数字。先来看一个关系运算符的例子:
- #!/bin/sh
- a=10
- b=20
- if [ $a -eq $b ]
- then
- echo "$a -eq $b : a is equal to b"
- else
- echo "$a -eq $b: a is not equal to b"
- fi
- if [ $a -ne $b ]
- then
- echo "$a -ne $b: a is not equal to b"
- else
- echo "$a -ne $b : a is equal to b"
- fi
- if [ $a -gt $b ]
- then
- echo "$a -gt $b: a is greater than b"
- else
- echo "$a -gt $b: a is not greater than b"
- fi
- if [ $a -lt $b ]
- then
- echo "$a -lt $b: a is less than b"
- else
- echo "$a -lt $b: a is not less than b"
- fi
- if [ $a -ge $b ]
- then
- echo "$a -ge $b: a is greater or equal to b"
- else
- echo "$a -ge $b: a is not greater or equal to b"
- fi
- if [ $a -le $b ]
- then
- echo "$a -le $b: a is less or equal to b"
- else
- echo "$a -le $b: a is not less or equal to b"
- fi
10 -eq 20: a is not equal to b10 -ne 20: a is not equal to b10 -gt 20: a is not greater than b10 -lt 20: a is less than b10 -ge 20: a is not greater or equal to b10 -le 20: a is less or equal to b
布尔运算符
先来看一个布尔运算符的例子:
- #!/bin/sh
- a=10
- b=20
- if [ $a != $b ]
- then
- echo "$a != $b : a is not equal to b"
- else
- echo "$a != $b: a is equal to b"
- fi
- if [ $a -lt 100 -a $b -gt 15 ]
- then
- echo "$a -lt 100 -a $b -gt 15 : returns true"
- else
- echo "$a -lt 100 -a $b -gt 15 : returns false"
- fi
- if [ $a -lt 100 -o $b -gt 100 ]
- then
- echo "$a -lt 100 -o $b -gt 100 : returns true"
- else
- echo "$a -lt 100 -o $b -gt 100 : returns false"
- fi
- if [ $a -lt 5 -o $b -gt 100 ]
- then
- echo "$a -lt 100 -o $b -gt 100 : returns true"
- else
- echo "$a -lt 100 -o $b -gt 100 : returns false"
- fi
10 != 20 : a is not equal to b10 -lt 100 -a 20 -gt 15 : returns true10 -lt 100 -o 20 -gt 100 : returns true10 -lt 5 -o 20 -gt 100 : returns false
字符串运算符
先来看一个例子:
- #!/bin/sh
- a="abc"
- b="efg"
- if [ $a = $b ]
- then
- echo "$a = $b : a is equal to b"
- else
- echo "$a = $b: a is not equal to b"
- fi
- if [ $a != $b ]
- then
- echo "$a != $b : a is not equal to b"
- else
- echo "$a != $b: a is equal to b"
- fi
- if [ -z $a ]
- then
- echo "-z $a : string length is zero"
- else
- echo "-z $a : string length is not zero"
- fi
- if [ -n $a ]
- then
- echo "-n $a : string length is not zero"
- else
- echo "-n $a : string length is zero"
- fi
- if [ $a ]
- then
- echo "$a : string is not empty"
- else
- echo "$a : string is empty"
- fi
abc = efg: a is not equal to babc != efg : a is not equal to b-z abc : string length is not zero-n abc : string length is not zeroabc : string is not empty
文件测试运算符
文件测试运算符用于检测 Unix 文件的各种属性。例如,变量 file 表示文件“/var/www/tutorialspoint/unix/test.sh”,它的大小为100字节,具有 rwx 权限。下面的代码,将检测该文件的各种属性:
- #!/bin/sh
- file="/var/www/tutorialspoint/unix/test.sh"
- if [ -r $file ]
- then
- echo "File has read access"
- else
- echo "File does not have read access"
- fi
- if [ -w $file ]
- then
- echo "File has write permission"
- else
- echo "File does not have write permission"
- fi
- if [ -x $file ]
- then
- echo "File has execute permission"
- else
- echo "File does not have execute permission"
- fi
- if [ -f $file ]
- then
- echo "File is an ordinary file"
- else
- echo "This is sepcial file"
- fi
- if [ -d $file ]
- then
- echo "File is a directory"
- else
- echo "This is not a directory"
- fi
- if [ -s $file ]
- then
- echo "File size is zero"
- else
- echo "File size is not zero"
- fi
- if [ -e $file ]
- then
- echo "File exists"
- else
- echo "File does not exist"
- fi
File has read accessFile has write permissionFile has execute permissionFile is an ordinary fileThis is not a directoryFile size is zeroFile exists
0 0
- shell 以及shell编程
- Shell和Shell编程
- 【shell】shell 编程基础
- Shell及其Shell编程
- Shell编程
- Shell编程
- Shell编程
- shell编程
- Shell编程
- Shell编程
- shell 编程
- Shell编程
- Shell编程
- shell编程
- shell编程
- Shell编程
- shell编程
- Shell 编程
- 微信公众号阅读10万+的文章其实充满套路
- shiro权限框架中的认证和授权过程
- struts2常用注解
- 1063. 计算谱半径(20)
- KMP算法-串的匹配问题
- shell--编程
- TensorFlow学习笔记(四):CNN之AlexNet网络
- freemarker自定义标签
- poi导出Excel工具类
- 整理matlab图像处理
- ARC forbids explicit message send of 'retainCount'
- union-find算法的研究
- WinSCP的使用
- socket取流阻塞