shell编程之基础语法循环(while、for、until)、判断(if、case)、函数、数组

一、判断

1、if

a、语法格式

1、
if [ ]
then
	xxxxxx
else
	xxxxxxx
fi
2、
if [ ];then
	xxxxx
else
	xxxxxx
fi

b、例题

1、猜一猜csdn的时间

[root@shell shell.test] cat guess.sh
while true
do
time=18
read -p "please enter any num" num
if [ $num -gt $time ]
then
    echo "大了"
elif [ $num -lt $time ]
 then
     echo "小了"
 else
     echo "恭喜你,猜对了"
 exit
 fi
 
done
[root@shell shell.test] sh guess.sh 
please enter any num10
小了
please enter any num19
大了
please enter any num18
恭喜你,猜对了

2、case

a、语法

case  值1 in
	1)
	xxxx
	;;
	2)
	xxxx
	;;
	*)
	xxxx
esac

b、例题

1、rsync进程的启动与停止

[root@shell shell.test] cat rsync.sh 
cat << EOF
=========menu=======
1、启动进程
2、杀死进程
3、重启进程
=========end========
EOF
case $1 in
    1)
        rsync --daemon
        echo "start"
        ;;
    2)
        pkill rsync
        echo "stop"
        ;;
    3)
        pkill rsync
        sleep 2
        rstnc --daemon
        ;;
    *)
        echo "please enter 1,2,3"
esac
[root@shell shell.test] sh rsync.sh 1
=========menu=======
1、启动进程
2、杀死进程
3、重启进程
=========end========
start
[root@shell shell.test] ps -ef |grep rsync
root       2372      1  0 10:40 ?        00:00:00 rsync --daemon
root       2374   1673  0 10:40 pts/0    00:00:00 grep --color=auto rsync

二、循环

常见的循环while,for,until

1、while

a、语法格式

while  true/flase
do
  xxxxxxxxx
done
如果要遍历文件里的行
1、
exec  < file
while read var
do
	xxxx 
done
2、
cat < file
while read var
do
	xxxxxxxx
done
3、
while read var
do 
	xxxxxx
done < file

b、例题

遍历文件每一行
默认换行时分隔符

[root@shell shell.test]# sh  while_file.sh
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
^C
[root@shell shell.test]# cat while_file.sh 
#!/bin/bash

# 1、cat < ./passwd
# 2、exec < ./passwd
while read line
do 
    sleep 2
    echo $line
done <./passwd

2、for循环

a、语法

1、for i in {
    
    1..10} #可以跟数字
2、for i in `eval echo {
     
     $a..$b}`#可以跟变量,但要用eval来二次扫描解析
3、for i in `seq x y`#可以生成数从x到y
4、for i in `cat file`# 可以遍历文件行
5、for (( i=1;i<5;i++ ))#c语言格式也可以
do
	xxxx
done

b、例题

1、以.为分隔符来遍历:在for循环中是以空格为分割符,所以在脚本中要将变量IFS改成以.分割,但最后得改回来。

[root@shell shell.test] cat for_file.sh 
#!/bin/bash
OLD_IFS=$IFS
IFS=$"."
a="www.baidu.com"
for i in $a
do
    sleep 1
    echo $i
done
IFS=$OLD_IFS
[root@shell shell.test] sh for_file.sh 
www
baidu
com

2、99乘法表

[root@shell shell.test] cat 9\*9.sh 
for i in {
    
    1..9}
do
    for j in `eval echo {
     
     1..$i}` 
    do 
        echo -ne "$j*$i=$(($i*$j)) \t"
    done
    echo " "
done
[root@shell shell.test] sh 9\*9.sh 
1*1=1 	 
1*2=2 	2*2=4 	 
1*3=3 	2*3=6 	3*3=9 	 
1*4=4 	2*4=8 	3*4=12 	4*4=16 	 
1*5=5 	2*5=10 	3*5=15 	4*5=20 	5*5=25 	 
1*6=6 	2*6=12 	3*6=18 	4*6=24 	5*6=30 	6*6=36 	 
1*7=7 	2*7=14 	3*7=21 	4*7=28 	5*7=35 	6*7=42 	7*7=49 	 
1*8=8 	2*8=16 	3*8=24 	4*8=32 	5*8=40 	6*8=48 	7*8=56 	8*8=64 	 
1*9=9 	2*9=18 	3*9=27 	4*9=36 	5*9=45 	6*9=54 	7*9=63 	8*9=72 	9*9=81

