shell基础语法

119 阅读3分钟

变量

your_name="runoob.com"
  • 变量名和等号之间不能有空格
  • 命名规则
    • 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头。

    • 中间不能有空格,可以使用下划线(_)。

    • 不能使用标点符号。

    • 不能使用bash里的关键字(可用help命令查看保留关键字)。

使用变量

只要在变量名前面加美元符号即可

your_name="qinjx"
echo $your_name
echo ${your_name} // 这样写也可以

删除变量

unset variable_name

变量类型

  1. 局部变量: 局部变量在脚本或命令中定义,仅在当前shell实例中有效,其他shell启动的程序不能访问局部变量。
  2. 环境变量: 所有的程序,包括shell启动的程序,都能访问环境变量,有些程序需要环境变量来保证其正常运行。必要的时候shell脚本也可以定义环境变量。
  3. shell变量: shell变量是由shell程序设置的特殊变量。shell变量中有一部分是环境变量,有一部分是局部变量,这些变量保证了shell的正常运行

字符串

字符串的拼接

your_name="runoob"

greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"
echo $greeting  $greeting_1

获取字符串长度

string="abcd"
echo ${#string} #输出 4

提取子字符串

以下实例从字符串第 2 个字符开始截取 4 个字符:

string="runoob is a great site"
echo ${string:1:4} # 输出 unoo

数组

支持一维数组(不支持多维数组),并且没有限定数组的大小。

array_name=(value0 value1 value2 value3)

单独定义数组的各个分量:

array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen

读取数组

valuen=${array_name[n]}

获取数组中的所有元素

使用@ 或 * 可以获取数组中的所有元素

my_array[0]=A
my_array[1]=B
my_array[2]=C
my_array[3]=D

echo "数组的元素为: ${my_array[*]}"
echo "数组的元素为: ${my_array[@]}"

获取数组的长度

 # 取得数组元素的个数
length=${#array_name[@]}
 # 或者
length=${#array_name[*]}

基本运算符

原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。 expr 是一款表达式计算工具,使用它能完成表达式的求值操作。

例如,两个数相加(注意使用的是反引号 ` 而不是单引号 '):

val=`expr 2 + 2` # 表达式和运算符之间要有空格,例如 2+2 是不对的
echo "两数之和为 : $val" #4

关系运算符

关系运算符只支持数字,不支持字符串,除非字符串的值是数字。

  • -eq 等于
  • -ne 不等
  • -gt 大于
  • lt 小于
  • -ge大于等于
  • -le 小于等于

布尔运算符

  • !
  • -o
  • -a
[ $a -lt 5 -o $b -gt 100 ] # "$a 小于 5 或 $b 大于 100 : 返回 true"

逻辑运算符

  • && 逻辑的 AND
  • || 逻辑的 OR

字符串运算符

  • = 相等
  • != 不等
  • -z 字符串长度为0返回true
  • -n 字符串长度为0返回false
  • str 检测字符串是否为空,不为空返回 true

文件测试运算符

  • -b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ] 返回 false。
  • -c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
  • -d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ] 返回 false。
  • -f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
  • -g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ] 返回 false。
  • -k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ] 返回 false。
  • -p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
  • -u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ] 返回 false。
  • -r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
  • -w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
  • -x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
  • -s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
  • -e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ] 返回 true。
file="/var/www/runoob/test.sh"
if [ -r $file ]
then
   echo "文件可读"
else
   echo "文件不可读"
fi

流程控制

if 语句语法格式

if condition
then
    command1 
    command2
    ...
    commandN 
fi

if else 语法格式

if condition
then
    command1 
    command2
    ...
    commandN
else
    command
fi

if else-if else 语法格式:

if condition1
then
    command1
elif condition2 
then 
    command2
else
    commandN
fi

for 循环

for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done
for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done
for str in 'This is a string'
do
    echo $str
done

while 语句

while condition
do
    command
don

until 循环

until condition
do
    command
done

case

case 值 in
模式1)
    command1
    command2
    ...
    commandN
    ;;
模式2)
    command1
    command2
    ...
    commandN
    ;;
esac

跳出循环

break