Bash Shell 脚本:我缺少什么简单的逻辑

发布于 2024-07-09 19:13:48 字数 557 浏览 7 评论 0原文

这可能是一个太笼统的问题,但是...... 我对尝试从 shell 脚本中移动目录感到困惑。 我不是 *nix 高级用户,但我很乐意通过命令行来完成大多数任务。 我想调用一个可以将“me”移动到目录的脚本,而不仅仅是类似于以下内容的脚本过程:

prompt:> goto lit   

其中 goto 是别名 -> goto='./goscript'

goscript 有一些简单的代码,例如:(

cd /path to work dirs/lit/user dir  

假设每个用户在 /lit 中有一个目录)

我自己通过设置我的个人别名移动到所需的目录,运行脚本来避免这个问题,然后返回到原来的目录。 这个问题是由一位同事向我提出的,他使用了类似的方法,但希望使该过程更加通用,这样我们就不需要创建我们需要的每个别名。 我认为这是一个很容易解决的问题,但我很困惑,因为到目前为止我还没有大量的 shell 脚本编写经验。

This may be too generic a question as is but...
I am stumped by trying to move through the directories from within a shell script. I'm not a *nix power user, but I am comfortable working through the command line for most tasks. I'd like to call a script that can move 'me' to a directory instead of just the script process similar to the following:

prompt:> goto lit   

where goto is an alias -> goto='./goscript'
and
goscript has some simple code in such as:

cd /path to work dirs/lit/user dir  

(assuming each user has a directory inside /lit)

I've avoided this issue myself by setting my personal alias' to move to the desired directory, run a script, then return to the original directory. This question was brought to me by a co-worker who uses a similar method, but wanted to make the process more generic so we don't need to create every single alias we need.
I thought this would be an easy problem to solve, but I'm stumped as I don't really have a great deal of shell scripting experience ...as of yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

桃气十足 2024-07-16 19:13:48

甚至比使用其他人所描述的别名更好,请查看 CDPATH 变量! 它基本上等同于 PATH 功能,但应用于 cd 命令。

例如,如果我将 CDPATH 定义为 $CDPATH:${HOME}/subdir,并且 ~/subdir 包含另一个目录,subsubdir,然后我可以简单地

cd subsubdir

从任何目录执行:,并按预期导航路径。

以下是更多具体信息:

http://www.caliban.org/bash/#bashtips

要设置 CDPATH 变量,请在 .bashrc 中添加一行,例如

export CDPATH=$CDPATH:${HOME}/subdir

Even better than using an alias as others have described, check out the CDPATH variable! It's basically equivalent of the PATH functionality, but applied to the cd command.

For example, if I define my CDPATH as $CDPATH:${HOME}/subdir, and ~/subdir contains another directory, subsubdir, then I can simply execute:

cd subsubdir

from any directory, and navigate the the path as expected.

Here's some more specifics:

http://www.caliban.org/bash/#bashtips

To set the CDPATH variable, add a line to your .bashrc, such as

export CDPATH=$CDPATH:${HOME}/subdir
你与清晨阳光 2024-07-16 19:13:48

您可以创建一个名为 goto (或其他)的函数,并确保它在 .bashrc 中定义(或者您可以从当前 shell 中“获取”它):

function goto {
    #  the "$USER" part will expand to the current username
    # the "$1" will expand to the first argument to the function ("goto xyz" => $1 is "xyz")
    cd /some-path/lit/$USER/$1
}

将其放入 ~/.bashrc 或单独的文件中,然后从提示符中调用“source the-file”,然后您可以像任何其他程序一样调用该函数:

prompt> goto folder
 cd /some-path/lit/your-user/folder

You can create a function that is called goto (or whatever) and make sure it is defined in your .bashrc (or you can "source" it from your current shell):

function goto {
    #  the "$USER" part will expand to the current username
    # the "$1" will expand to the first argument to the function ("goto xyz" => $1 is "xyz")
    cd /some-path/lit/$USER/$1
}

Put this in ~/.bashrc or in a separate file and call "source the-file" from your prompt then you can call the function just like any other program:

prompt> goto folder
 cd /some-path/lit/your-user/folder
酸甜透明夹心 2024-07-16 19:13:48

要在提示符的相同环境中执行脚本,您必须使用 来调用它。

假设您已命名脚本:

cd .. 

如果您使用 . 你得到:

gt; pwd
gt; /home/users/rd/proj
gt; . up
gt; pwd
gt; /home/users/rd