3、until

条件成立循环结束

a、语法

until 条件
do 
	xxxxxxxx
done

4、脚本运行语法

在这里插入图片描述

5、循环控制

a、continue

跳出本次循环

[root@shell shell.test]# cat continue 
for i in {
    
    1..9}
do
    for j in `eval echo {
     
     1..$i}` 
    do 
        if [ $i -eq 3 ]#当i=3时跳出本次循环
        then
        continue
       fi 
        echo -ne "$j*$i=$(($i*$j)) \t"

    done
    echo " "
done
[root@shell shell.test]# sh continue 
1*1=1 	 
1*2=2 	2*2=4 	 
 
1*4=4 	2*4=8 	3*4=12 	4*4=16 	 
1*5=5 	2*5=10 	3*5=15 	4*5=20 	5*5=25 	 
1*6=6 	2*6=12 	3*6=18 	4*6=24 	5*6=30 	6*6=36 	 
1*7=7 	2*7=14 	3*7=21 	4*7=28 	5*7=35 	6*7=42 	7*7=49 	 
1*8=8 	2*8=16 	3*8=24 	4*8=32 	5*8=40 	6*8=48 	7*8=56 	8*8=64 	 
1*9=9 	2*9=18 	3*9=27 	4*9=36 	5*9=45 	6*9=54 	7*9=63 	8*9=72 	9*9=81 	

continue n:退到第n次循环继续

b、break

break n跳出几次循环

[root@shell shell.test] cat break.sh 
for i in {
    
    1..9}
do
    for j in `eval echo {
     
     1..$i}` 
    do 
        if [ $i -eq 3 ]
        then
            break 2 #跳出两次循环,就全部跳出来了
        fi 
        echo -ne "$j*$i=$(($i*$j)) \t"

    done
    echo " "
done
[root@shell shell.test] sh break.sh
1*1=1 	 
1*2=2 	2*2=4 	 

三、函数

函数在脚本中可以将功能模块化,代码重用,减少代码量。

1、函数的几种定义方法

  • 定义
1、
testfunction01 (){
    
    
echo “123”
}
2、
function testfunc02 (){
    
    
echo “456”
}
3、
function testfunc03 {
    
    
echo “789”
}
  • 调用
functionn testfunc01 张三(参数)

例如

[root@shell shell.test] cat fun。sh
test  (){
    
    
echo $1
}
function test02 (){
    
    
echo $1
}
function test03 {
    
    
echo $1
}
test name 
test02 is
test03 lay       
[root@shell shell.test]# sh fun.sh 
name
is
lay

2、函数返回值

函数的返回值可以用return来定义,用echo $?来看返回值是多少

四、数组

1、数组的定义

1、name=(a b c d)
echo ${name[1]}====>b
echo ${name[@]}====>abcd
2、ip=("$a")#添加变量
3、arry=(`ls`)#添加命令
3、ip=(1 2 3 [10]=11)#可以空过数来给后面直接赋值

2、数组操作

1、获取元素个数,长度
echo ${#name[@]};echo ${#name[*]}
2、打印数组个数
echo ${name[@]};echo ${name[*]}
3、获取元素长度
echo ${#name[x]}#第x+1个元素的长度
4、遍历数组
array=`ls`for i in ${name[@]}
do
	echo $i
done
5、查看定义的所有的数组
`declare -a`
6、`declare -a name([x]=ssss)`更改数组的值,但是这样的话会将其他的值全部删掉,所以要将前面的都加上才能改
7、查看索引
echo ${!name[@]}#索引是从0开始的
8、切片
echo ${name[@]:x:y}#从第x个位置取y个数字

Guess you like

Origin blog.csdn.net/qq_46495338/article/details/114520153