Answers:
dir=/home/smith/Desktop/Test
parentdir="$(dirname "$dir")"
如果有斜杠也可以使用。
parentdir=$(dirname `pwd`)
...但是“在这里看到的”是坏的。解决方法是:
> pwd
/home/me
> x='Om Namah Shivaya'
> mkdir "$x" && cd "$x"
/home/me/Om Namah Shivaya
> parentdir="$(dirname "$(pwd)")"
> echo $parentdir
/home/me
显然,父目录是通过简单地附加点号文件名而得到的:
/home/smith/Desktop/Test/.. # unresolved path
但是,您必须要使用解析的路径(没有任何点对点路径组件的绝对路径):
/home/smith/Desktop # resolved path
使用的顶级答案的问题dirname
是,当您输入带点号的路径时,它们不起作用:
$ dir=~/Library/../Desktop/../..
$ parentdir="$(dirname "$dir")"
$ echo $parentdir
/Users/username/Library/../Desktop/.. # not fully resolved
这更强大:
dir=/home/smith/Desktop/Test
parentdir=`eval "cd $dir;pwd;cd - > /dev/null"`
您可以输入/home/smith/Desktop/Test/..
,也可以输入更复杂的路径,例如:
$ dir=~/Library/../Desktop/../..
$ parentdir=`eval "cd $dir;pwd;cd - > /dev/null"`
$ echo $parentdir
/Users # the fully resolved path!
编辑:如果它不起作用,请检查您cd
是否尚未修改,这是通常的做法,
$ which cd
cd is a function #cd was modified to print extra info, so use "builtin cd" to override
cd ()
{
builtin cd "$@";
ls -FGAhp
}
...
eval
如果解析用户输入有可能使您进入您不希望的地方或对系统造成不良影响,则使用此方法不是很好。可以pushd $dir 2>&1 >/dev/null && pwd && popd 2>&1 >/dev/null
代替使用eval
吗?
eval
:只需这样做parentdir=$(cd -- "$dir" && pwd)
。由于该代码cd
在子shell中运行,因此您无需cd
返回到原来的位置。如果以连字符开头--
的扩展名是$dir
。的&&
只是防止parentdir
有一个非空的内容时,cd
没有suceed。
我喜欢非常简短,清晰,有保证的代码。奖励点是,如果它不运行外部程序,则由于您需要在当天处理大量条目,因此速度会明显提高。
不确定您所拥有和想要的是什么保证,因此无论如何都要提供。
如果您有保证,则可以用非常短的代码来完成。这个想法是使用bash文本替换功能来剪切最后的斜杠以及随后的斜杠。
从原始问题的简单情况到更复杂的情况进行回答。
P=/home/smith/Desktop/Test ; echo "${P%/*}"
/home/smith/Desktop
P=/home/smith/Desktop/Test/ ; echo "${P%/*/}/"
/home/smith/Desktop/
for P in \
/home/smith/Desktop/Test \
/home/smith/Desktop/Test/
do
P_ENDNOSLASH="${P%/}" ; echo "${P_ENDNOSLASH%/*}"
done
/home/smith/Desktop
/home/smith/Desktop
for P in \
/home/smith/Desktop/Test \
/home/smith/Desktop/Test/ \
/home/smith///Desktop////Test//
do
P_NODUPSLASH="${P//\/*(\/)/\/}"
P_ENDNOSLASH="${P_NODUPSLASH%%/}"
echo "${P_ENDNOSLASH%/*}";
done
/home/smith/Desktop
/home/smith/Desktop
/home/smith/Desktop
${BASH_SOURCE[0]}
,如果不是通过source
或来获取的,则该参数将是脚本的完整路径.
。
bash: dirname 'Test': syntax error: invalid arithmetic operator (error token is "'Test'")
。链接的代码也是错误的。
dirname Test
从目录Test
是英寸
根据您是否需要绝对路径,您可能需要采取额外的步骤:
child='/home/smith/Desktop/Test/'
parent=$(dirname "$child")
abs_parent=$(realpath "$parent")
使用这个:export MYVAR="$(dirname "$(dirname "$(dirname "$(dirname $PWD)")")")"
如果你想要第四个父目录
export MYVAR="$(dirname "$(dirname "$(dirname $PWD)")")"
如果要第三个父目录
export MYVAR="$(dirname "$(dirname $PWD)")"
如果要第二个父目录
丑陋但高效
function Parentdir()
{
local lookFor_ parent_ switch_ i_
lookFor_="$1"
#if it is not a file, we need the grand parent
[ -f "$lookFor_" ] || switch_="/.."
#length of search string
i_="${#lookFor_}"
#remove string one by one until it make sens for the system
while [ "$i_" -ge 0 ] && [ ! -d "${lookFor_:0:$i_}" ];
do
let i_--
done
#get real path
parent_="$(realpath "${lookFor_:0:$i_}$switch_")"
#done
echo "
lookFor_: $1
{lookFor_:0:$i_}: ${lookFor_:0:$i_}
realpath {lookFor_:0:$i_}: $(realpath ${lookFor_:0:$i_})
parent_: $parent_
"
}
lookFor_: /home/Om Namah Shivaya
{lookFor_:0:6}: /home/
realpath {lookFor_:0:6}: /home
parent_: /home
lookFor_: /var/log
{lookFor_:0:8}: /var/log
realpath {lookFor_:0:8}: /UNIONFS/var/log
parent_: /UNIONFS/var
lookFor_: /var/log/
{lookFor_:0:9}: /var/log/
realpath {lookFor_:0:9}: /UNIONFS/var/log
parent_: /UNIONFS/var
lookFor_: /tmp//res.log/..
{lookFor_:0:6}: /tmp//
realpath {lookFor_:0:6}: /tmp
parent_: /
lookFor_: /media/sdc8/../sdc8/Debian_Master//a
{lookFor_:0:35}: /media/sdc8/../sdc8/Debian_Master//
realpath {lookFor_:0:35}: /media/sdc8/Debian_Master
parent_: /media/sdc8
lookFor_: /media/sdc8//Debian_Master/../Debian_Master/a
{lookFor_:0:44}: /media/sdc8//Debian_Master/../Debian_Master/
realpath {lookFor_:0:44}: /media/sdc8/Debian_Master
parent_: /media/sdc8
lookFor_: /media/sdc8/Debian_Master/../Debian_Master/For_Debian
{lookFor_:0:53}: /media/sdc8/Debian_Master/../Debian_Master/For_Debian
realpath {lookFor_:0:53}: /media/sdc8/Debian_Master/For_Debian
parent_: /media/sdc8/Debian_Master
lookFor_: /tmp/../res.log
{lookFor_:0:8}: /tmp/../
realpath {lookFor_:0:8}: /
parent_: /
从想法/评论开始Charles Duffy-2014年12月17日下午5:32,主题是在Bash脚本中获取当前目录名称(无完整路径)
#!/bin/bash
#INFO : /programming/1371261/get-current-directory-name-without-full-path-in-a-bash-script
# comment : by Charles Duffy - Dec 17 '14 at 5:32
# at the beginning :
declare -a dirName[]
function getDirNames(){
dirNr="$( IFS=/ read -r -a dirs <<<"${dirTree}"; printf '%s\n' "$((${#dirs[@]} - 1))" )"
for(( cnt=0 ; cnt < ${dirNr} ; cnt++))
do
dirName[$cnt]="$( IFS=/ read -r -a dirs <<<"$PWD"; printf '%s\n' "${dirs[${#dirs[@]} - $(( $cnt+1))]}" )"
#information – feedback
echo "$cnt : ${dirName[$cnt]}"
done
}
dirTree=$PWD;
getDirNames;
..
',但可能与您的想法不符。