shell函数,if(test),while,until,for,case

shell函数,if(test),while,until,for,case

1.shell函数

shell中函数定义的语法格式如下:

function  name ()
{
    action;
    [return int;]
}
name #调用函数

说明:
1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。
3、和其他编程语言不同的是,shell函数在定义时不能指明参数,但是在调用时却可以传递参数,并且给他传递什么参数他就接收什么参数

#!/bin/bash
#定义函数
function url(){
	echo "http://www.baidu.com"
}
url
#!/bin/bash
#计算所有参数的和
function getsum(){
	local sum=0
	for n in $@
	do 
	   	((sum+=n))
	done
	return $sum
}
#调用函数并传递参数
getsum 11 22 33 44
echo $?
# $@:表示函数的所有参数
# $?:表示函数的退出状态(返回值)

if(test)

if condition
then
    command1 
fi

请注意condition后边的分号;当if和then位于同一行的时候,这个分号是必须的,否则会有语法错误
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述
1数值比较

n1 -eq n2 检查n1是否与n2相等 (equal)
n1 -ge n2 检查n1是否大于或等于n2 (greater and equal)
n1 -gt n2 检查n1是否大于n2 (greater than)
n1 -le n2 检查n1是否小于或等于n2 (less and equal)
n1 -lt n2 检查n1是否小于n2 (less than)
n1 -ne n2 检查n1是否不等于n2 (not equal)

例如:

#!/bin/bash
#数值条件测试可以用在数字和变量上
val1=10
val2=11
if [ $val1 -gt 5 ]
then
        echo "the test value $val1 is greater than 5"
fi

if [ $val1 -eq $val2 ]
then
        echo "the value are equal"
else
        echo "the value are different"
fi

在这里插入图片描述

#!/bin/bash
val1=`echo "scale=4;10 / 3 " | bc`
echo "the test value is $val1"
#不能在test命令中使用浮点值
if [ $val1 -gt 3 ]
then
        echo "the result is larger than 3"
fi

在这里插入图片描述
2字符串比较

str1 = str2 检查str1是否和str2相同
str1 != str2 检查str1是否和str2不同
str1 < str2 检查str1是否比str2小
str1 > str2 检查str1是否比str2大
-n str1 检查str1的长度是否非0
-z str1 检查str1的长度是否为0

例如:

#!/bin/bash
#字符串的相等性
#比较字符串相等性的时候 test的比较会将所有的标点和大写也考虑在内
testing=student
if [ $USER != $testing ]
then
        echo "this is not $testing"
else
        echo "Welcome $testing"
fi

在这里插入图片描述

#!/bin/bash
val1=baseball
val2=hokey
if [ $val1 \> $val2 ]
then
        echo "$val1 is greater than $val2"
else
        echo "$val1 is lesser than $val2"
fi

在这里插入图片描述
注意:大于小于符号必须转义,否则shell会把他们当作重定向符号而把字符串当作文件名
字符串比较的顺序是按ASCII表的顺序的,大写字母比小写字母的值小。

#!/bin/bash
val1=testing
val2=''
if [ -n "$val1" ]
then
        echo "the string '$val1' is not empty"
else
        echo "the string '$val1' is empty"
fi

if [ -z "$val2" ]
then
        echo "the string '$val2' is empty"
else
        echo "the string '$val2' is not empty"
fi

在这里插入图片描述3 3.文件比较
这是shell编程中最强大的也是最常用到的比较。test命令允许你测试Linux文件系统上文件和目录的状态,如下表:

-d file 检查file是否存在并是一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧

例如:

#!/bin/bash
if [ -d $HOME ]
then
        echo " your home directory exists"
        cd $HOME
        ls -a
else
        echo "there is a problem with your home directory"
fi

在这里插入图片描述

#!/bin/bash
if [ -e $HOME ]
then
        echo "OK on the directory. now to check the file"
        if [ -e $HOME/testing ]
        then
                echo "appending date to existing file"
                date >> $HOME/testing
        else
                echo "Creating new file"
                date > $HOME/testing
fi
else
        echo "sorry, you do not have a home dirctory"
fi

在这里插入图片描述

#!/bin/bash
if [ -e $HOME ]
then
        echo "the object exists.is it a file?"
        if [ -f $HOME ]
        then
                echo "yes,it is a file "
        else
                echo "no,it is not a file"
                if [ -f $HOME/.bash_history ]
                then
                        echo "but this is a file "
                fi
        fi
