shell了解
变量
1.变量
环境变量 env printenv | less
定义的所有变量 set
2.用户自定义变量
1)基本语法:
(1)定义变量: 变量名=变量值 =号前后不能有空格
(2)全局变量: 局部变量 export 变量名 (在子shell中更改 export 不会影响父shell)
全局变量、只读变量、撤销变量
a=$((1+5))
echo $a
6
[root@hmk scripts]# a=$[5+9]
[root@hmk scripts]# echo $a
14
只读变量
[root@hmk scripts]# readonly b=5
[root@hmk scripts]# echo $b
5
撤销变量
unset 变量名
声明静态变量:readonly变量,不能unset
3.特殊变量
1)基本语法
$n ( 功能描述:n为数字,$0代表该脚本,$1-$9代表第一个到第九个参数,十以内的参数,十以上的参数需要用大括号包括,如${10} )
#!/bin/bash
echo "$0"
echo "$1"
echo "Hello $2"
位置参数
./hello.sh xly kang
[root@hmk scripts]# ./hello.sh xly kang
./hello.sh
xly
Hello kang
$# 获取输入参数的个数 常用于循环,判断参数的个数是否正确
$* 这个变量代表命令行中所有的参数,$*把所有的参数看成一个整体
$@ 这个变量代表命令行中所有的参数,不过$@把每个参数区分对待
$? (最后一次命令的返回状态。如果变量的值为0,证明上一个命令执行正确。)
运算符
1)基本语法
"$((运算式))" 或 "$[运算式]"
[root@hmk scripts]# expr 1 + 2
3
[root@hmk scripts]# expr 5 \* 2
10
[root@hmk scripts]# a=$(expr 5 \* 2)
[root@hmk scripts]# echo $a
10
[root@hmk scripts]# a=`expr 5 \* 3`
[root@hmk scripts]# echo $a
15
条件判断
1)基本语法
(1)test condition
(2)[ condition ] (注意condition前后要有空格)
[root@hmk scripts]# test $a = Hello
[root@hmk scripts]# echo $?
1
条件非空即为true,[ kang ]返回true [ ] 返回false
2)条件判断
(1)两个整数之间的比较
-eq 等于(equal) -ne不等于(not equal)
-lt 小于(less than)-le 小于等于(less equal)
-gt 大于(greater than)-ge大于等于(greater than)
注:如果是字符串之间的比较,用"="判断相等,用"!="判断不等。
(2)按照文件权限进行判断
-r 有读的权限(read)
-w 有写的权限 (write)
-x 有执行的权限 (execute)
&&表示前一条命令执行成功时,才执行后一条命令,||表示上一条执行失败后,执行下一条语句
[root@hmk ~]# [ hk ] && echo OK || echo not ok
OK
[root@hmk ~]# [ ] && echo OK || echo not ok
not ok
流程控制
if判断
1)基本语法
1.单分支
if [ 条件判断 ];then
程序
fi
或者
if [ 条件判断 ]
then
程序
fi
2)多分支
if [ 条件判断 ]
then
程序
elif [ 条件判断式 ]
then
程序
else
程序
fi
case语句
1)基本语法
case $变量名 in
"值1")
如果变量的值等于1,则执行程序1
;;
"值2")
如果变量的值等于1,则执行程序2
;;
……
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac
case.sh
[root@hmk scripts]# cat case.sh
#!/bin/bash
case $1 in
1)
echo 'one'
;;
2)
echo two
;;
*)
echo none
esac
for循环
1)基本语法
for (( 初始值;循环控制条件;变量变化))
do
程序
done
2)案例从一加到一百
#!/bin/bash
sum=0
for(( i=0;i<=100;i++ )) 双小括号 <=
do
sum=$[$sum+$i]
done
echo $sum
[root@hmk scripts]# ./for.sh
5050
基本语法)
for 变量 in 值 1 值 2 值 3 ……
do
程序
done
[root@hmk scripts]# vi for2.sh
#!/bin/bash
for i in ykw xly
do
echo $i love themself
done
~
[root@hmk scripts]# cat for.sh
#!/bin/bash
for i in {1..100}
do
sum=$[ $sum + $i ]
done
echo $sum
while循环
1)基本语法
while [ 条件判断 ]
do
程序
done
[root@hmk scripts]# cat while.sh
#!/bin/bash
sum=0
i=1
while [ $i -le 100 ]
do
sum=$[$sum+$i]
i=$[$i+1]
done
echo $sum
#!/bin/bash
sum=0
i=1
while [ $i -le 100 ]
do
# sum=$[$sum+$i]
# i=$[$i+1]
let sum+=$i
let i+=1
done
echo $sum
read读取控制台输入
1)基本语法
read (选项)(参数)
1.选项:
-p:指定读取时的提示符:
-t: 指定读取值时等待的时间(秒),如果-t不加表示一直等待
2.参数
变量:指定读取值的变量名
[root@hmk scripts]# cat read.sh
#!/bin/bash
read -t 7 -p "Enter your name in 7 seconds :" NN
echo $NN
函数
系统函数
basename
1)基本语法
basename [string/pathname] [suffix] (功能描述:basename命令会删掉所有的前缀包括最后一个('/')字符,然后将字符串显示出来)
basename 可以理解为取路径里的文件名称
[root@hmk scripts]# basename /root/scripts/hello.sh
hello.sh
[root@hmk scripts]# basename /root/scripts/hello.sh .sh
hello
dirname
1)基本语法
dirname 文件绝对路径 (功能描述:从给定的包含绝对路径的文件名中去除文件名(非目录部分),然后返回剩下的路径(目录的部分))
dirname可以理解为取文件路径的绝对路径名称
[root@hmk scripts]# dirname /root/scripts/hello.sh
/root/scripts
自定义函数
1)基本语法
[ function ] funname[()]
{
Action
[return int;]
}
注意: 必须在调用函数地方之前,先声明函数,shell脚本是逐步运行。不会像其他语言一样先编译。
函数返回值,只能通过$?系统变量获得,可以显示加:return返回,如果不加,将以最后一条命令运行结果,作为返回值。return 后跟数值n(0-255)
[root@hmk ~]# vim fun_test.sh
echo "HE: $s"
#!/bin/bash
function add(){
s=$[$1 + $2]
return $s
}
read -p '请输入第一个整数:' a
read -p '请输入第二个整数:' b
add $a $b
echo he: $?
[root@hmk ~]# ./fun_test.sh
请输入第一个整数:2
请输入第二个整数:4
he: 6
[root@hmk ~]# cat fun_test.sh
#!/bin/bash
function add(){
s=$[$1 + $2]
echo $s
}
read -p '请输入第一个整数:' a
read -p '请输入第二个整数:' b
sum=$(add $a $b)
echo "和的平方:"$[$sum*$sum]
[root@hmk ~]# ./fun_test.sh
请输入第一个整数:1
请输入第二个整数:2
和的平方:9