【嵌入式Linux学习】shell脚本入门

举报
ReCclay 发表于 2022/02/22 01:02:50 2022/02/22
【摘要】 一般地,shell脚本必须以#!/bin/bash开头read读取并打印的例子。【注意read读取的变量与引号之间一定要加空格!!!】 #!/bin/bash read -p "Input your n...
  • 一般地,shell脚本必须以#!/bin/bash开头
  • read读取并打印的例子。【注意read读取的变量与引号之间一定要加空格!!!】
#!/bin/bash
read -p "Input your name and age: " name age
echo "Your name is $name, your age is $age"

  
 
  • 1
  • 2
  • 3
  • 数值运算时,变量要加两个括号。【#注意等号前后不要加空格!】
#!/bin/bash
echo "Input two int num:"
read -p "first: " first
read -p "second: " second
total=$((first + second))
echo "$first + $second = $total"


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • test判断文件是否存在
#!/bin/bash
echo "Input file name"
read -p "filename " filename
test -e $filename && echo "$filename exist" || echo "$filename not exist"


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • test查看字符串是否一样
#!/bin/bash
echo "Input two string"
read -p "firstStr " firstStr
read -p "secondStr " secondStr
test $firstStr == $secondStr && echo "equal String" || echo "Not equal"

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • []判断符。注意中括号要与里面的内容用空格空开,并且里面的字符串必须加双引号!
o "Input two string"
read -p "firstStr " firstStr
read -p "secondStr " secondStr
[ $firstStr == $secondStr ] && echo "equal String" || echo "Not equal"


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 默认变量
!/bin/bash
echo "filename:" $0
echo "total param num:" $#
echo "whole param:" $@
echo "first param:" $1
echo "second param:" $2


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 条件判断。【注意then前面是个分号】
#!/bin/bash

read -p "please input(y/n):" value

if [ "$value" == "Y" ] || [ "$value" == "y" ]; then
    echo "your input is Y"
    exit 0
fi

if [ "$value" == "N" ] || [ "$value" == "n" ]; then
    echo "your input is N"
    exit 0
fi


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 多个条件判断。【注意结束fi只有一个!】
#!/bin/bash

read -p "please input(y/n):" value

if [ "$value" == "Y" ] || [ "$value" == "y" ]; then
    echo "your input is Y"
    exit 0
elif [ "$value" == "N" ] || [ "$value" == "n" ]; then
    echo "your input is N"
    exit 0
else
    echo "your input can't identify!!!"
fi


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • case语句。【注意最后的通配符不能用引号括起来!】
#!/bin/bash

case $1 in
    "a")
        echo "param is: a"
        ;;
    "b")
        echo "param is: b"
        ;;
    *)
        echo "can't identify!!!"
        ;;
esac


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 函数调用。【注意函数调用的时候不需要加括号】
#!/bin/bash

function help(){
    echo "this is help cmd"

}
function close(){
    echo "this is close cmd"
}

case $1 in
    "-h")
        help
        ;;
    "-c")
        close
        ;;
esac


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 函数传参。【注意传参时,同样不需要加括号!!!】
#!/bin/bash

function print(){
    echo "param 1: $1"
    echo "param 2: $2"

}

print a b


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • while循环
in/bash
while [ "$value" != "close" ]
do
    read -p "please input str:" value
done

echo "stop while loop"


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • for循环
#!/bin/bash

for name in zzk zz1 zz2 zz3
do
    echo "your name: $name"
done


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 常见形式for循环【注意,for是两个括号!!!】
!/bin/bash
read -p "please input count: " count

sum=0
for((i=0; i<count; i++))
do
    sum=$(($sum+$i))
done

echo "1+2+...+$count=$sum"


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。

原文链接:recclay.blog.csdn.net/article/details/102882574

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。