linux shell 脚本攻略

sed 流编辑器

已匹配字符串标志&

sed 's/[^\n]/&\n/g'
sed '/^$/d' 去除空行
uniq -c 统计每行重复次数
tr -d ' \n' 删除空格和换行符

bash匹配

%string 从右往左匹配,截取左边的字符串
%%string贪婪匹配

 DannyYo@DannyYo-pc  /myshell/test  file_jpg="namememe.jpg"
 DannyYo@DannyYo-pc  /myshell/test  name=${file_jpg%%me*}  
 DannyYo@DannyYo-pc  /myshell/test  echo $name             
na
 DannyYo@DannyYo-pc  /myshell/test  name=${file_jpg%me*}  
 DannyYo@DannyYo-pc  /myshell/test  echo $name          
nameme

同理
string# 从左往右匹配,截取右边
string## 贪婪

临时文件名命名

temp_file=$(tempfile) 
temp_file="/tmp/file-$RANDOM"  #返回随机数
temp_file="/tmp/var.$$"  #.$$返回当前脚本进程ID 

批量重命名

count=1
for img in *.jpg
do 
new=image-$count.${img##*.}
mv "$img" "$new" 2> /dev/null

if [ $? -eq 0 ]; then
echo "Renaming $img to $new"
let count++
fi
done

rename 's/ /_/g' * 把文件名中的空格替换成'_'
rename 'y/a-z/A-Z/' * 把文件名转换成大写

你可能感兴趣的:(linux shell 脚本攻略)