本文是笔者研究生期间在阅读《Linux命令行与shell脚本编程大全》之后总结出来的一些重点知识的记录,在此重新整理输出。以便在给上个帖子 涉及到的相关知识点进行一下讲解,帮助自己复习
shell脚本的首行规范化应该是
#!/bin/bash
# function description
其中第一行必须如此,#后面的惊叹号会告诉shell用哪个shell来运行脚本,第二行是本脚本的功能说明。
● 当进行变量赋值后,要引用这个变量,则要用 $符号,而如果要某个命令的结果进行赋值则要用 $()
● 变量名称区分大小写,赋值的=前后不能有空格
days=10
guest="Katie"
echo "$guest checked in $days days aGo"
#!/bin/bash
testing=$(date)
echo "The date and time are: " $testing
● 一维数组,即向量,空格隔开,下标读取, */@读取所有元素
array_name=(value0 value2 value3)
# 数组使用
my_array=(A B C D)
echo "数组的元素为: ${my_array[*]}
echo "数组的元素为: ${my_array[@]}
echo "数组的元素个数为: ${#my_array[@]}"
● 重定向输入输出
#### 输出重定向
> # 保存
>> # 追加保存,不覆盖
#### 输入重定向,也就是将 file 作为参数 发给这个命令,比如下面,
# wc命令可以对对数据中的文本进行计数
wc < file1
● 管道 | :一个命令的输出作为另一个命令的输入
rpm -qa | sort > rpm.list
● 其他
# 命令替换
for i in `seq 1 10`
`expr 2+2`
#### echo read printf test命令
echo "It is a test"
echo "\"It is a test\"" # 转义
#!/bin/sh
read name # 从标准输入中读取参数赋给name变量
echo "$name It is a test"
echo -e "OK! \n" # -e 开启转义
echo "It is a dog" > myfile
printf "%-10s %-8s %-4s.2f\n" 郭靖 男 66.1234
2.1 for循环
## for循环
for loop in `seq 1 5` # 命令替换
do
echo "The value is: $loop"
done
#或者一条命令
for var in item1 item2 item3;do echo $var;done
# while循环
while condition
do
command
done
#### 示例小脚本
for i in (seq 1 22) X Y M;do cat chr{i}.fa >>hg38.fa;done # () 都是做命令替换的 {}是做变量替换的
for i in `seq 1 12`;do cut -f 6,9 w${i}_transcript.gtfs >w${i}.txt;done
for i in `seq 1 12`;do sort w${i}.txt >w${i}.gtf;done
for i in `cat list.txt`;do grep -w $i whole.gff3|awk '$3=="gene"' >>result.txt;done
2.2 if-else类
### shell流程控制
# 判断 只有第一个 command正常成功执行,返回退出码0的语句中的then的 command2才会被执行
if command1
then
command2
command3
fi
# 另一种形式
if command; then
commands
fi
# if-then-else 语句
if command
then
command2
else
command3
fi
# 嵌套if
if grep $testuser /etc/passwd
then
echo "The user $testuser exists on this system."
else
echo "The user $testuser does not exist on this system."
if ls -d /home/$testuser/
then
echo "However, $testuser has a directory."
fi
fi
# elif
if command1
then
commands
elif command2
then
more commands
fi
2.3 if中对条件的判断
除了 正常执行某个命令之外,还可以用 test 和 [ C++ondation ] 两种形式来判断,判断条件有数值比较 字符串比较 文件比较
### 更一般性的判断语句,而不只是以command是否成功执行为判断依据
# test condition # 检查某个表达式是否成立;test可以判断 数值比较 字符串比较 文件比较
if test conditon #
then
fi
if [ condition ] # 另一种方式
then
fi
● 数值比较 左边 对于右边的关系
-eq 相等
-ge 大于或等于
-gt 大于
-le 小于或等于
-lt 小于
-ne 不等于
● 字符串比较
-n 长度非0
-z 长度为0
= 相同
!= 不同
> 大
< 小
test比较数值用 文本代码(eq,lt)来比较,而比较字符串用 标准数学符号(>=)来比较,比较字符串使用标准的ASCII码顺序,数值< 大写字母< 小写字母
# 节选自 《linux命令行与shell脚本大全》第三版 P245
# -n -z 检查一个变量是否含有数据 可以用来判断 自己写的shell程序的命令行参数是否完备
cat test10.sh
$ cat test10.sh
#!/bin/bash
# testing string length
val1=testing
val2=''
#
if [ -n $val1 ]
then
echo "The string '$val1' is not empty"
else
echo "The string '$val1' is empty"
fi
#
if [ -z $val2 ]
then
echo "The string '$val2' is empty"
else
echo "The string '$val2' is not empty"
fi
#
if [ -z $val3 ]
then
echo "The string '$val3' is empty"
else
echo "The string '$val3' is not empty"
fi
$
$ ./test10.sh
The string 'testing' is not empty
The string '' is empty
The string '' is empty
$
● 文件比较
-d file 检查file是否存在并是一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
更多参见 P246 12.4.3
● 复合条件测试
[ condtion1 ] && [ condtion2 ]
[ condtion1 ] || [ condtion2 ]
2.4 Case
# case
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
例子
days=10
guest="Katie"
echo "$guest checked in $days days aGo"
#!/bin/bash
testing=$(date)
echo "The date and time are: " $testing
0
在命令行获取参数,默认的 执行脚本被认为是 $0,那么 后面每隔一个空格 的参数就依次是 $1, $2 ......。
days=10
guest="Katie"
echo "$guest checked in $days days aGo"
#!/bin/bash
testing=$(date)
echo "The date and time are: " $testing
1
days=10
guest="Katie"
echo "$guest checked in $days days aGo"
#!/bin/bash
testing=$(date)
echo "The date and time are: " $testing
2
BTW:
阅读完上面的内容,基本上就能看懂上期我发的 简单可靠的SpringBoot Jar包启动脚本了。