目录
(2)expr + , - , \* , / , % 加 减 乘 除 取余
(5)多条件判断(&& 表示前一条命令执行成功,才执行后一条命令,|| 表示上一条命令执行失败,才执行下一条命令)
一、两种执行方式
1、sh hello.sh
本质是bash 解析器帮你执行脚本,所以脚本本身不需要执行权限
2、./hello.sh
本质是 脚本需要自己执行,所以需要执行权限
二、一个简单的shell脚本
1、需求
在root家目录下的shell目录下的test目录下 创建 test1.txt 文件,并输入内容 aaaa
2、编辑shell脚本如下
#!/bin/sh
cd /root/shell/test
touch test1.txt
echo "aaaa" >> test1.txt
> 是覆盖
>> 是追加
三、shell中的特殊变量
1、 $n
(1) 基本语法
n 为数字 ,$0 代表该脚本的名称,$1 - $9 代表第一到第九个参数,十以上的参数,需要用大括号包含,比如 ${10}
(2)一个简单的输入参数小例子
脚本内容如下:
#!/bin/sh
echo "$0 $1 $2"
执行如下:
[root@hadoop shell]# sh test1.sh aa bb
test1.sh aa bb
2、 $#
(1)基本语法
获取所有输入参数的个数,常用于循环
(2)一个简单的例子
脚本内容
#!/bin/sh
echo "$0 $1 $2"
echo "$#"
执行结果
[root@hadoop shell]# sh test1.sh aa bb
test1.sh aa bb
2
3、 $* 和 $@
(1)基本语法
$* (这个变量代表命令行中所有的参数,把所有参数看做一个整体)
$@ (这个变量也代表命令行中所有的参数,但是他把每个参数区分对待)
4、 $?
(1)基本语法
最后一次执行命令的返回状态。若为0代表上一个命令执行正确,否则代表上一个命令执行的不正确。
(2)小例子
[root@hadoop shell]# cd .
[root@hadoop shell]# echo $?
0
[root@hadoop shell]# cd.
-bash: cd.: command not found
[root@hadoop shell]# echo $?
127
四、运算符
1、基本语法
(1)$((运算式子)) 或者 $[运算式]
(2)expr + , - , \* , / , % 加 减 乘 除 取余
注意 expr 运算符之间要有空格
2、案例
(1)简单的四则运算
[root@hadoop shell]# expr 2 + 3
5
[root@hadoop shell]# expr 2 \* 3
6
(2)复杂一点的乘法(3+2)*4
[root@hadoop shell]# expr `expr 3 + 2` \* 4
20
(3)常用的 $[运算式] 解决 (3+2)*4
[root@hadoop shell]# echo $[(3+2)*4]
20
五、条件判断
1、基本语法
[ condition ] (注意 condition为条件 ,其前后都有空格)
注意:条件非空即为true。
2、常用条件判断
(1)两个整数之间的比较
= | 字符串比较 |
-lt | 小于(less than) |
-le | 小于等于(less equal) |
-eq | 等于(equal) |
-gt | 大于(greater than) |
-ge | 大于等于(greater equal) |
-ne | 不等于(not equal) |
(2)文件权限判断
-r | 有读的权限(read) |
-w | 有写的权限(write) |
-x | 有执行的权限(execute) |
(3)文件类型判断
-f | 文件存在,并且是一个常规的文件(file) |
-e | 文件存在(existence) |
-d | 文件存在,并且是一个目录(directory) |
(4)简单小例子
[root@hadoop shell]# [ 23 -ge 24 ]
[root@hadoop shell]# echo $?
1
[root@hadoop shell]# [ -x hello.sh ]
[root@hadoop shell]# echo $?
0
(5)多条件判断(&& 表示前一条命令执行成功,才执行后一条命令,|| 表示上一条命令执行失败,才执行下一条命令)
六、流程控制
1、if判断
(1)基本语法
if [ 条件判断式 ]
then
程序
elif
then
程序
else
程序
fi
(2)案例实操
需求:输入一个数字,如果是1,则输出 TRUE!,如果是2.则输出 FALSE!,如果是其它,则什么也不输出。
shell脚本如下:
#!/bin/sh
if [ $1 -eq "1" ]
then
echo "TRUE!"
elif [ $1 -eq "2" ]
then
echo "FALSE!"
fi
执行如下:
[root@hadoop shell]# sh test2.sh 1
TRUE!
[root@hadoop shell]# sh test2.sh 2
FALSE!
[root@hadoop shell]# sh test2.sh 3
2、case 判断
(1)基本语法
case $变量名 in
"值1")
执行程序 1
;;
"值2")
执行程序 2
;;
*)
执行默认(变量值不在以上选择里)
;;
esac
(2) 案例实操
需求:输入一个数字,如果是1,输出 TRUE,如果是2,输出FALSE,如果是其它,输出WIN
shell脚本如下:
#!/bin/sh
case $1 in
"1")
echo "TRUE"
;;
"2")
echo "FALSE"
;;
*)
echo "WIN"
;;
esac
脚本执行如下
[root@hadoop shell]# sh test3.sh 1
TRUE
[root@hadoop shell]# sh test3.sh 2
FALSE
[root@hadoop shell]# sh test3.sh 44
WIN
3、for 循环
(1)基本语法1
for ((初始值;循环控制条件;变量变化))
do
程序
done
(2)案例实操1
需求:从 1 加到 100
shell 脚本如下:
#!/bin/sh
sum=0
for ((i=1;i<=100;i++))
do
sum=$[$sum+$i]
done
echo $sum
执行结果如下
[root@hadoop shell]# sh test4.sh
5050
(3)基本语法2
for 变量 in 值1 值2 值3
do
程序
done
解释:将后面的值一一赋给前面的变量
(4)案例实操2 一般和 $* 和 $@ 在一起使用
需求:打印所有输入参数
shell 脚本如下
#!/bin/sh
for i in "$*"
do
echo "aaaaa $i"
done
for j in "$@"
do
echo "bbbb $j"
done
执行情况如下
[root@hadoop shell]# sh test5.sh oo pp gg
aaaaa oo pp gg
bbbb oo
bbbb pp
bbbb gg
4、while 循环
(1)基本语法
while [ 条件判断式 ]
do
程序
done
(2)案例实操1
需求 从1加到100
shell 脚本如下
#!/bin/sh
s=0
i=0
while [ $i -le 100 ]
do
s=$[$s+$i]
i=$[$i+1]
done
echo $s
执行结果如下
[root@hadoop shell]# sh test6.sh
5050
七、read 读取控制台输入
1、基本语法
read (选项)(参数)
选项:
-p 指定读取时的提示符
-t 指定读取值时等待的时间(秒)
参数:
变量:指定读取值的变量名
2、案例实操
(1)提示 7 秒内读取控制台输入的名称
shell 编程如下
#!/bin/sh
read -t 7 -p "Enter your name in 7 second: " name
echo $name
执行结果如下
[root@hadoop shell]# sh test7.sh
Enter your name in 7 second: aa
aa
八、函数
1、系统函数
(1)basename
截取一个文件绝对路径中,最后的文件名,如下:
[root@hadoop shell]# basename /root/shell/hello.sh
hello.sh
[root@hadoop shell]# basename /root/shell/hello.sh .sh
hello
(2)dirname
截取一个文件绝对路径中,去除文件名的路径,如下:
[root@hadoop shell]# dirname /root/shell/hello.sh
/root/shell
2、自定义函数
(1)基本语法
[ function ] funname[()]
{
Action;
[return int;]
}
funname
(2) 一个例子
需求 控制台上输入两个值,函数接受 并对他们求和输出
shell脚本如下
#!/bin/sh
function sum()
{
s=0
s=$[$1+$2]
echo $s
}
read -p 'input 1 num:' a
read -p 'input 2 num:' b
sum $a $b
执行结果如下
[root@hadoop shell]# sh test8.sh
input 1 num:1
input 2 num:2
3