else
        echo "sorry.the object does not exists"
fi

在这里插入图片描述

#!/bin/bash
pwdfile=/etc/shadow

if [ -f $pwdfile ]
then
        if [ -r $pwdfile ]
        then
                tail $pwdfile
        else
                echo "sorry"

        fi
else
        echo "sorry"
fi

在这里插入图片描述

#!/bin/bash
file=test
touch $file
if [ -s $file ]
then
        echo "the $file file exists and has data in it"
else
        echo "the $file exists and is empty"
fi

在这里插入图片描述

while循环

while循环是shell脚本中最简单的一种循环,当条件满足时,while重复地执行一组语句,当条件不满足时,就退出while循环。
shell while用法如下:

while condition
do	
	statements
done

其中,codition表示判断条件,statements表示要执行的语句(可以只有一条,也可以由多条),do和done都是shell中的关键字

例如:

#!/bin/bash
#计算1-100的和
i=1
sum=0
while ((i<=100))
do
        ((sum+=i))
        ((i++))
done
echo "the num is $sum"

在这里插入图片描述

#!/bin/bash
#从m加到n的值
read m
read n
sum=0
while ((m<=n))
do
        ((sum+=m))
        ((m++))
done
echo "the num is $sum"

在这里插入图片描述
在终端中读取数据,可以等于在文件中读取数据,按下ctrl+d组合键表示读取到文件流的末尾,此时read就会读取失败,得到一个非0的退出状态 从而导致判断条件不成立 结束循环

#!/bin/bash
sum=0
echo "input num:(enter ctrl+d end)"
while read n
do
        ((sum+=n))
done
echo "the sum is:$sum"

在这里插入图片描述

until

until循环和while循环恰好相反,当判断条件不成立时才进行循环,一旦判断条件成立,就终止循环
shell until的用法如下:

until condition
do
	statements
done

condition表示判断条件,statements表示要执行的语句(可以只有一条,也可以由多条),do和done都是shell中的关键字

例如:

#!/bin/bash
i=1
sum=0
until ((i>100))
do
        ((sum+=i))
        ((i++))
done
echo "$sum"

在这里插入图片描述

for循环

c语言风格的for循环

for ((expr1; expr2; expr3))
do
	command
done
#!/bin/bash
sum=0
for ((i=1;i<=100;i++))
do
        ((sum+=i))
done
echo "$sum"

在这里插入图片描述

使用类C风格for循环要注意以下事项:
a.如果循环条件最初的退出状态为非0,则不会执行循环体
b.当执行更新语句时,如果循环条件的退出状态永远为0,则for循环将永远执行下去,从而产生死循环
c.Linux shell中不运行使用非整数类型的数作为循环变量
d.如果循环体中的循环条件被忽略,则默认的退出状态为0
e.在类C风格的for循环中,可以将三个语句全部忽略掉,

#!/bin/bash
sum=0
for i in 1 2 3 4 5 6
do
        echo $i
        ((sum+=i))
done
echo "$sum"

在这里插入图片描述

#!/bin/bash
#给出一个取值范围{start..end}
#注意:中间用两个点连接
#这种形式只支持数字和字母
sum=0
for i in {1..50}
do
        ((sum+=i))
done
echo $sum

在这里插入图片描述
seq 是一个linux命令,用来产生某个范围内的整数,并且可以设置步长
在这里插入图片描述

#!/bin/bash
for filename in $(ls *.sh)
do
        echo $filename
done

在这里插入图片描述

case

case语法:

case "字符串变量" in
  值1)指令1...
;;
  值2)指令2...
;;
      ......
      *)指令3...
esac

例如:

#!/bin/bash
printf "Input integer number: "
    read num
    case $num in
    1)
    echo "Monday"
    ;;
    2)
    echo "Tuesday"
    ;;
    3)
    echo "Wednesday"
    ;;
    4)
    echo "Thursday"
    ;;
    5)
    echo "Friday"
    ;;
    6)
    echo "Saturday"
    ;;
    7)
    echo "Sunday"
    ;;
    *)
    echo "error"
    esac

在这里插入图片描述

发布了70 篇原创文章 · 获赞 0 · 访问量 1287

猜你喜欢

转载自blog.csdn.net/AaNiceMan/article/details/104311913
今日推荐