“cd” does not work in shell script

陌路散爱 提交于 2019-11-29 16:01:48

问题


I am wondering why cd does not work in shell script. It is as follows,

#!/bin/sh
cd test
mkdir $(date +%d-%mm-%Y)

When I run this, I get can't cd to test

cd: 2: can't cd to /test

Why is it like this?


回答1:


I had the same problem. Turned out the problem was \r\n line endings.

To fix it, do

tr -d "\r" < oldname.sh > newname.sh

From http://talk.maemo.org/showthread.php?s=1cadd53b369d5408c2b9d53580a32dc4&t=67836&page=2




回答2:


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.




回答3:


put pwd as the first line. Then see if that directory has a test subdirectory.

It looks like its running from the root directory




回答4:


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:

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

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 and your current directory, but in the script it only searches 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.

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.




回答5:


The answer by Benito Ciaro is on point. I would just like to add another method that you can use to remove \r\n line endings. Open the script in text-editor Sublime and in the menu

Goto View → Line Endings → Unix

This will remove the '\r' character from your script. Don't forget to save your file.




回答6:


Well I got it working using ""

So in your case it would be:

cd "test"

/Marcus




回答7:


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.




回答8:


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).

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

Giving the full path will also work if the script is in your $PATH.




回答9:


2 is the errno for "No such file or directory". Are you sure the script test exists in the working directory of the script?

You might want to cd to a known "good" directory first and then cd into known child directories of that good directory.




回答10:


I faced the same problem in ubuntu. My command shell is:

$ 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"



回答11:


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.

Try this

. myscript.sh


来源:https://stackoverflow.com/questions/4918249/cd-does-not-work-in-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!