(shell脚本知识)正点原子嵌入式Linux-18,19-shell脚本入门/shel脚本条件判断,函数和循环

shell脚本

  • shell脚本就是将连续运行的命令整理成一个文件,将命令封装执行。
  • shell脚本的第一行内容固定为:#!/bin/bash
  • shell脚本一般以.sh结尾
  • 在创建shell脚本后,需要将shell脚本文件权限改为可执行
  • shell脚本=两边不加空格

shell脚本语法

  • test检查命令1

  • if语句
    在这里插入图片描述2

    #!/bin/bash
    
    read -p "please input(Y/N)" input
    
    if [ "$input" == "Y" ]||[ "$input" == "y" ];then
        echo "user input y"
        exit 0
    fi
    
    if [ "$input" == "N" ]||[ "$input" == "n" ]: then
        echo "yout input is N"
        exit 0
    fi 
    
  • case语句3

    casein
    模式1)
        command1
        command2
        command3
        ;;
    模式2)
        command1
        command2
        command3
        ;;
    *)
        command1
        command2
        command3
        ;;
    esac
    
    echo 'Input a number between 1 to 4'
    echo 'Your number is:\c'
    read aNum
    case $aNum in
        1)  echo 'You select 1' # 其实这里加不加""结果都是一样的
        ;;
        2)  echo 'You select 2'
        ;;
        3)  echo 'You select 3'
        ;;
        4)  echo 'You select 4'
        ;;
        *)  echo 'You do not select a number between 1 to 4'
        ;;
    esac
    
  • shell脚本函数4

    [ function ] funname [()]
    
    {
    
        action;
    
        [return int;]
    
    }
    

    这里function可以加可以不加

  • shell循环

  1. while 循环5
    while [ 条件 ]
    do
    	循环内容
    done
    
    until [ 条件 ]
    do
    	循环内容
    done
    
  2. for循环6
    for i in 1 2 3 4 5;do #详细列出(字符且项数不多)
        echo $i
    done
    
    
    for ((i=1;i<=5;i++));do #((语法循环--有点像C语法,但记得双括号
        echo $i
    done
    

shell脚本示例

#!/bin/bash

echo "input something:"     # 输出
read input                  # 输入
echo "you input is:" $input
#!/bin/bash
read -p "please input your age and height" age height   # -p选项用于显示提示信息
echo "you age=$age, height=$height"
$(())  # 在括号中进行数值运算
#!/bin/bash

read -p "please input two num:" first second
total=$((first+second))
echo "$first+$second=$total"
cmd1 $$ cmd2   # 当cmd1执行并为真,cmd2开始执行
cmd1 || cmd2   # 当cmd1执行并为真,则cmd2不执行,否则cmd2执行 
#!/bin/bash

read -p "please input the file you want to check:" filename
test -e $filename && echo "$filename exist" || echo "$filename does't exist"     # 查看文件是否存在
#!/bin/bash

read -p "please input two string:" firststr secondstr
# 注意中括号左右两边需要使用空格空开
[ "$firststr == $secondstr" ] && echo "$filename exist" || echo "$filename does't exist"     # 判断字符串是否相同
read -p "please input two string:" firststr secondstr
# 注意==两边需要使用空格空开
test $firststr == $secondstr && echo "$firststr == $secondstr" || echo "$firststr != $secondstr"   
read -p "please input two string:" firststr secondstr
# 注意==两边需要使用空格空开
test $firststr == $secondstr && echo "$firststr == $secondstr" || echo "$firststr != $secondstr"   

$n

#!/bin/bash

echo "file name:$0"          # 输出文件名   
echo "total input parameters:$#"  # 输出参数总数
echo "output all parameters:$@"   # 输出所有的输入参数
echo "output first input parameter:$1" #输出第一个输入参数
echo "output second input parameter:$2" # 输出第2个输入参数

运行结果为
在这里插入图片描述

函数

