变量
字符串
#!/bin/sh
test_first=hello
test_first='hello'
test_first="hello"
test_second=2
# 定义变量 不能有空格
# 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头
# 不能使用bash里的关键字
echo "hello" = echo hello
# 输出结果: hello
# 显示普通字符串双引号可省略
echo "\"hello\"" = echo \"hello\"
# 输出结果: "hello"
# 显示转义字符 双引号可省略
echo $test_first = echo ${test_first}
# 使用变量 这里 {} 可以省略
echo "Ah... ${test_first}World"
# 这种不能省略 {}
# 字符串拼接
test_third="world"
"hello, "$test_third" !" = "hello, ${test_third} !" = 'hello, '$test_third' !'
# 不等于 'hello, $test_third !'
# 字符串有三种定义方法 单引号、双引号、不用引号
# 双引号里可以有变量,也可以出现转义字符
# 单引号里的所有内容原样输出,单引号成对出现,作为字符串拼接
# 获取字符串长度
echo ${#test_third}
# 输出结果: 5
# 提取子字符串
echo ${test_third:1:3}
# 输出结果: orl
echo ${test_third:1}
# 输出结果: orld
删除子串
test_third="liushuangqiao@139.com"
# * 是通配符
${string#substring}
# 从变量${string}的开头删除最短匹配${substring}的子串
echo ${test_third#*i}
# 输出结果: ushuangqiao@139.com
${string##substring}
# 从变量${string}的开头删除最短匹配${substring}的子串
echo ${test_third##*i}
# 输出结果: ao@139.com
${string%substring}
# 从变量${string}的结尾删除最短匹配${substring}的子串
echo ${test_third%.*}
# 输出结果: liushuangqiao@139
${string%%substring}
# 从变量${string}的结尾删除最长匹配${substring}的子串
echo ${test_third%%i*}
# 输出结果: l
替换
${string/substring/replacement}
# 使用$replacement, 来代替第一个匹配的$substring
${string//substring/replacement}
# 使用$replacement, 代替所有匹配的$substring
${string/#substring/replacement}
# 如果$string的前缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement}
# 如果$string的后缀匹配$substring, 那么就用$replacement来代替匹配到的$substring
# index
# replace
# 用 ls /etc 语句给变量赋值
for file in `ls /etc` = for file in $(ls /etc)
# 以上语句将 /etc 下目录的文件名循环出来
readonly test_first
# 将变量设为只读,只读变量不能被改变 也不能被删除
unset test_second
# 删除变量
数组
array_name=(value0 value1 value2 value3)
=
array_name=(
value0
value1
value2
value3
)
=
array_name[0]=value0
array_name[1]=value1
array_name[n]=valuen
读取
${数组名[下标]}
valuen=${array_name[n]}
# 使用 @ 符号可以获取数组中的所有元素,例如:
echo ${array_name[@]}
# 获取数组长度的方法与获取字符串长度的方法相同,例如:
# 取得数组元素的个数
length=${#array_name[@]}
# 或者
length=${#array_name[*]}
# 取得数组单个元素的长度
lengthn=${#array_name[n]}
read name
# 接收键盘输入的内容并赋值给name
echo -e "OK! \n" # -e 开启转义
echo "It is a test"
#!/bin/sh
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"
echo `date`
# 显示命令执行结果
test
流程控制
# 作为逻辑运算符左右都有空格
# 数值
-eq 等于则为真
-ne 不等于则为真
-gt 大于则为真
-ge 大于等于则为真
-lt 小于则为真
-le 小于等于则为真
#!/bin/bash
test1=1
test2=1
# [] 执行基本的算术运算
# -eq可以换为=,左右同样有空格
if test $[test1] -eq $[test2]
then
echo "两个数相等"
else
echo "两个数不相等"
fi
# 字符串
= 等于则为真
!= 不相等则为真
-z 字符串 字符串的长度为零则为真
-n 字符串 字符串的长度不为零则为真
# 文件
-e 文件名 如果文件存在则为真
-r 文件名 如果文件存在且可读则为真
-w 文件名 如果文件存在且可写则为真
-x 文件名 如果文件存在且可执行则为真
-s 文件名 如果文件存在且至少有一个字符则为真
-d 文件名 如果文件存在且为目录则为真
-f 文件名 如果文件存在且为普通文件则为真
-c 文件名 如果文件存在且为字符型特殊文件则为真
-b 文件名 如果文件存在且为块特殊文件则为真
cd /bin
if test -e ./bash
then
echo '文件已存在!'
else
echo '文件不存在!'
fi
与( -a )、或( -o )、非( ! )
其优先级为:"!"最高,"-a"次之,"-o"最低。
cd /bin
if test -e ./notFile -o -e ./bash
then
echo '至少有一个文件存在!'
else
echo '两个文件都不存在'
fi
a=10
b=20
if [ $a == $b ]
then
echo "a 等于 b"
elif [ $a -gt $b ]
then
echo "a 大于 b"
elif [ $a -lt $b ]
then
echo "a 小于 b"
else
echo "没有符合的条件"
fi