1.用for循环结合if判断,利用tr命令进行大小写转换:
#!/bin/bash
[ $# -eq 0 ] && echo "must be give a canshu" && exit #判断用户是否输入了参数
[ ! -d $1 ] && echo "the $1 is not a directory" && exit #判断用户输入的参数是否为目录
cd $1 || { echo "con't cd to the directory $1";exit 1; } #切换到用户提供的目录
for filename in `ls` #遍历目录下的所有文件
do
if [[ $filename != *[[:upper:]]* ]];then #判断文件中是否包含大写字母,否则跳过进入下一循环,[[:upper:]]可以换成[A-Z]
continue
fi
new=`echo $filename | tr 'A-Z' 'a-z' ` #将文件中的大写字母转换为小写
mv $filename $new
echo "the $filename is rename to $new"
done
脚本中稍作修改就可以将文件名是小写的文件,改为大写:
if [[ $filename != *[[:lower:]]* ]];then #判断文件中是否包含小写字母,否则跳过进入下一循环,[[:lower:]]可以换成[a-z]
continue
fi
new=`echo $filename | tr 'a-z' 'A-Z' ` #将文件中的小写字母转换为大写