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工具
  • 热门搜索
    约莫的近义词 天然气表怎么换电池 中短发发型图片 张字怎么写好看 柔美的近义词 照片大小怎么改 安卓版本怎么升级 头胀痛是怎么回事 垂头丧气的近义词 美妙的近义词