Linux简单的shell脚本

批量修改文件名称的shell脚本

#! /bin/bash

# imagine the folder contains ten thousand images
# named such as leon001362.png, leon104378.png

folder="your folder"
filelist=$(ls $folder)
format='.png'

for filename in $filelist
do
# pick 001362, 104378
num=${filename:4:6}

# use regular regression to make a integer
# 001362 to 1362 and 104378 unchanged
num=$(expr $num | sed 's/^0*//')

# rename
mv $filename ${num}${format}
done

这是跟一个项目有关的脚本,描述起来比较麻烦,直接上代码

#! /bin/bash

# You have a folder, which contains many images, such as, 1.png, 2.png ... 10000.png.
# You have a txt, which labels the class of a image, such as,
# "100 105 1" means 100.png, 101.png, 102.png, 103.png, 104.png and 105.png belong to class 1.
# "107 108 0" means 107.png and 108.png belong to class 0.
# But the txt has some errors,  it contains "100 105 1" and "105 107 0"
# that means 105.png belongs to class 1 and 0, we want to change it to class 0. 

total=0
array[0]=0

while read line
do 
# seperate every line into a array, "100 105 1" into a array, 
# array[0]=100, array[1]=105, array[2]=1
OLD_IFS="$IFS"
IFS=" "
  arr=($line)
  IFS="$OLD_IFS"

  for(( i=${arr[0]}; i<=${arr[1]}; i=i+1 ))
  do
array[$total]=$i
let total+=1
file="the image folder""$i"".png"
# if i.png exists in the image folder
if [ ! -f "$file" ]; then
echo "$i"".png"
fi
done
done < "the txt folder"
echo "total:"$total 
len=${#array[@]}
echo "len:"$len

# remove the dupicated elements
for((i=0;i<$len;i++))
do
# notice the space near the brackets '[ ]'
if [[ ${array[$i]} -eq ${array[$i+1]} ]] || [[ ${array[$i]} -eq ${array[$i-1]} ]]; then
echo "equal:"$i
echo "frame:"${array[$i]}
fi
done
len=${#array[@]}
echo "len:"$len
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值