简体   繁体   English

Shell脚本:需要使用当前目录之前的文件夹

[英]Shell Script: Need to use previous folder than current directory

Having this structure: 具有以下结构:

/dir1/dir2/dir3/dir4/my_script.sh /dir1/dir2/dir3/dir4/my_script.sh

At one point in my script a need to store the complete path to dir3 into a variable. 在脚本的某一时刻,需要将dir3的完整路径存储到变量中。 I know this works: 我知道这可行:

CURRENT=`pwd`

But, How can I store the previous one? 但是,如何存储上一个?

WARNING: I have searched for similar questions, but all include changing the current directory, which is something i want to avoid. 警告:我搜索了类似的问题,但是所有问题都包括更改当前目录,这是我要避免的事情。

Edit: What I mean is, as i'm running my_script.sh, the current directory is /dir1/dir2/dir3/ dir4/ I want to store in a variable just: /dir1/dir2/ dir3/ 编辑:我的意思是,当我运行my_script.sh时,当前目录为/ dir1 / dir2 / dir3 / dir4 /我想存储在一个变量中:/ dir1 / dir2 / dir3 /

How about using dirname ? 如何使用dirname

dirname $(pwd)
/dir1/dir2/dir3

To store the value of one directory up in a variable : 要将一个目录的值存储在变量中:

 x=$(dirname $(pwd))
 echo $x
 /dir1/dir2/dir3

You can use BASH string manipulation to get parent's parent directory path of any full path: 您可以使用BASH字符串操作来获取任何完整路径的parent的父目录路径:

$> pwd
/dir1/dir2/dir3/dir4
$> echo "${PWD%/[^/]*}"
/dir1/dir2/dir3

%/[^/]* removes last matching pattern /* from $PWD which is the current working directory. %/[^/]*从当前工作目录$PWD删除最后一个匹配模式/*

Rather than doing any string manipulation or launching commands in a subshell, you can refer to the parent directory of any directory as .. locally or if you can trust the $PWD variable, you can use $PWD/.. . 您可以在本地将任何目录的父目录引用为..而不是在子shell中进行任何字符串操作或启动命令,或者如果您可以信任$PWD变量,则可以使用$PWD/..

Of course, this just lets you USE the parent directory, it doesn't give you the NAME of the parent directory. 当然,这只允许您使用父目录,而没有给您父目录的名称。 For that, you already have a couple of usable alternatives. 为此,您已经有几个可用的替代方法。 For one more, read on. 再读一遍。

In bash, the cd builtin command has two options, -P which tells cd to use the physical directory structure traversing the directory structure as it resolves symbolic links, and -L about which the man page says this: 在bash中, cd内置命令具有两个选项-P告诉cd在解析符号链接时使用遍历目录结构的物理目录结构,以及-L手册页对此进行说明:

                                   the -L option forces symbolic links
      to  be followed by resolving the link after processing instances
      of .. in dir.  If .. appears in dir, it is processed by removing
      the  immediately previous pathname component from dir, back to a
      slash or the beginning of dir.

So ... while you might be perfectly fine with the following: 所以...虽然您可能会完全满意以下内容:

parentdir="$(cd ..; pwd)"

You can therefore get the parent directory with: 因此,您可以使用以下命令获取父目录:

parentdir="$(cd -L ..; pwd)"

Note that while this does involve a directory change, the change is effectively within a subshell ( $(...) ), so it doesn't affect your script. 请注意,尽管这确实涉及目录更改,但是更改实际上是在子shell( $(...) )内,因此它不会影响您的脚本。

Note that I say "effectively" because strictly speaking, $(...) , cd and pwd are all built-in to bash, so you're not actually spawning new shells. 请注意,我说“有效”是因为严格来说, $(...)cdpwd都是bash 内置的 ,因此您实际上并没有产生新的shell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM