linux教程:shell脚本基本语法

2025-07-06 22:44:37

1、if判断语句的写法是这样:#!/bin/basha=1;if [ $a -lt 10 ]; then echo "ok"fi其中a=1中间一定不能有空格,如果a<10打印ok。

linux教程:shell脚本基本语法

2、保存文件。然后给文件授权:chmod +x test.sh然后执行脚本./test.sh

linux教程:shell脚本基本语法

3、字符串相等:#!/bin/basha="hello"if [ $a = "hello" ]; then echo "ok"fi

linux教程:shell脚本基本语法

4、字符串匹配:注意比较条件中是[[ 条件 ]],否则会报错。例如:#!/bin/basha="he造婷用痃llo"if [[ $a =~ "hello" ]]; then echo "match ok"else echo "match fail"fi

linux教程:shell脚本基本语法

5、另一种字符串比较方法:#!/bin/basha屏顿幂垂="hello"if [[ $a == *"he造婷用痃llo"* ]]; then echo "match ok"else echo "match fail"fi

linux教程:shell脚本基本语法
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