Shell Encyclopedia
Read each line of the file
while read line
do
echo $line
done < a.txt
Does the file exist
if [ ! -d "/data/" ];then
mkdir /data
else
echo "文件夹已经存在"
fi
illustrate
-e 判断对象是否存在
-d 判断对象是否存在,并且为目录
-f 判断对象是否存在,并且为常规文件
-L 判断对象是否存在,并且为符号链接
-h 判断对象是否存在,并且为软链接
-s 判断对象是否存在,并且长度不为0
-r 判断对象是否存在,并且可读
-w 判断对象是否存在,并且可写
-x 判断对象是否存在,并且可执行
-O 判断对象是否存在,并且属于当前用户
-G 判断对象是否存在,并且属于当前用户组
-nt 判断file1是否比file2新 [ "/data/file1" -nt "/data/file2" ]
-ot 判断file1是否比file2旧 [ "/data/file1" -ot "/data/file2" ]
Array definition and loop value
arr=(a b c)
echo ${arr[0]} #获取第一个元素的值
echo ${arr[-1]} #获取最后一个元素的值
# 获取所有元素的值
echo ${arr[*]}
echo ${arr[@]}
# 统计数组的长度
echo ${
#arr[*]}
# 打印数组的下标值
echo ${
!arr[@]}
# 循环 方式一 直接取值
for i in ${arr[@]}
do
echo $i
done
# 循环 方式二 下标取值
for i in ${
!arr[@]}
do
echo ${arr[i]}
done
# 循环 方式三
for((i=0;i<${#arr[@]};i++))
do
echo ${arra[i]}
done
variable loop
for i in $(seq 1 $1)
do
echo $i
done
for (( i = 1; i < $line; i++ ))
do
echo $i
done
Flow control statement: case
case $1 in
"start")
op start $2
;;
"stop")
op stop $2
;;
"status")
op status $2
;;
*)
echo "Usage: service $2 start|stop|status"
;;
esac
Judging values are equal/greater/less than
# 整数比较
-eq 等于,如:if ["$a" -eq "$b" ]
-ne 不等于,如:if ["$a" -ne "$b" ]
-gt 大于,如:if ["$a" -gt "$b" ]
-ge 大于等于,如:if ["$a" -ge "$b" ]
-lt 小于,如:if ["$a" -lt "$b" ]
-le 小于等于,如:if ["$a" -le "$b" ]
< 小于(需要双括号),如:(("$a" < "$b"))
<= 小于等于(需要双括号),如:(("$a" <= "$b"))
> 大于(需要双括号),如:(("$a" > "$b"))
>= 大于等于(需要双括号),如:(("$a" >= "$b"))
Determine string equality
if [ "$test"x = "test"x ]; then
# = 等于,如:if [ "$a" = "$b" ]
# == 等于,如:if [ "$a" == "$b" ],与 = 等价
There are a few key points here:
- use a single equal sign
- Note that there is a space on each side of the equal sign: this is a requirement of the unix shell
- Notice the x at the end of "$test"x, which is deliberately arranged, because when $test is empty, the above expression becomes x = testx, which is obviously not equal. And if there is no x, the expression will report an error: [: =: unary operator expected
awk sum, average, maximum, minimum
# 科学计数法打印
bin/hdfs dfs -du /warehouse/hive/ | awk '{sum+=$1} END {print "Sum = ", sum/1024/1024}'
# 非科学计数法打印
bin/hdfs dfs -du /warehouse/hive/ | awk '{sum+=$1} END {printf("%d\n", sum/1024/1024)}'
1、求和
awk '{sum+=$1} END {print "Sum = ", sum}' number.txt
2、求平均
awk '{sum+=$1} END {print "Average = ", sum/NR}' number.txt
3、求最大值
awk 'BEGIN {max = 0} {if ($+0 > max+0) max=$1} END {print "Max=",max}' number.txt
4、求最小值(min的初始值设置一个超大数即可)
awk 'BEGIN {min = 1999999} {if ($1<min) min=$1 fi} END {print "Min=", min}' number.txt
5. 输出排序最大值所在行内容
如下,以第一列排序
cat num.txt
858 mail
1858 nginx
8502 tomcat
1145 zabbix
3457 mongodb
1356 redis
974 Mysql
记录最大值的时候,同时记录一下当前行,后面输出这个变量即可 此处的的content可随意定义
awk 'BEGIN {max = 0} {if ($1+0 > max+0) {max=$1 ;content=$0} } END {print content}' num.txt
sed usage
# 取 1到3 行的内容 重定向到 b.txt
sed -n "1,3p" a.txt > b.txt
# 替换
sed -i "s/abc/def/g" ./a.txt
If we use the sed command, if there are special characters, an error may be reported. The example is as follows:
# /不可以作为界定符,因为会与里面的内容冲突
sed -i 's/flink//etc/g'
# 井号#不可以作为界定符,因为会与里面的内容冲突
sed -i 's#jdbc://127.0.0.1&password=1#aa$username=root#g'
# @不可以作为界定符,因为会与里面的内容冲突
sed -i 's@jdbc://127.0.0.1&password=1@aa$username=root@g'
So how to solve it?
We have to look inside to select a specific delimiter, for example:
If it contains "/", you can choose "#" or "@" as the delimiter;
if it contains "#", you can choose "@" or "/" as the delimiter;
if it contains "@", you can choose "/" or "#" as a delimiter.
So what if you have all of the above? The escape symbol "\" can only be used in the replaced text, such as:
sed -i 's/oracle/\/etc\/oracle/g'
expr
expr supports ordinary arithmetic operations, and the precedence of arithmetic expressions is lower than that of string expressions and higher than that of logical relational expressions.
- + - Addition and subtraction. The parameters at both ends will be converted to integers, and an error will be reported if the conversion fails;
- * / % Multiplication, division, and modulo operations. The parameters at both ends will be converted to integers, and an error will be reported if the conversion fails;
- () can be used to indicate priority, but needs to be escaped with a backslash.
a=3
b=4
echo `expr $a + $b` #输出7
echo `expr $a - $b` #输出-1
echo `expr $a \* $b` #输出12,*需要转义
echo `expr $a / $b` #输出0,整除
echo `expr $a % $b` #输出3
echo `expr \($a + 1\)\*\($b+1\)` #输出20,值为(a+1)*(b+1)
bc calculator
echo '6.5/2.7' | bc
2
echo 'scale=5;6.5/2.7' | bc #其中scale是控制小数点位数
2.40740
echo "5+3" | bc
8
echo "(2+6)*3" | bc
24
#关系运算符
||, &&, !, =, ==
#基本数学运算
+, -, *, /, %, ^,
#自增,自减
++, --
#逻辑运算符
<, >, <=, >=, !=
length() #用于求表达式的结果长度
length(300)
3
scale() #用于获取表达式小数点后位数
scale(3.14)
2
sqrt() #求平方根
scale=6;sqrt(21)
4.582575
#如果使用了bc -l,可以将预置的数学运算导入
s(x) #sin函数,x为弧度
s(3.14)
.00159265291648695254
c(x) #cos函数
c(0)
1.00000000000000000000
a(x) #arctang函数
a(sqrt(2)/2)
0.61547970867038734106
l(x) #自然对数
l(10)
2.30258509299404568401
e(x) #自然数为底的指数函数
e(2)
7.38905609893065022723
j(n,x) #Basel函数,n阶
j(1,3)
0.33905895852593645892