变量
作用域
name是局部变量,正常只能在本shell脚本可见,age是全局变量,所有shell脚本都可见。source执行shell脚本可以使用局部变量是因为没有发生进程切换。
[root@node02 shell]# cat variable.sh
#/bin/bash
name=dushuai
export age=19
sh variable_for_call.sh
#source /opt/project/shell/variable_for_call.sh
[root@node02 shell]#
[root@node02 shell]# cat variable_for_call.sh
#/bin/bash
echo $name
echo $age
while true; do
sleep 1000
done
[root@node02 shell]#
sh variable_for_call.sh执行结果
[root@node02 shell]# sh variable.sh
19
[root@node02 shell]# ps -auxf
root 1085 0.0 0.1 56848 1696 ? Ss 19:24 0:00 login -- root
root 1252 0.0 0.2 108576 2284 tty1 Ss 21:18 0:00 \_ -bash
root 1323 0.0 0.1 106080 1348 tty1 S+ 21:34 0:00 \_ sh variable.sh
root 1324 0.0 0.1 106080 1336 tty1 S+ 21:34 0:00 \_ sh variable_for_call.sh
root 1325 0.0 0.0 100924 620 tty1 S+ 21:34 0:00 \_ sleep 1000
source /opt/project/shell/variable_for_call.sh执行结果
[root@node02 shell]# sh variable.sh
dushuai
19
[root@node02 shell]# ps -auxf
root 1085 0.0 0.1 56848 1696 ? Ss 19:24 0:00 login -- root
root 1252 0.0 0.2 108576 2284 tty1 Ss 21:18 0:00 \_ -bash
root 1315 0.0 0.1 106080 1360 tty1 S+ 21:32 0:00 \_ sh variable.sh
root 1316 0.0 0.0 100924 620 tty1 S+ 21:32 0:00 \_ sleep 1000
参数传递
[root@node02 shell]# cat parameters.sh
#/bin/bash
echo $@
for i in $@; do # $@ "$@" "$*"作用一样,获取所有的入参数组
echo $i
done
echo '============'
for i in "$@"; do
echo $i
done
echo '============'
echo $*
for i in $*; do
echo $i
done
echo '============'
for i in "$*"; do # $* 获取所有入参字符串
echo $i
done
echo '============'
echo $# # $# 获取入参的个数
echo '============'
echo $$ # $$ 获取脚本的pid
echo '============'
echo $0 # $0 获取脚本的第0个参数,也就是脚本名
echo '============'
[root@node02 shell]# sh parameters.sh p1 p2 p3
p1 p2 p3
p1
p2
p3
============
p1
p2
p3
============
p1 p2 p3
p1
p2
p3
============
p1 p2 p3
============
3
============
1134
============
parameters.sh
============
[root@node02 shell]#
[root@node02 shell]# echo $? # $?获取脚本执行结果
0
[root@node02 shell]#
字符串
[root@node02 ~]# str=dushuai #字符串长度
[root@node02 ~]# echo ${#str}
7
[root@node02 ~]# str=dushuai #截取str,从第0位开始往后数两位结束
[root@node02 ~]# echo ${str:0:2}
du
数组
[root@node02 ~]# arr=("cat" "dog" "monkey") #初始化数组
[root@node02 ~]# echo ${arr[0]} #打印第1个元素
cat
[root@node02 ~]# echo ${arr[@]} #打印所有元素
cat dog monkey
[root@node02 ~]# echo ${#arr[@]} #打印元素个数
3
[root@node02 ~]# echo ${#arr[*]} #打印元素个数
3
[root@node02 ~]#
注释
[root@node02 shell]# cat annotate.sh
#/bin/bash
#------------------------
# annotate.sh
# author: dushuai
# e-mail: tdcq799@163.com
#-------------------------
:<<EOF
annotate.sh
author: dushuai
e-mail: tdcq799@163.com
EOF
:<<!
annotate.sh
author: dushuai
e-mail: tdcq799@163.com
!
运算符
算术运算符
[root@node02 shell]# cat operator.sh
#/bin/bash
a=20
b=30
expr $a + $b # 加
expr $a - $b # 减
expr $a \* $b # 乘
expr $a / $b # 除
expr $a % $b # 取余
c=$a # 赋值
if [ $a == $c ] # 判断相等
then
echo 'a == c'
fi
if [ $a != $b ] # 判断不等
then
echo 'a != b'
fi
[root@node02 shell]#
[root@node02 shell]# sh operator.sh
50
-10
600
0
20
a == c
a != b
关系运算符
[root@node02 shell]# cat operator.sh
#/bin/bash
a=20
b=30
if [ $a -eq $b ]
then
echo 'a equal b'
else
echo 'a not equal b'
fi
if [ $a -ne $b ]
then
echo 'a not equal b'
else
echo 'a equal b'
fi
if [ $a -gt $b ]
then
echo 'a greater than b'
else
echo 'a not greater than b'
fi
if [ $a -lt $b ]
then
echo 'a less than b'
else
echo 'a not less than b'
fi
if [ $a -ge $b ]
then
echo 'a greater than or equal b'
else
echo 'a not greater than or equal b'
fi
if [ $a -le $b ]
then
echo 'a less than or equal b'
else
echo 'a not less than or equal b'
fi
[root@node02 shell]# sh operator.sh
a not equal b
a not equal b
a not greater than b
a less than b
a not greater than or equal b
a less than or equal b
[root@node02 shell]#
布尔运算符
[root@node02 shell]# cat operator.sh
#/bin/bash
a=20
b=30
if [ ! $a == $b ]
then
echo 'a not equal b'
else
echo 'a equal b'
fi
if [ $a -lt 100 -a $b -lt 100 ] # -a 不短路与,放到[]里使用
then
echo 'a less than 100 and b less than 100'
else
echo 'wrong'
fi
if [ $a -lt 100 -o $b -gt 100 ] # -o 不短路或,放到[]里使用
then
echo 'a less than 100 or b grater than 100'
else
echo 'wrong'
fi
[root@node02 shell]#
[root@node02 shell]# sh operator.sh
a not equal b
a less than 100 and b less than 100
a less than 100 or b grater than 100
[root@node02 shell]#
逻辑运算符
[root@node02 shell]# cat operator.sh
#/bin/bash
a=20
b=30
if [[ $a -lt 100 && $b -lt 100 ]] # 短路与,放在[[]]里使用,[[]]是内置关键字
then
echo 'a less than 100 and b less than 100'
else
echo 'wrong'
fi
if [[ $a -lt 100 || $b -gt 100 ]] # 短路或,放在[[]]里使用
then
echo 'a less than 100 or b grater than 100'
else
echo 'wrong'
fi
[root@node02 shell]# sh operator.sh
a less than 100 and b less than 100
a less than 100 or b grater than 100
字符串运算符
[root@node02 shell]# cat operator.sh
#/bin/bash
a="abc"
b="def"
if [ $a = $b ]
then
echo 'a equals b'
else
echo 'a not equals b'
fi
if [ $a != $b ]
then
echo 'a not equals b'
else
echo 'a equals b'
fi
if [ -z $a ]
then
echo 'a length is 0'
else
echo 'a length is not 0'
fi
if [ -n $a ]
then
echo 'a length is not 0'
else
echo 'a length is 0'
fi
if [ $a ] # a如果没有初始化,判断结果位false
then
echo 'a is not empty'
else
echo 'a is empty'
fi
[root@node02 shell]#
[root@node02 shell]# sh operator.sh
a not equals b
a not equals b
a length is not 0
a length is not 0
a is not empty
文件测试运算符
[root@node02 shell]# cat operator.sh
#/bin/bash
file="/opt/project/shell/score.txt"
if [ -r $file ]
then
echo "文件可读"
else
echo "文件不可读"
fi
if [ -w $file ]
then
echo "文件可写"
else
echo "文件不可写"
fi
if [ -x $file ]
then
echo "文件可执行"
else
echo "文件不可执行"
fi
if [ -f $file ]
then
echo "文件存在且为普通文件"
else
echo "wrong"
fi
if [ -d $file ]
then
echo "文件是个目录"
else
echo "文件不是个目录"
fi
if [ -s $file ]
then
echo "文件不为空"
else
echo "文件为空"
fi
if [ -e $file ]
then
echo "文件存在"
else
echo "文件不存在"
fi
[root@node02 shell]#
[root@node02 shell]# sh operator.sh
文件可读
文件可写
文件不可执行
文件存在且为普通文件
文件不是个目录
文件不为空
文件存在
逻辑语句
if
[root@node02 shell]# cat flowcontroll.sh
#/bin/bash
a=20
b=30
if [ $a -eq $b ]
then
echo "a equals b"
elif [ $a -lt $b ]
then
echo "a less than b"
elif [ $a -gt $b ]
then
echo "a greater than b"
else
echo "wrong"
fi
[root@node02 shell]#
[root@node02 shell]# sh flowcontroll.sh
a less than b
case
[root@node02 shell]# cat testcase.sh
#/bin/bash
echo 'input num is:'
read num
case $num in
1) echo 'input is 1'
;; # 相当于break
2) echo 'input is 2'
;;
3) echo 'input is 3'
;;
4) echo 'input is 4'
;;
*) echo 'wrong num' # 相当于default
;;
esac
[root@node02 shell]# sh testcase.sh
input num is:
4
input is 4
[root@node02 shell]#
for
[root@node02 shell]# cat testfor.sh
#/bin/bash
for i in 1 2 3 4
do
echo $i
done
for str in 'a' 'b' 'c'
do
echo $str
done
[root@node02 shell]# sh testfor.sh
1
2
3
4
a
b
c
[root@node02 shell]#
example:
for i in `seq -w 1 21`;do echo "http://thns.tsinghua.edu.cn/thnsebooks/ebook73/$i"; done
for i in `seq -f"http://thns.tsinghua.edu.cn/thnsebooks/ebook73/%02g.pdf" 1 21`;do echo $i; done
for i in {1..5}; do echo -n "$i "; done
while
[root@node02 shell]# cat testwhile.sh
#/bin/bash
i=1
while(($i<=5))
do
echo $i
let "i++"
done
echo '==============='
i=1
while(($i<=5))
do
echo $i
let "i++"
if (($i==3))
then
break
fi
done
echo '==============='
i=1
while(($i<=5))
do
if (($i==3))
then
let "i++"
continue
fi
echo $i
let "i++"
done
[root@node02 shell]# sh testwhile.sh
1
2
3
4
5
===============
1
2
===============
1
2
4
5
[root@node02 shell]#
函数
[root@node02 shell]# cat testfunction.sh
#/bin/bash
function1() {
echo "input is $1"
#return $(($1+1))
return `expr $1 + 1`
}
function1 2
echo $?
[root@node02 shell]# sh testfunction.sh
input is 2
3
[root@node02 shell]#
其他关键字
echo
[root@node02 shell]# cat operator.sh
#/bin/bash
echo -n "abc" # 不换行回显
echo "def"
echo -e "abc\ndef" # 特殊字符不转义回显
[root@node02 shell]# sh operator.sh
abcdef
abc
def
test
[root@node02 shell]# cat operator.sh
#/bin/bash
a=20
b=30
if test $a -eq $b # 和[]作用一样
then
echo 'a equals b'
else
echo 'a not equals b'
fi
[root@node02 shell]# sh operator.sh
a not equals b
awk
seq
Shell seq 命令_shell seq命令-CSDN博客
sed
nl
https://blog.51cto.com/mengbo2006214/1967573
grep
参考:
https://www.cnblogs.com/FengZeng666/p/15323028.html
https://blog.csdn.net/eidolon_foot/article/details/135717160
https://blog.csdn.net/kouryoushine/article/details/91361718
https://blog.csdn.net/qq_36326332/article/details/112543884
https://www.jb51.net/article/206537.htm
https://www.cnblogs.com/soymilk2019/p/14978480.html
https://blog.csdn.net/u012060033/article/details/104310372?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-104310372-blog-133384203.235^v43^pc_blog_bottom_relevance_base5&spm=1001.2101.3001.4242.2&utm_relevant_index=4
https://www.runoob.com/linux/linux-comm-awk.html
https://blog.csdn.net/aikudexiaohai/article/details/133509113
https://www.runoob.com/linux/linux-comm-sed.html
https://blog.51cto.com/mengbo2006214/1967573
https://www.bilibili.com/video/BV1Eq4y1z7u8?p=13&spm_id_from=pageDriver&vd_source=67d7114b0eb64b14ba400a66f04f0da8
https://www.w3cschool.cn/shellbook/u2fjkozt.html
https://www.runoob.com/linux/linux-comm-grep.html
https://www.jb51.net/article/283696.htm