Linux “cd”在 shell 脚本中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4918249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"cd" does not work in shell script
提问by Abhishek
I am wondering why cd
does not work in shell script. It is as follows,
我想知道为什么cd
在 shell 脚本中不起作用。它如下,
#!/bin/sh
cd test
mkdir $(date +%d-%mm-%Y)
When I run this, I get can't cd to test
当我运行这个时,我无法 cd 来测试
cd: 2: can't cd to /test
Why is it like this?
为什么会这样?
回答by Kevin A. Naudé
Shouldn't you use
你不应该使用
cd /test
instead? I don't know shell scripting, but I can see that your particular script is sensitive to the current directory.
反而?我不知道 shell 脚本,但我可以看到您的特定脚本对当前目录很敏感。
回答by Foo Bah
put pwd
as the first line. Then see if that directory has a test subdirectory.
放在pwd
第一行。然后查看该目录是否有 test 子目录。
It looks like its running from the root directory
它看起来像是从根目录运行
回答by KazW
It depends on where the script is being executed from, if the script is in your $PATH, then it will be based off of the current directory you gave the command from (working directory).
这取决于脚本的执行位置,如果脚本在您的 $PATH 中,那么它将基于您从(工作目录)发出命令的当前目录。
If this is a script being run as a cron job, it's best to use a full directory path.
Example:
cd /home/user/test
如果这是作为 cron 作业运行的脚本,最好使用完整的目录路径。
示例:
cd /home/user/test
Giving the full path will also work if the script is in your $PATH.
如果脚本在您的 $PATH 中,则提供完整路径也将起作用。
回答by Matthew Iselin
2
is the errno for "No such file or directory". Are you sure the script test
exists in the working directory of the script?
2
是“没有这样的文件或目录”的错误号。您确定该脚本test
存在于脚本的工作目录中吗?
You might want to cd
to a known"good" directory first and then cd into knownchild directories of that good directory.
您可能希望先cd
进入一个已知的“好”目录,然后 cd 进入该好目录的已知子目录。
回答by Benito Ciaro
I had the same problem. Turned out the problem was \r\n line endings.
我有同样的问题。原来问题是 \r\n 行尾。
To fix it, do
要修复它,请执行
tr -d "\r" < oldname.sh > newname.sh
From http://talk.maemo.org/showthread.php?s=1cadd53b369d5408c2b9d53580a32dc4&t=67836&page=2
来自http://talk.maemo.org/showthread.php?s=1cadd53b369d5408c2b9d53580a32dc4&t=67836&page=2
回答by Oldek
Well I got it working using ""
好吧,我使用“”让它工作
So in your case it would be:
所以在你的情况下,它将是:
cd "test"
/Marcus
/马库斯
回答by Tyler
I had this problem, and was very confused for a while.
我遇到了这个问题,并且很困惑一段时间。
It turns out I had set my $CDPATH
environment variable, which normally allows regular cd
commands to work as usual. However, I was running my script in non-interactive mode, as "sh" (not "bash"), where the behavior is a little different. It seems that a command like:
事实证明我已经设置了我的$CDPATH
环境变量,它通常允许常规cd
命令照常工作。但是,我在非交互模式下运行我的脚本,作为“sh”(不是“bash”),行为有点不同。似乎是这样的命令:
cd subdir # works via interactive bash; not in script run via sh.
will work as expected in my interactive login shell, bash, even when CDPATH
is set. However, when I run the identical command in a script (using sh
), it failed with
将在我的交互式登录 shell bash 中按预期工作,即使CDPATH
已设置。但是,当我在脚本中运行相同的命令(使用sh
)时,它失败了
myscript.sh: line 9: cd: subdir: No such file or directory
I modified it to be a relative path:
我将其修改为相对路径:
cd ./subdir
and it works! I believe the difference is in how the shell uses CDPATH
. In one case, it searches both CDPATH
andyour current directory, but in the script it onlysearches CDPATH
. This is similar to the behavior of PATH
. If you leave .
(the current directory) out of your PATH
, then you have to type ./localbinary
instead of just localbinary
to execute that file.
它有效!我相信不同之处在于 shell 如何使用CDPATH
. 在一种情况下,它会同时搜索CDPATH
和您当前的目录,但在脚本中它只搜索CDPATH
. 这类似于 的行为PATH
。如果您将.
(当前目录)排除在 . 之外PATH
,则您必须键入./localbinary
而不是仅localbinary
执行该文件。
This is my educated guess. When I set / unset CDPATH
it breaks / unbreaks the cd subdir
command, and cd ./subdir
works in all cases for me.
这是我有根据的猜测。当我设置/取消设置时,CDPATH
它会中断/取消cd subdir
命令,并且cd ./subdir
适用于所有情况。
回答by Martijn Courteaux
Not really relevant for this question. I had the same error message, however, I was using
与这个问题无关。我有同样的错误消息,但是,我正在使用
cd ~/foo/bar
After changing this to
将其更改为
cd $HOME/foo/bar
it was fixed.
它是固定的。
回答by Zepp
Make sure you are in the right directory
确保您在正确的目录中
Run the command bellow to known where are you
运行下面的命令知道你在哪里
pwd
Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd
succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.
Shell 脚本在子shell 中运行,每个子shell 都有自己的当前目录的概念。的cd
成功,但只要子shell退出,你是在交互式背壳和从来都没有改变,因此。
Try this
尝试这个
. myscript.sh
回答by Chetabahana
I faced the same problem in ubuntu. My command shell is:
我在 ubuntu 中遇到了同样的问题。我的命令外壳是:
$ export DIR=mydir
then execute a script file that contains:
然后执行包含以下内容的脚本文件:
#!/bin/sh
cd ~/$DIR
giving output:
给出输出:
cd: 2: can't cd to ~/mydir
by trying many options, at the end it can only be solved like this:
通过尝试多种选择,最终只能这样解决:
#!/bin/sh
WORKDIR=~/$DIR
cd "$WORKDIR"