linux教程:shell脚本基本语法

 时间:2026-02-17 13:24:21

1、if判断语句的写法是这样:

#!/bin/bash

a=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/bash

a="hello"

if [ $a = "hello" ]; then

        echo "ok"

fi

linux教程:shell脚本基本语法

4、字符串匹配:

注意比较条件中是[[ 条件 ]],否则会报错。例如:

#!/bin/bash

a="hello"

if [[ $a =~ "hello" ]]; then

        echo "match ok"

else

        echo "match fail"

fi

linux教程:shell脚本基本语法

5、另一种字符串比较方法:

#!/bin/bash

a="hello"

if [[ $a == *"hello"* ]]; then

        echo "match ok"

else

        echo "match fail"

fi

linux教程:shell脚本基本语法

  • 如何设置xshell编码
  • eclipse如何配置tomcat6
  • docker镜像加速方法
  • weblogic 配置数据源(以oracle为例)
  • Oracle开发工具:[1]SQL*Plus工具
  • 热门搜索
    有志不在年高的意思 思域什么梗 robot是什么意思 凌乱什么意思 离殇是什么意思 峰值是什么意思 prior什么意思 鹿柴的意思 冰清玉洁的意思 我顶你个肺什么意思