To execute a script in the same environment of your prompt you have to invoke it with .

Say you have the script named up:

cd .. 

if you invoke it with . you get:

gt; pwd
gt; /home/users/rd/proj
gt; . up
gt; pwd
gt; /home/users/rd
悲欢浪云 2024-07-16 19:13:48

你不能。 该脚本有自己的环境副本,因此它无法更改登录 shell 的环境。

You can't. The script has its own copy of the environment, and so it can't change the login shell's environment.

掀纱窥君容 2024-07-16 19:13:48

当我问这个问题时,我还不是会员(讽刺的是,我的匿名登录问题比我的官方登录问题加起来更有天赋),所以我现在无法关闭它 - 但是 - Remo D. 等人。 答案将是导致我们需要的可行解决方案的答案。 如果任何拥有 mod 权限的人都可以关闭此 & 选择它作为接受的答案,我会很感激。

I wasn't a member when I asked this (ironically my anonymous login question has higher flair than my official login questions combined), so I can't close it now - But- The Remo D. et al. answer would be the one that led to the working solution we needed. If anyone with mod powers can close this & select that as the accepted answer, i'd apprediate it.

愿得七秒忆 2024-07-16 19:13:48

您不能在 bash 脚本中使用 cd。 不过,您可以为 cd 和路径指定别名。

alias goto='cd /path_to_work/usr/dir'

更新:您可以将该行放入 .bashrc 文件中,然后

source ~/.bashrc

创建别名。

You can't use cd in a bash script. You could alias the cd and path though.

alias goto='cd /path_to_work/usr/dir'

UPDATE: you would put that line in your .bashrc file and then do

source ~/.bashrc

to create the alias.

橙味迷妹 2024-07-16 19:13:48

您可以使用别名完成一些简单的事情。 例如,我有一些别名,可以将我置于工作空间环境中并为我们的 Makefile 系统配置变量。 这是我用作示例的真实别名。 “&&” 将继续执行命令,直到其中一个命令失败 - 在像这样的简单场景中,不需要清理,因为不太可能发生失败。

alias main1='cd ~/code/main1 && export TOP=`pwd` && export DEBUG=1'

如果您想在同事之间开发别名共享库,您可以通过 NFS 共享它们并获取要包含的文件。

You can accomplish some simple stuff like this using an alias. For example, I have some aliases that put me into a workspace environment and configure variables for our Makefile system. Here's a real alias I am using as an example. The '&&' will continue executing the commands until one of them fails--in simple scenarios like this, no clean-up is needed, since failure isn't likely.

alias main1='cd ~/code/main1 && export TOP=`pwd` && export DEBUG=1'

If you want to develop a shared library of aliases amongst your co-workers, you can share them over NFS and source the file to be included.

云雾 2024-07-16 19:13:48

每个脚本对当前工作目录都有自己的想法。 “cd”不是一个进程,而是一个 shell 内置程序。

做你想做的事情的方法是创建一个别名。 在 csh 中,它将类似于:

alias goto 'cd /path_to_work/\!*/user_dir'

sh/bash 有类似的别名设施

Each script has its own idea of the current working directory. "cd" is not a process but a shell build-in.

The way to do what you want is to create an alias. In csh it will be something like:

alias goto 'cd /path_to_work/\!*/user_dir'

sh/bash have similar alias facilities

最丧也最甜 2024-07-16 19:13:48

我的 .rc 中往往有几十个别名,如下所示:

alias work='cd /home/foo/work'
alias rails='cd /home/foo/rails'
alias assets='cd /home/foo/rails/assets'

等等。

如果这对您不起作用,您也许可以使用 bash 中的调试挂钩来替换之前的命令行如果“goto”关键字是该行的第一个内容,则它会被执行。 这将涉及在 bash 中搜索“trap”,但要预先警告,此类咒语在所有系统上的行为并不相同。 几个月前我写了一篇这样的经历,欢迎您结果

I tend to have a couple dozen aliases in my .rc that look like this:

alias work='cd /home/foo/work'
alias rails='cd /home/foo/rails'
alias assets='cd /home/foo/rails/assets'

etc.

If that isn't going to work for you, you might be able to use the debug hooks in bash to replace the command line in-place before it is executed - if the 'goto' keyword is the first thing on the line. This will involve googling around for 'trap' in bash, but be forewarned, such incantations do not behave the same on all systems. I wrote up one such experience a few months ago, and you're welcome to the results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文