很方便的两个shell script......
2009-06-30 02:08:00 来源:WEB开发网核心提示:作者:juner 最近我把系统重作了一下,于是成了 WIN98 和 linux 共存的系统,很方便的两个shell script......, 但工作久了,发现很不方便(在 WIN98 下的文件名都是大写,把 "$i" 改成 "$filez" 给忽略了,我现在改过来(不过下边的
作者:juner
最近我把系统重作了一下,于是成了 WIN98 和 linux 共存的系统, 但工作久了,发现很不方便(在 WIN98 下的文件名都是大写,而 linux 下的文件名一般都是小写字符)。在使用 mcopy 的时候,拷过来的文件名都是大写的,很不方便 linux 操作!
于是就写了两个shell脚本,这几天我用它们方便了许多操作,现在拿出来给双系统的朋友分享(我知道,像我这样用双系统的人很多哟! ;-) ),应该有人能用上它。 脚本很简单,不过在发此贴之前把它们包装了一下(这样看起来就像个系统命令了,呵呵)。不足的是,只能改当前的目录中的文件名(就是说,只可以:% upks2lowks * 或 % upks2lowks myfile, 而不能 % upks2lowks /mydir/myfile),其实后者也可以,不过会有错信息输出!
脚本内容如下:
#!/bin/sh
# This Shell Script can change UPPER-CASE FILENAME to low-case filename.
error=0;count=0
number=$(echo * | /usr/bin/wc -w)
if [ "$1" = "-h" -o "$1" = "--help" ]; then
echo "Usage: upks2lowks [PATH] [FILENAME]..."
echo "Description: Change \"FILENAME\" to \"filename\"!"
echo ""
echo " -h, --help display this info and exit"
echo " -v, --version display version info and exit"
echo ""
echo "Report bugs to
echo "PS: I think U can modify it by nothing! It's just a shell script! ;-)"
exit
fi
if [ "$1" = "-v" -o "$1" = "--version" ];then
echo "A tool form my file-utils, no version info!"
echo "(maybe ver0.0.0)?! (^o^)"
echo "Written by Juner.L"
echo ""
echo "Copyright (C) Juner"
echo "This is free software; see the source for copying conditions. There is NO"
echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
exit
fi
if [ -z "$1" ]; then
/usr/bin/printf "\a"
echo "Just put the \"FILENAME\" follow the command!"
else
for i in $*
do
if [ -e "$i" -a -w "$i" ]; then
file=$(echo $i | /usr/bin/tr "[A-Z]" "[a-z]")
if [ "$i" = "$file" ]; then
echo "The file \"$i\" is already lowcased!"
count=$(/usr/bin/expr 1 + $count)
else
echo "$i => $file"
/bin/mv -f $i $file 2> /dev/null
fi
else
echo "warning: file \"$i\" not exist OR you have no \"w\" right to file!"
fi
done
if [ "$number" -eq "$count" ]; then
echo " all file(s) is lowcased!"
else
echo " $count file(s) already lowcased!"
fi
fi
还有一个和它差不多,只改了几个小地方,是把小写的文件名改成大写的,如下:
#!/bin/sh
# This Shell Script can change low-case filename to UPPER-CASE filename.
error=0;count=0
number=$(echo * | /usr/bin/wc -w)
if [ "$1" = "-h" -o "$1" = "--help" ]; then
echo "Usage: lowks2upks [path] [filename]..."
echo "Description: Change \"filename\" to \"FILENAME\"!"
echo ""
echo " -h, --help display this info and exit"
echo " -v, --version display version info and exit"
echo ""
echo "Report bugs to
echo "PS: I think U can modify it by nothing! It's just a shell script! ;-)"
exit
fi
if [ "$1" = "-v" -o "$1" = "--version" ];then
echo "A tool form my file-utils, no version info!"
echo "(maybe ver0.0.0)?! (^o^)"
echo "Written by Juner.L"
echo ""
echo "Copyright (C) Juner"
echo "This is free software; see the source for copying conditions. There is NO"
echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
exit
fi
if [ -z "$1" ]; then
/usr/bin/printf "\a"
echo "Just put the \"filename\" follow the command!"
else
for i in $*
do
if [ -e "$i" -a -w "$i" ]; then
file=$(echo $i | /usr/bin/tr "[a-z]" "[A-Z]")
if [ "$i" = "$file" ]; then
echo "THE FILE \"$i\" IS ALREADY UPPERCASED!"
count=$(/usr/bin/expr 1 + $count)
else
echo "$i => $file"
/bin/mv -f $i $file 2> /dev/null
fi
else
echo "WARING: FILE \"$i\" NOT EXIST or YOU HAVE NO \"w\" RIGHT TO FILE!"
fi
done
if [ "$number" -eq "$count" ]; then
echo " ALL FILE(S) IS UPPERCASED!"
else
echo " $count FILE(S) ALREADY UPPERCASED!"
fi
fi
续
经过进一步了解 gawk ,我已经把上面两个脚本优化了,改过后的脚本可以在任意目录下改任意目录(你有写权限的目录/文件)下的文件名,
也就是说,可以在任意目录下
% upks2lowks /home/juner/tmp/*
而不必
% cd /home/juner/tmp/
% upks2lowks *
改后的 shell script 如下:
(如果命令路径和你机器上的不一样,改成一样的就行了)
--------------------------------------
#!/bin/sh
# This Shell Script can change UPPER-CASE filename to low-case filename.
# init the vars!
error=0;count=0
number=$(echo $* | /usr/bin/wc -w)
# end of the "init the vars"!
# process the parameter(s)!
if [ "$1" = "-h" -o "$1" = "--help" ]; then
echo "Usage: $0 [PATH] [FILENAME]..."
echo "Description: Change \"FILENAME\" to \"filename\"!"
echo ""
echo " -h, --help display this info and exit"
echo " -v, --version display version info and exit"
echo ""
echo "Report bugs to
echo "PS: I think U can modify it by nothing! It's just a shell script! ;-)"
exit 0
fi
if [ "$1" = "-v" -o "$1" = "--version" ];then
echo "upks2lowks (a tool form my file-utils) ver0.1a"
echo "Written by Juner.L"
echo ""
echo "Copyright (C) Juner"
echo "This is free software; see the source for copying conditions. There is NO"
echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
exit 0
fi
# end of the "process the parameter(s)"!
# working part!
if [ -z "$1" ]; then
/usr/bin/printf "\a"
echo "Just put the \"FILENAME\" follow the command!"
else
for i in $*
do
declare -x filez=$(echo $i | /bin/awk BEGIN'{FS="/"} {print $NF}')
pathz=$(echo $i | /bin/awk BEGIN'{FS=ENVIRON["filez"]} {print $1}')
if [ -e "$i" -a -w "$i" ]; then
file=$(echo $filez | /usr/bin/tr "[A-Z]" "[a-z]")
#不好意思,刚发帖的时候直接把上面的粘贴过来,把 "$i" 改成 "$filez" 给忽略了,我现在改过来(不过下边的那个没错)
if [ "$filez" = "$file" ]; then
echo "The file \"$i\" is already lowcased!"
count=$(/usr/bin/expr 1 + $count)
else
echo "$pathz$filez => $pathz$file"
/bin/mv -f $pathz$filez $pathz$file 2> /dev/null
fi
else
echo "warning: file \"$i\" not exist OR you have no \"w\" right to file!"
fi
done
if [ "$number" -eq "$count" ]; then
echo " all file(s) is lowcased!"
else
echo " $count file(s) already lowcased!"
fi
fi
# end of the "working part"!
--------------------------------------
另一个文件也是这么改,道理是一样的。
--------------------------------------
#!/bin/sh
# This Shell Script can change low-case filename to UPPER-CASE filename.
# init the vars!