歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> 很方便的兩個shell script

很方便的兩個shell script

日期:2017/2/27 14:21:48   编辑:SHELL編程
  最近我把系統重作了一下,於是成了 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 <[email protected]>." 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 <[email protected]>." 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 <[email protected]>." 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 <[email protected]>." 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 " 就行了。



但是,"declare -x filez=$(echo $i /bin/awk BEGIN'{FS="/"} {print $NF}')" 一句中的 "declare" 在 cygwin 下的 sh 就認不出來了,我不知道其他版本的會不會有問題(我用的是 Red Hat 6.2)。其實,如果不行也可以把"declare -x " 換成 " && export filez " 就行了。





Copyright © Linux教程網 All Rights Reserved