|
发表于 2009-2-18 23:04:40
|
显示全部楼层
allinster@debian:~/shells$ cat h.sh
if test -f "$1"
then
printf "$1\n"
elif test -d "$1"
then
(cd $1;printf "$1\n";pwd)
else
echo "$1不是文件或目录"
fi
这个是你原来的脚本,我这没有lpr命令,改成printf,为了显示清楚,加了个换行的 \n,为了说明参数目录存在时,shell确实进入了该目录,用pwd将其路径输出
allinster@debian:~/shells$ ls
aaa dd ddd ha hello.sh h.sh log_usrs.sh num_char.sh num_pars.sh #现在的aaa是个目录
allinster@debian:~/shells$ sh h.sh aaa
aaa
/home/allinster/shells/aaa #看到没,shell已经进入了该目录了
allinster@debian:~/shells$ rmdir aaa
allinster@debian:~/shells$ touch aa
allinster@debian:~/shells$ sh h.sh aa
aa
allinster@debian:~/shells$ rm aa
allinster@debian:~/shells$ sh h.sh aa
aa不是文件或目录
关于楼主的问题,我以为是:shell确实进入了参数指定的目录,但是脚本执行完后,这个shell就退出来了,所以,你还在你当前的目录下,没有看到你当前的shell的目录变了,你可以将你的
(cd $1;lpr $1)改成(cd $1;pwd;lpr $1)让它显示有没有进到目录里去
以个人观点,楼主这种写法看着很不爽啊,我习惯这样:
#!/bin/bash
if [ -f "$1" ]
then
printf "$1\n"
elif [ -d "$1" ]
then
cd $1
printf "$1\n"
pwd
else
echo "$1不是文件或目录"
fi
况且printf我也不多用啊,一般用echo,多方便 |
|