這個函數最後的 `done' 沒有
panduan2()
{
comman_counter=0
for i in `0 $c`
if [ -f $i ]
then
mv $i "$i + new"
comman_counter=`expr $comman_counter + 1`
return $comman_counter
else
return 2
fi # 下壹列加個 done 上去
}
mv $i "$i + new" # 會改名字為壹個有兩個空格和壹個加號的檔案名
其他還有不用 " " 來保護變量, [ -f $i ] , 例
nc10@your-5554c55be4 ~
$ i="buggy file"
nc10@your-5554c55be4 ~
$ ls buggy\ file
buggy file
nc10@your-5554c55be4 ~
$ [ -f $i ] && echo yes
bash: [: buggy: binary operator expected
nc10@your-5554c55be4 ~
$ [ -f "$i" ] && echo yes
yes
nc10@your-5554c55be4 ~
$
只要檔案名有空格就出錯, 不太大的腳本或是常重復用的代碼,
我本人不會寫太多函數, 我寫壹個看看,,
#! /bin/sh
# using traditional sh, not bash, for portable
usage="Usage: `basename $0` path"
path=$1
count=0
if [ $# -eq 0 ] || [ ! -d "$path" ]
then
echo "$usage" >&2
exit 1
fi
cd "$path" && echo "We are in `pwd`"
echo ""
files=`ls -l | grep '^-' | wc -l`
directory=`ls -l | grep '^d' | wc -l`
echo "We have $files files and $directory directory here."
echo "We just rename the file."
for i in *
do
if [ -f "$i" ] ; then
name="$i.NEW"
mv -f "$i" "$name"
count=`expr $count + 1`
else
continue
fi
echo "$i is renamed to $name"
done
echo "All done."
echo "$count are renamed."
exit 0
測試
nc10@your-5554c55be4 ~
$ ls tmp
marksix.awk* sheet02.pdf sheet03.txt sheet05.pdf testdir2/
sheet01.pdf sheet02.txt sheet04.pdf sheet05.txt
sheet01.txt sheet03.pdf sheet04.txt testdir/
nc10@your-5554c55be4 ~
$ cat test_script
#! /bin/sh
# using traditional sh, not bash, for portable
usage="Usage: `basename $0` path"
path=$1
count=0
if [ $# -eq 0 ] || [ ! -d "$path" ]
then
echo "$usage" >&2
exit 1
fi
cd "$path" && echo "We are in `pwd`"
echo ""
files=`ls -l | grep '^-' | wc -l`
directory=`ls -l | grep '^d' | wc -l`
echo "We have $files files and $directory directory here."
echo "We just rename the file."
for i in *
do
if [ -f "$i" ] ; then
name="$i.NEW"
mv -f "$i" "$name"
count=`expr $count + 1`
else
continue
fi
echo "$i is renamed to $name"
done
echo "All done."
echo "$count are renamed."
exit 0
nc10@your-5554c55be4 ~
$ chmod +x test_script
nc10@your-5554c55be4 ~
$ ./test_script
Usage: test_script path
nc10@your-5554c55be4 ~
$ ./test_script llll
Usage: test_script path
nc10@your-5554c55be4 ~
$ ./test_script tmp
We are in /home/nc10/tmp
We have 11 files and 2 directory here.
We just rename the file.
marksix.awk is renamed to marksix.awk.NEW
sheet01.pdf is renamed to sheet01.pdf.NEW
sheet01.txt is renamed to sheet01.txt.NEW
sheet02.pdf is renamed to sheet02.pdf.NEW
sheet02.txt is renamed to sheet02.txt.NEW
sheet03.pdf is renamed to sheet03.pdf.NEW
sheet03.txt is renamed to sheet03.txt.NEW
sheet04.pdf is renamed to sheet04.pdf.NEW
sheet04.txt is renamed to sheet04.txt.NEW
sheet05.pdf is renamed to sheet05.pdf.NEW
sheet05.txt is renamed to sheet05.txt.NEW
All done.
11 are renamed.
nc10@your-5554c55be4 ~
$ ls tmp
marksix.awk.NEW* sheet02.pdf.NEW sheet03.txt.NEW sheet05.pdf.NEW testdir2/
sheet01.pdf.NEW sheet02.txt.NEW sheet04.pdf.NEW sheet05.txt.NEW
sheet01.txt.NEW sheet03.pdf.NEW sheet04.txt.NEW testdir/
nc10@your-5554c55be4 ~
$
代碼羅唆,呵呵,妳可用 sh -x script_name 看看是怎麼運行的