1.变量基础知识
${var}变量var的值,与$var相同
${var-DEFAULT}如果var没有被声明,那么就以$DEFAULT作为其值*
${var:-DEFAULT}如果var没有被声明,或者其值为空,那么就以$DEFAULT作为其值*
${var=DEFAULT}如果var没有被声明,那么就以$DEFAULT作为其值*
${var:=DEFAULT}如果var没有被声明,或者其值为空,那么就以$DEFAULT作为其值*
${var+OTHER}如果var声明了,那么其值就是$OTHER,否则就为null字符串
${var:+OTHER}如果var被设置了,那么其值就是$OTHER,否则就为null字符串
${var?ERR_MSG}如果var没被声明,那么就打印$ERR_MSG*
${var:?ERR_MSG}如果var没被设置,那么就打印$ERR_MSG*
${!varprefix*}匹配之前所有以varprefix开头进行声明的变量
${!varprefix@}匹配之前所有以varprefix开头进行声明的变量
#!/bin/bash
#
#
#定义path变量
path=/data/back/
#搜索path路径下包含tar.gz的文件并删除
find$path-name"*.tar.gz"-typef|xagrsrm-f
#优化之后
find${path:=/data/back/}-name"*.tar.gz"-typef|xagrsrm-f
#此方式path变量没有赋值的情况,有赋值使用赋值,没有赋值使用=号后面的值
#此方式主要针对删除操作
2.变量子串知识
${#string}$string的长度
${string:position}在$string中,从位置$position开始提取子串
${string:position:length}在$string中,从位置$position开始提取长度为$length的子串
${string#substring}从变量$string的开头,删除最短匹配$substring的子串
${string##substring}从变量$string的开头,删除最长匹配$substring的子串
${string%substring}从变量$string的结尾,删除最短匹配$substring的子串
${string%%substring}从变量$string的结尾,删除最长匹配$substring的子串
${string/substring/replacement}使用$replacement,来代替第一个匹配的$substring
${string//substring/replacement}使用$replacement,代替所有匹配的$substring
${string/#substring/replacement}如果$string的前缀匹配$substring,那么就用$replacement来代替匹配到的$substring
${string/%substring/replacement}如果$string的后缀匹配$substring,那么就用$replacement来代替匹配到的$substring
批量修改文件名
-rw-r--r--1rootroot0Dec100:10eddy_10299_1_finished.jpg
-rw-r--r--1rootroot0Dec100:10eddy_10299_2_finished.jpg
-rw-r--r--1rootroot0Dec100:10eddy_10299_3_finished.jpg
-rw-r--r--1rootroot0Dec100:10eddy_10299_4_finished.jpg
#!/bin/bash
#author:eddydate:-11-30
#renamefile
#v1.0
foriin`lstemp/*.jpg`
do
mv$i`echo${i%_finished*}.jpg`
done
以上方式偏于复杂及不好理解
使用linux自带的rename命令进行批量重命名
-rw-r--r--1rootroot0Dec100:33eddy_10.txt
-rw-r--r--1rootroot0Dec100:33eddy_1.txt
-rw-r--r--1rootroot0Dec100:33eddy_2.txt
-rw-r--r--1rootroot0Dec100:33eddy_3.txt
-rw-r--r--1rootroot0Dec100:33eddy_4.txt
-rw-r--r--1rootroot0Dec100:33eddy_5.txt
-rw-r--r--1rootroot0Dec100:33eddy_6.txt
-rw-r--r--1rootroot0Dec100:33eddy_7.txt
-rw-r--r--1rootroot0Dec100:33eddy_8.txt
-rw-r--r--1rootroot0Dec100:33eddy_9.txt
[root@eddytemp]#rename"eddy""yys"eddy_*
-rw-r--r--1rootroot0Dec100:33yys_10.txt
-rw-r--r--1rootroot0Dec100:33yys_1.txt
-rw-r--r--1rootroot0Dec100:33yys_2.txt
-rw-r--r--1rootroot0Dec100:33yys_3.txt
-rw-r--r--1rootroot0Dec100:33yys_4.txt
-rw-r--r--1rootroot0Dec100:33yys_5.txt
-rw-r--r--1rootroot0Dec100:33yys_6.txt
-rw-r--r--1rootroot0Dec100:33yys_7.txt
-rw-r--r--1rootroot0Dec100:33yys_8.txt
-rw-r--r--1rootroot0Dec100:33yys_9.txt
批量修改扩展名
依然可以用rename实现
[root@eddytemp]#rename"txt""jpg"yys_*
-rw-r--r--1rootroot0Dec100:33yys_10.jpg
-rw-r--r--1rootroot0Dec100:33yys_1.jpg
-rw-r--r--1rootroot0Dec100:33yys_2.jpg
-rw-r--r--1rootroot0Dec100:33yys_3.jpg
-rw-r--r--1rootroot0Dec100:33yys_4.jpg
-rw-r--r--1rootroot0Dec100:33yys_5.jpg
-rw-r--r--1rootroot0Dec100:33yys_6.jpg
-rw-r--r--1rootroot0Dec100:33yys_7.jpg
-rw-r--r--1rootroot0Dec100:33yys_8.jpg
-rw-r--r--1rootroot0Dec100:33yys_9.jpg