shell脚本学习

Shell脚本规范

开头必须为:#!/bin/bash

脚本执行时候开头为 sh 或 bash

例: sh test.sh 或 bash test.sh

如果想直接执行 如 ./test.sh

将文件设置为可执行

chmod +x test.sh 或 chmod 777 test.sh

特殊变量

$n

基本语法

$n(n 为数字,$0代表该脚本名称,&1~&9代表一到九个参数,十以上的参数 要使用大括号,如${10})

$#

基本语法

$#(获取所有输入参数的个数,常用语循环,判断参数的个数是否正确以及加强脚本健壮性)

$*、$@

基本语法

$*(这个变量代表命令行中所有的参数,$ * 把所有的参数看成一个整体)

$@(这个变量也代表命令行中所有的参数,不过$@把每个参数分区对待)

[atguigu@hadoop101 shells]$ vim parameter.sh 
#!/bin/bash 
echo '==========$n==========' 
echo $0 
echo $1 
echo $2 
echo '==========$#==========' 
echo $# 
echo '==========$*==========' 
echo $* 
echo '==========$@==========' 
echo $@ 
[atguigu@hadoop101 shells]$ ./parameter.sh a b c d e f g
==========$n========== 
./parameter.sh 
a
b
==========$#========== 
7
==========$*========== 
a b c d e f g 
==========$@========== 
a b c d e f g

$?

$?(最后一次执行的命令的返回状态。如果这个变量值为0,证明上一个命令执行正确;)

[atguigu@hadoop101 shells]$ ./helloworld.sh 
hello world 
[atguigu@hadoop101 shells]$ echo $? 
0

运算符

基本语法

"$((运算式))" 或 "$[运算式]"

[atguigu@hadoop101 shells]# S=$[(2+3)*4] 
[atguigu@hadoop101 shells]# echo $S

条件判断

(1) 两个整数之间比较

-eq 等于(equal) -ne 不等于(not equal)

-lt 小于(less than) -le 小于等于(less equal)

-gt 大于 (greater than) -ge 大于等于(greater equal)

(2)按照文件权限判断

-r 有读的权限 -w 有写的权限 -x有执行权限

(3)按照文件类型进行判断

-e 文件存在 -f 文件存在并是个常规文件 -d 文件存在并是目录

流程控制

if判断

基本语法

(1)单分支

if [ 条件判断式 ];then
    程序
fi

if [ 条件判断式 ]
then
    程序
fi

(2)多分支

if [ 条件判断式 ]
then
    程序
elif [ 条件判断式 ]
then
    程序
else
    程序
fi

注意:

[ 条件判断式 ],中括号和条件判断式之间必须有空格

if后面要有空格

[atguigu@hadoop101 shells]$ touch if.sh 
[atguigu@hadoop101 shells]$ vim if.sh
#!/bin/bash 
if [ $1 -eq 1 ] 
then 
echo "banzhang zhen shuai" 
elif [ $1 -eq 2 ] 
then 
echo "cls zhen mei" 
fi
[atguigu@hadoop101 shells]$ chmod 777 if.sh 
[atguigu@hadoop101 shells]$ ./if.sh 1 
banzhang zhen shuai

case语言

基本语法

case $变量名 in
"值1")
程序
;;
"值2")
程序
;;
*)
如果变量的值不存在则执行次程序
;;
esac

注意

(1)case行尾必须为单词 "in" ,每一个模式匹配必须以有括号")" 结束。

(2)双分号 ";;" 表示命令序列结束,相当于java的break。

(3)最后的 "*)" 表示默认模式,相当于java的default。

[atguigu@hadoop101 shells]$ touch case.sh 
[atguigu@hadoop101 shells]$ vim case.sh 
!/bin/bash 
case $1 in 
"1") 
echo "banzhang" 
;;
"2") 
echo "cls" 
;;
*)
echo "renyao" 
;;
esac 
[atguigu@hadoop101 shells]$ chmod 777 case.sh 
[atguigu@hadoop101 shells]$ ./case.sh 1 
banzhang

for循环

基本语法

for (( 初始值;循环控制条件;变量变化 ))
do
程序
done

实例:从1加到100

#!/bin/bash
sum=0
for (( i=0;i<100;i++ ))
do
sum=$[$sum+$i]
done
echo $sum
[atguigu@hadoop101 shells]$ chmod 777 for1.sh 
[atguigu@hadoop101 shells]$ ./for1.sh 
5050

基本语法2

for 变量 in 值1 值2 值3 ...
do
程序
done

实例

[atguigu@hadoop101 shells]$ touch for2.sh 
[atguigu@hadoop101 shells]$ vim for2.sh 
#!/bin/bash 
#打印数字 
for i in cls mly wls 
do 
echo "ban zhang love $i" 
done 
[atguigu@hadoop101 shells]$ chmod 777 for2.sh 
[atguigu@hadoop101 shells]$ ./for2.sh 
ban zhang love cls 
ban zhang love mly
ban zhang love wls

while循环

基本语法

while [ 条件判断式 ]
do
程序
done

实例

从1加到100

[atguigu@hadoop101 shells]$ touch while.sh 
[atguigu@hadoop101 shells]$ vim while.sh 
#!/bin/bash 
sum=0 
i=1
while [$i -le 100]
do
sum = $[$sum+$i]
i=$[$i+1]
done
echo $sum
[atguigu@hadoop101 shells]$ chmod 777 while.sh 
[atguigu@hadoop101 shells]$ ./while.sh 
5050

实例

判断变量是否为空

#!/bin/bash 
​
str=""
if [ ! -n "${str}" ] 
then
echo "字符串为空"
else
echo "字符串不为空"${str}
fi

判断文件是否存在

#!/bin/bash 
​
path=""
if [ ! -d "${path}" ] 
then
echo "路径不存在"
else
echo "路径存在-- "${str}
fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yzzzjj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值