LINUX高手帮忙
真的有很多问题, 语法有问题,设计有问题
这个函数最后的 `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 看看是怎麼运行的