shell_2
shell特殊参数变量
space_var.sh
echo '$0:'
echo $0
echo '$1,$2,$3:'
echo $1 $2 $3
echo '$#:'
echo $#
echo '$*:'
echo $*
echo '$@:'
echo $@
[root@VM-4-12-centos shellfile]# bash space_var.sh a b c
$0:返回shell脚本文件ming
space_var.sh
$1,$2,$3:获取shell脚本的参数
a b c
$#:shell脚本后面参数的总个数
3
$*:获取shell脚本所有参数,加引号作为一个整体
a b c
$@:获取shell脚本所有参数,加引号后每一个参数为一个独立的字符串,可用for
a b c
shell特殊状态变量
### $? 上一次命令状态返回值,0正确,非0失败
### $$ 当前shell脚本的进程号
### $! 上一次后台进程的pid
nohup xxx & 1 > /dev/null 让命令后台执行
### $_ 之前执行命令的最后一个参数
bash一些基础的内置命令
### bash
-n 不换行输出
-e 解析字符串中的特殊字符
[root@VM-4-12-centos shellfile]# echo hello world
hello world
[root@VM-4-12-centos shellfile]# echo -n hello world
hello world[root@VM-4-12-centos shellfile]#
[root@VM-4-12-centos shellfile]# echo -e "hello\nworld"
hello
world
[root@VM-4-12-centos shellfile]# printf "你好\t世界"
你好 世界[root@VM-4-12-centos shellfile]#
### eval
执行多个命令
[root@VM-4-12-centos shellfile]# eval ls;cd /tmp
a.sh b.sh space_var.sh
[root@VM-4-12-centos tmp]#
### exec
不创建子进程,执行后续命令,且执行完毕后,自动exit
expr命令
ARG1 | ARG2 若ARG1 的值不为0 或者为空,则返回ARG1,否则返回ARG2
ARG1 & ARG2 若两边的值都不为0 或为空,则返回ARG1,否则返回 0
ARG1 < ARG2 ARG1 小于ARG2
ARG1 <= ARG2 ARG1 小于或等于ARG2
ARG1 = ARG2 ARG1 等于ARG2
ARG1 != ARG2 ARG1 不等于ARG2
ARG1 >= ARG2 ARG1 大于或等于ARG2
ARG1 > ARG2 ARG1 大于ARG2
ARG1 + ARG2 计算 ARG1 与ARG2 相加之和
ARG1 - ARG2 计算 ARG1 与ARG2 相减之差
ARG1 * ARG2 计算 ARG1 与ARG2 相乘之积
ARG1 / ARG2 计算 ARG1 与ARG2 相除之商
ARG1 % ARG2 计算 ARG1 与ARG2 相除之余数
字符串 : 表达式 定位字符串中匹配表达式的模式
match 字符串 表达式 等于"字符串 :表达式"
substr 字符串 偏移量 长度 替换字符串的子串,偏移的数值从 1 起计
index 字符串 字符 在字符串中发现字符的地方建立下标,或者标0
length 字符串 字符串的长度
[root@VM-4-12-centos ~]# expr match aaa "a"
1
[root@VM-4-12-centos ~]# expr match aaa "aa"
2
[root@VM-4-12-centos ~]# expr match aaa "aaa"
3
[root@VM-4-12-centos ~]# expr match aaa "aaaa"
0
[root@VM-4-12-centos ~]# expr substr abcd 1 2
ab
[root@VM-4-12-centos ~]# expr index abc a
1
[root@VM-4-12-centos ~]# expr length abc
3
[root@VM-4-12-centos ~]# expr 10 + 10
20
变量字串
[root@VM-4-12-centos ~]# name="hello world"
[root@VM-4-12-centos ~]# echo ${name}
hello world
[root@VM-4-12-centos ~]# echo ${#name}
11
[root@VM-4-12-centos ~]# echo ${name:1}
ello world
[root@VM-4-12-centos ~]# echo ${name:0:4}
hell
[root@VM-4-12-centos ~]# echo ${name#hello}
world
[root@VM-4-12-centos ~]# echo ${name/hello/你好}
你好 world
计算变量长度
[root@VM-4-12-centos ~]# name="hello world"
[root@VM-4-12-centos ~]# echo $name
hello world
[root@VM-4-12-centos ~]# echo $name | wc -l
1
[root@VM-4-12-centos ~]# echo $name | wc -L
11
[root@VM-4-12-centos ~]# expr length $name
expr: 语法错误
[root@VM-4-12-centos ~]# expr length "$name"
11
[root@VM-4-12-centos ~]# echo "$name" | awk '{print length($0)}'
11
[root@VM-4-12-centos ~]# echo ${#name}
11
统计命令时长
前置知识
[root@VM-4-12-centos ~]# for num in {1..100};do echo $num;done
1
2
3
4
5
6
略
[root@VM-4-12-centos ~]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@VM-4-12-centos ~]# seq -s ":" 10
1:2:3:4:5:6:7:8:9:10
[root@VM-4-12-centos ~]# for n in {1..3};do str1=`seq -s ":" 10`;echo $str1;done
1:2:3:4:5:6:7:8:9:10
1:2:3:4:5:6:7:8:9:10
1:2:3:4:5:6:7:8:9:10
[root@VM-4-12-centos ~]# time
real 0m0.000s
user 0m0.000s
sys 0m0.000s
[root@VM-4-12-centos ~]# time for n in {1..10000};do char=`seq -s "xxx" 100`;echo ${#char} &>/dev/null;done
real 0m11.640s #实际运行时间
user 0m4.208s #用户态执行时间
sys 0m7.637s #内核态执行时间
[root@VM-4-12-centos ~]# time for n in {1..10000};do char=`seq -s "xxx" 100`;echo ${char} | wc -L &>/dev/null;done
real 0m25.863s
user 0m12.939s
sys 0m21.458s
[root@VM-4-12-centos ~]# time for n in {1..10000};do char=`seq -s "xxx" 100`;expr length "${char}" &>/dev/null;done
real 0m22.659s
user 0m8.569s
sys 0m15.122s
[root@VM-4-12-centos ~]# time for n in {1..10000};do char=`seq -s "xxx" 100`;echo ${char} | awk '{print length($0)}' &>/dev/null;done
real 0m28.381s
user 0m13.768s
sys 0m25.434s
字符串截取
[root@VM-4-12-centos ~]# name=abcABCabcABC
[root@VM-4-12-centos ~]# echo ${name#abc}
ABCabcABC
# 从前截取第一个
[root@VM-4-12-centos ~]# echo ${name#a*c}
ABCabcABC
# 从前截取全部
[root@VM-4-12-centos ~]# echo ${name##a*c}
ABC
# 从后截取第一个
[root@VM-4-12-centos ~]# echo ${name%A*C}
abcABCabc
# 从后截取全部
[root@VM-4-12-centos ~]# echo ${name%%A*C}
abc
[root@VM-4-12-centos ~]# echo ${name/abc/123}
123ABCabcABC
[root@VM-4-12-centos ~]# echo ${name//abc/123}
123ABC123ABC
批量改文件名
[root@VM-4-12-centos sub_str]# touch student_{1..5}_finished.png
[root@VM-4-12-centos sub_str]# touch student_{1..5}_finished.jpg
[root@VM-4-12-centos sub_str]# ls
student_1_finished.jpg student_2_finished.jpg student_3_finished.jpg student_4_finished.jpg student_5_finished.jpg
student_1_finished.png student_2_finished.png student_3_finished.png student_4_finished.png student_5_finished.png
[root@VM-4-12-centos sub_str]# for file_name in `ls *.jpg`;do mv $file_name `echo ${file_name//_finished/}`;done
[root@VM-4-12-centos sub_str]# for file_name in `ls *.png`;do mv $file_name `echo ${file_name//_finished/}`;done
[root@VM-4-12-centos sub_str]# ls
student_1.jpg student_1.png student_2.jpg student_2.png student_3.jpg student_3.png student_4.jpg student_4.png student_5.jpg student_5.png
shell扩展变量
# :-变量为空,返回后面字符串信息
[root@VM-4-12-centos sub_str]# echo $age
[root@VM-4-12-centos sub_str]# result=${age:-20}
[root@VM-4-12-centos sub_str]# echo $result
20
# :=变量为空,赋值前面两个
[root@VM-4-12-centos sub_str]# echo $hobby
[root@VM-4-12-centos sub_str]# key=${hobby:=soccer}
[root@VM-4-12-centos sub_str]# echo $key
soccer
[root@VM-4-12-centos sub_str]# echo $hobby
soccer
# :?
[root@VM-4-12-centos sub_str]# echo ${new_name:?}
-bash: new_name: 参数为空或未设置
[root@VM-4-12-centos sub_str]# echo ${new_name:?该变量值为空}
-bash: new_name: 该变量值为空
[root@VM-4-12-centos sub_str]# new_name=zs
[root@VM-4-12-centos sub_str]# echo ${new_name:?该变量值为空}
zs
# :+ 与:-相反
[root@VM-4-12-centos sub_str]# echo $test
[root@VM-4-12-centos sub_str]# echo ${test:+111}
[root@VM-4-12-centos sub_str]# test=222
[root@VM-4-12-centos sub_str]# echo ${test:+111}
111
应用
# 数据备份,删除过期数据的脚本
find ${dir_path:=/data/mysql_back_data/} -name '*.tar.gz' -type f -mtime +7 | xargs rm -rf

浙公网安备 33010602011771号