+ -
当前位置:首页 → 问答吧 → shell脚本代码,超级简单,但是我不懂。

shell脚本代码,超级简单,但是我不懂。

时间:2011-11-30

来源:互联网

Perl code

temp="$1"
temp1=${temp##*/}
temp2=${temp%/*}
if [ -d "$temp2" ]
 then
 cd "$temp2"
 thisdir=`pwd` 
fi



问下上述代码的第2、3行是什么意思?谢谢!

作者: insulted   发布时间: 2011-11-30

shell也是可以调试的。通过:sh -x a.sh(shell脚本文件名)

作者: JoeBlackzqq   发布时间: 2011-11-30

http://bbs.chinaunix.net/viewthread.php?tid=218853&page=7#pid1617953

作者: ljc007   发布时间: 2011-11-30

Assembly code

[root@RHEL6A scripts]# ls -ld /tmp/dir1/dir2
drwxr-xr-x. 2 root root 4096 11月 30 13:19 /tmp/dir1/dir2
[root@RHEL6A scripts]# ls -l /tmp/dir1/dir2/
总用量 0
-rw-r--r--. 1 root root 0 11月 30 13:19 test1.txt
[root@RHEL6A scripts]# more s10.sh 
#!/bin/bash
temp="$1"
temp1=${temp##*/}
temp2=${temp%/*}
if [ -d "$temp2" ]
then
    cd "$temp2"
    thisdir=`pwd`
    echo $thisdir
fi
[root@RHEL6A scripts]# ./s10.sh /tmp/dir1/dir2/test1.txt 
/tmp/dir1/dir2
[root@RHEL6A scripts]# 


temp1=${temp##*/} 从变量temp开头删除最长匹配*/的字符串,如果是个文件名完整路径,那就剩下文件名了
temp2=${temp%/*} 从变量temp结尾删除最短匹配/*的字符串,如果是文件名完整路径,那就得到该文件所在目录

作者: askandstudy   发布时间: 2011-11-30