分享
 
 
 

很方便的两个shellscript

王朝other·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

最近我把系统重作了一下,于是成了 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 <sea___sky@sina.com>."

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 <sea___sky@sina.com>."

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 <sea___sky@sina.com>."

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!

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 <sea___sky@sina.com>."

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 "lowks2upks (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]")

if [ "$filez" = "$file" ]; then

echo "THE FILE "$i" IS ALREADY UPPERCASED!"

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 "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

# end of the "working part"!

--------------------------------------

但是,"declare -x filez=$(echo $i | /bin/awk BEGIN'{FS="/"} {print $NF}')" 一句中的 "declare" 在 cygwin 下的 sh 就认不出来了,我不知道其他版本的会不会有问题(我用的是 Red Hat 6.2)。其实,如果不行也可以把"declare -x " 换成 " && export filez " 就行了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有