安全重启:
按住alt+<PrtSc>
,然后依次按下reisub即可安全重启。
¶ 语法
¶ 条件判断
man bash
,然后搜索CONDITIONAL EXPRESSIONS
,可以看到完整的列表。
¶ if elif else
参考:https://blog.csdn.net/u014783674/article/details/24474001
#!/bin/bash
if [ condition1 ]; then
# Do something
elif [ condition2 ]; then
# Do something
else
# Do something
fi
¶ 判断某环境变量是否存在
参考:https://blog.csdn.net/blade2001/article/details/7243143?utm_source=blogxgwz3
上面的文章好像写反了
例子:判断环境变量DISPLAY是否存在(若不存在说明没有提供显示设备)
if [ $DISPLAY ]; then
# DISPLAY存在
else
# DISPLAY不存在
fi
或者
if [ ! $DISPLAY ]; then
表示如果$DISPLAY不存在
¶ 字符串
功能 | 例子 |
---|---|
为空 | [ -z "$1" ] 或者 [ ! "$1" ] |
非空 | [ -n "$1" ] 或者 [ "$1" ] |
相等 | [ "$1" == "$2" ] |
不相等 | [ "$1" != "$2" ] |
所以如果判断目录是否非空,可以这样:if [ "$(ls -A xxx)" ]
参考:https://www.cyberciti.biz/faq/linux-unix-shell-check-if-directory-empty/
¶ 整数的大小判断
参考:
https://blog.csdn.net/shang_feng_wei/article/details/90378017
https://www.jb51.net/article/56553.htm
https://blog.csdn.net/HappyRocking/article/details/90609554#1_70
代码 | 含义 | 例子 |
---|---|---|
-eq | = | [ $1 -eq 2 ] |
-ne | != | [ $1 -ne 2 ] |
-le | <= | [ $1 -le 2 ] |
-lt | < | [ $1 -lt 2 ] |
-ge | >= | [ $1 -ge 2 ] |
-gt | > | [ $1 -gt 2 ] |
-a | && | [ $1 -gt 0 -a $1 -lt 10 ] |
-o | || | [ $1 -lt 0 -o $1 -gt 10 ] |
其中-eq
和-ne
可以分别用=
和!=
替换。
如果想像C语言那样进行条件判断,可以使用[[]]
。
例如下面这两句都表示如果参数的个数等于0或者大于2
if [[ $# = 0 || $# > 2 ]]; then
if [ $# = 0 -o $# -gt 2 ]; then
¶ 浮点数的大小判断
if awk "BEGIN {exit !(1.234 >= .233)}"; then
echo "yes"
fi
来源:https://stackoverflow.com/a/45591665/13688160
¶ 判断文件类型
来源:https://jingyan.baidu.com/article/95c9d20d5ac536ec4e7561ad.html
#!/bin/bash
if [ -z $1 ]; then #如果没有输入参数,也就是第一个参数的字符串长度为0
: #空语句
else
if [ -e $1 ]; then #如果文件存在的话
if [ -f $1 ]; then #如果文件是个普通文件?
echo $1" is a text file."
elif [ -d $1 ]; then #如果文件是个目录文件?
echo $1" is a directory."
elif [ -c $1 ]; then #如果文件是个字符设备?
echo $1" is a char device."
elif [ -b $1 ]; then #如果文件是个块设备?
echo $1" is a block device."
else #否则
echo $1" is unknow file."
fi
fi
另外,-s
表示文件存在且不为空。来源:https://stackoverflow.com/questions/9964823/how-to-check-if-a-file-is-empty-in-bash
¶ 判断文件权限
代码 | 含义 |
---|---|
-r | 存在且可读 |
-w | 存在且可写 |
-x | 存在且可执行 |
参考:https://stackoverflow.com/questions/10319652/check-if-a-file-is-executable
¶ 函数
参考:https://www.runoob.com/linux/linux-shell-func.html
¶ 定义
function FunctionName {
do_some_thing_here
return Interger
}
其中function
可以省略,也可以不return。
参数用法与脚本类似。($#
表个数,$1, $9, ${10}
表具体参数)
¶ 使用
FunctionName par1 par2 par3
¶ 循环
¶ while
参考:https://wiki.jikexueyuan.com/project/shell-tutorial/shell-while-loop.html
while Command
do
Statement(s) to be executed if Command is true
done
或者
while [ Condition ]; do
Statement(s) to be executed if Condition is true
done
也可以对命令返回值取反,比如
while ! Command; do
Statement(s) to be executed if Command is false
done
¶ for
参考:https://blog.csdn.net/astraylinux/article/details/7016212
for ((i=0; i<10; ++i))
do
echo $i
done
注意是双括号。
还有其他用法。看参考链接
¶ 正则表达式
if [[ 字符串 =~ 模式 ]]; then
echo 字符串中含有模式
fi
比如最简单的:
if [[ 2333test233222 =~ test233 ]]; then
echo yes;
fi
会打印出yes
。
如果要判断是否不包含:
if ! [[ 2333test233222 =~ test233 ]]; then
echo yes;
fi
或操作:
if [[ 2333test233222 =~ abc || 2333test233222 =~ test233 ]]; then
echo yes;
fi
# yes
与操作:
if [[ 2333test233222 =~ abc && 2333test233222 =~ test233 ]]; then
echo yes;
fi
¶ 重定向
# 将`stdout`重定向到`stdout.txt`
Command > stdout.txt
# 将`stderr`重定向到`stderr.txt`
Command 2> stderr.txt
# 将stderr重定向到stdout
Command 2>&1
# 将stderr和stdout都重定向到一个文件
# 参考:<https://blog.csdn.net/u011630575/article/details/52151995>
Command > shell.log 2>&1
Command &> shell.log
# 将`stdin`重定向到`stdin.txt`
Command < stdin.txt
# 将Command1的stdout输入到Command2的stdin
Command1 | Command2 # 例如 echo 'whoami' | bash,可以让bash执行whoami。
¶ 将多行字面量作为stdin
Command <<标记
第一行
第二行
...
标记
例如:
bash <<EOF
whoami
echo 2333
EOF
¶ 数组
来源:https://www.yiibai.com/bash/bash-array.html
¶ 定义
ARRAY_NAME=(element_1st element_2nd element_Nth)
¶ 访问某个下标的元素
下标从0开始。
echo ${ARRAY_NAME[2]}
¶ 访问所有元素
# 经实测,bash是每个元素一个不带引号的结果,zsh是每个元素一个带引号的结果
echo ${ARRAY_NAME[@]}
echo ${ARRAY_NAME[*]}
# 每个元素一个带引号的结果
echo "${ARRAY_NAME[@]}"
# 所有元素组成一个结果
echo "${ARRAY_NAME[*]}"
例如:
a=(1 "2 3")
# bash 相当于 echo 1 2 3
# zsh 相当于 echo 1 "2 3"
echo ${a[@]}
echo ${a[*]}
# 相当于 echo 1 "2 3"
echo "${a[@]}"
# 相当于 echo "1 2 3"
echo "${a[*]}"
¶ 访问slice
# 从第m个开始一直到末尾
${ARRAY_NAME[@]:m}
# 从第m个开始取n个。下标从0开始。
${ARRAY_NAME[@]:m:n}
保存为新的数组:
SLICED_ARRAY=(${ARRAY_NAME[@]:m:n})
参考:https://stackoverflow.com/questions/1335815/how-to-slice-an-array-in-bash
¶ 长度
```shell
a=(1 2 3 4)
echo $