linux教程:shell脚本基本语法

 时间:2026-04-27 00:36:22

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工具
  • 热门搜索
    抽油烟机怎么清洗 夏普电视怎么投屏 晒伤怎么办 电脑分辨率怎么调 qq怎么删除聊天记录 小米账号怎么注销 脂肪粒怎么去除 亚麻籽怎么吃 白鲢鱼怎么做好吃 出生地怎么填