shell的运算符有哪些
数学计算的命令有哪些,并举例使用
用例:
8 & 8 -> 8 1和1才是1
10 & 8 8 和
10 | 8 10 与
10 ^ 8 2 异或 不同的取1 相同取0
${}中的使用,并举例包含(#, ##,%,%%,:, /, //),并举例使用
截取字符串
${parameter}:返回变量的内容
[root@wl class]# var_a=12345678 #定义变量var_a
[root@wl class]# echo $var_a
12345678
[root@wl class]# echo ${var_a}
12345678
${#parameter}:返回变量内容的长度(字符数)
[root@wl class]# echo ${#var_a}
8
${paramater:offset} :从offset位置之后开始提取子串,到结尾
[root@wl class]# echo ${var_a:2} #从位置2开始之后(3开始)提取到末尾
345678
[root@wl class]# echo ${var_a:3}
45678
${paramater:offset:length}:从offset位置之后开始提取,提取长度为length(可为负)的子串
[root@wl class]# echo ${var_a:2:3} #从2之后(3开始)提取长度为3的子串
345
[root@wl class]# echo ${var_a:2:5}
34567
删除字符串(删除格式必须从开头开始连续)
${parameter#word}:从变量开头开始删除最短匹配的word子串(无法匹配,则不删)
[root@wl class]# var_c=book.testfile.tar.gz #定义变量var_c
[root@wl class]# echo ${var_c#*.}
testfile.tar.gz
${parameter##word} :从变量开头开始删除最长匹配的word子串(无法匹配,则不删)
[root@wl class]# echo ${var_c##*.}
gz
${parameter%word} :从变量结尾开始删除最短匹配的word子串(无法匹配,则不删)
[root@wl class]# echo ${var_c%.*}
book.testfile.tar
${parameter%%word}:从变量结尾开始删除最长匹配的word子串(无法匹配,则不删)
[root@wl class]# echo ${var_c%%.*}
book
替换字符串
${parameter/pattern/string} :使用string替换第一个匹配到的pattern内容
[root@wl class]# var_b="hello word and haha hello word xixi"
[root@wl class]# echo ${var_b/hello word/nihaoa}
nihaoa and haha hello word xixi
${parameter//pattern/string}:使用string替换所有匹配到的pattern内容
[root@wl class]# echo ${var_b//hello word/nihaoa}
nihaoa and haha nihaoa xixi
条件测试的语法 (()), [[]],[],test,功能以及语法格式
①test示例:
[root@wl class]# ll total 0 -rw-r--r--. 1 root root 0 Feb 20 10:35 file
[root@wl class]# test -f file;echo $?
0
[root@wl class]# test -f file1;echo $?
1
[root@wl class]# test -x file;echo $?
1
②[]示例(注意测试表达式和方括号两边需要有空格)
[root@wl class]# ll
total 0 -rw-r--r--. 1 root root 0 Feb 20 10:35 file
[root@wl class]# [ -f file ];echo $?
0
[root@wl class]# [ -f file1 ];echo $?
1
[root@wl class]# [ -w file ];echo $?
0
③[[]]示例(注意测试表达式和[[]]两边需要有空格)
[root@wl class]# ll total 0 -rw-r--r--. 1 root root 0 Feb 20 10:35 file
[root@wl class]# [[ -f file ]];echo $? 0
[root@wl class]# [[ -f file1 ]];echo $? 1
[root@wl class]# [[ -x file ]];echo $? 1
④(())示例
[root@wl class]# (( 2!=3 ));echo $? 0
[root@wl class]# ((2!=3));echo $? 0
[root@wl class]# ((2=3));echo $?
-bash: ((: 2=3: attempted assignment to non-variable (error token is "=3") 1
[root@wl class]# ((2==3));echo $?
1
[root@wl class]# ((2>3));echo $?
1
[root@wl class]# ((2<3));echo $?
0
假设执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据
程序的文件名;共有几个参数;若参数的个数小于2个则告知用户参数数量太少;全部的参数内容;第一 个参数;第二个参数。
echo "the script name is $0"
echo "the paramaeter number is $#"
[ "$#" -lt 2 ] && echo "the number of parameter is less than 2."&& exit 0
echo "your whole parameter is '$@'"
echo "the 1st parameter is $1"
echo "the 2nd parameter ia $2"
输出:
[kiosk@foundation0 2]$ bash ./test2.sh 3
the script name is ./test2.sh
the paramaeter number is 1
the number of parameter is less than 2.
[kiosk@foundation0 2]$ bash ./test2.sh 1 2
the script name is ./test2.sh
the paramaeter number is 2
your whole parameter is '1 2'
the 1st parameter is 1
the 2nd parameter ia 2