demoFun(){
    echo "这是我的第一个 shell 函数!"
}
echo "-----函数开始执行-----"
demoFun
echo "-----函数执行完毕-----"

执行结果:

-----函数开始执行-----
这是我的第一个 shell 函数!
-----函数执行完毕-----
funWithReturn(){
    echo "这个函数会对输入的两个数字进行相加运算..."
    echo "输入第一个数字: "
    read aNum
    echo "输入第二个数字: "
    read anotherNum
    echo "两个数字分别为 $aNum$anotherNum !"
    return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? !"

注意使用$?来获取函数返回值
执行结果:

这个函数会对输入的两个数字进行相加运算...
输入第一个数字: 
1
输入第二个数字: 
2
两个数字分别为 1 和 2 !
输入的两个数字之和为 3 !
#!/bin/bash
# author:菜鸟教程
# url:www.runoob.com

funWithParam(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    echo "第十个参数为 ${10} !"
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
执行结果:
第一个参数为 1 !
第二个参数为 2 !
第十个参数为 10 !
第十个参数为 34 !
第十一个参数为 73 !
参数总数有 11 个!
作为一个字符串输出所有参数 1 2 3 4 5 6 7 8 9 34 73 !

while循环

#!/bin/bash
i=1
sum=0
while [ $i -le 100 ]
do
  let sum=sum+$i
  let i++
done

echo $sum        # 5050

for循环

#!/bin/bash
 
#打印1~5的for语句
for i in {1..5};do  #数字段形式
    echo $i
done
 
for i in 1 2 3 4 5;do #详细列出(字符且项数不多)
    echo $i
done
for ((i=1;i<=5;i++));do #((语法循环--有点像C语法,但记得双括号
    echo $i
done
 
for i in `seq 5`;do  #seq形式 起始从1开始,sequence序列
    echo $i
done
#!/bin/bash
 
#for in语句与` `和$( )合用,利用` `或$( )的将多行合为一行的缺陷,实际是合为一个字符串数组
for i in `ls`;do
    echo $i
done
#robot  test2.sh  test.sh  workspace
 
for i in $(ls);do
    echo $i
done
#robot  test2.sh  test.sh  workspace
 
 
#打印当前目录下的.sh文件
for  i in $(ls *.sh);do
    echo $i
done
#--->test.sh  test2.sh
 
#打印当前目录下的.sh文件
for  i in *.sh;do #"*.sh"不是命令,不需要与``,$()来联合使用,若用就会出错
    echo $i
done
#--->test.sh  test2.sh
 
for i in ls ;do #若是命令而不与``,$()来联合使用,就只会打印命令
    echo $i
done
#-->ls
 
for i in f1 f2 f3 ;do
    echo $i
done
#-->f1 f2 f3
 
LIST="root usr data data2"
#打印除带有空格的字符串
for d in $LIST;do #用for in语句自动对字符串按空格遍历的特性,对多个目录遍历 
    echo $d
done
#--->root usr data data2
 
#打印出/var/log/路径下的所有文件和文件夹,注意有路径
for i in /var/log/*;do
    echo $i   
done
#-->/var/log/yum.log /var/log/httpd ...
#--打印当前目录下的文件名和文件夹名,注意是没有路径的
for i in *;do
    echo $i  
done
#-->workspace test.sh group1 test2.sh

挖坑

  • 需要在查询shell命令时补充shell脚本的相关知识

参考资料


  1. https://www.runoob.com/linux/linux-shell-test.html ↩︎

  2. https://blog.csdn.net/doiido/article/details/43966819 ↩︎

  3. https://wiki.jikexueyuan.com/project/shell-tutorial/shell-case-esac-statement.html ↩︎

  4. https://www.runoob.com/linux/linux-shell-func.html ↩︎

  5. https://blog.csdn.net/wdz306ling/article/details/79602739 ↩︎

  6. https://blog.csdn.net/shimazhuge/article/details/40820069 ↩︎

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值