- 论坛徽章:
- 1
|
原帖由 cocacopi 于 2005-12-30 11:49 发表
比如如果有个脚本需要调用自己同级或子级目录的其它脚本,这个时候安装位置的绝对路径就显得很重要了。其实有个很简单的方法,让人手工输入安装路径到shell脚本中就行了。
但是,难道shell脚本就没办法知道自己所 ...
我不是已經回過了嗎:
原帖由 網中人 于 2005-12-30 10:49 发表
若 script path 是 / 開頭的, 直接 ${0%/*} 就好, 否則就是 $PWD/${0%/*}.
下面是實在結果:
kenny@x40:~/tmp> cat 1.sh
#!/bin/bash
[ ${0:0:1} = / ] && c_dir=${0%/*} || c_dir=$PWD/${0%/*}
echo current dir is: $c_dir
echo file you want is : $c_dir/1.txt
echo
kenny@x40:~/tmp> ./1.sh
current dir is: /home/kenny/tmp/.
file you want is : /home/kenny/tmp/./1.txt
kenny@x40:~/tmp> /home/kenny/tmp/1.sh
current dir is: /home/kenny/tmp
file you want is : /home/kenny/tmp/1.txt
kenny@x40:~/tmp> ../tmp/1.sh
current dir is: /home/kenny/tmp/../tmp
file you want is : /home/kenny/tmp/../tmp/1.txt
kenny@x40:~/tmp> /tmp/../etc/sysconfig/../../home/kenny/tmp/../tmp/1.sh
current dir is: /tmp/../etc/sysconfig/../../home/kenny/tmp/../tmp
file you want is : /tmp/../etc/sysconfig/../../home/kenny/tmp/../tmp/1.txt
kenny@x40:~/tmp> ~/tmp/1.sh
current dir is: /home/kenny/tmp
file you want is : /home/kenny/tmp/1.txt
kenny@x40:~/tmp> cd ..
kenny@x40:~> tmp/1.sh
current dir is: /home/kenny/tmp
file you want is : /home/kenny/tmp/1.txt
kenny@x40:~> ./tmp/1.sh
current dir is: /home/kenny/./tmp
file you want is : /home/kenny/./tmp/1.txt
kenny@x40:~> cd tmp/d1
kenny@x40:~/tmp/d1> ../1.sh
current dir is: /home/kenny/tmp/d1/..
file you want is : /home/kenny/tmp/d1/../1.txt
記住: 找得到路徑就好, 不管你身在何處...
[ 本帖最后由 網中人 于 2005-12-30 12:44 编辑 ] |
|