I know this question has been asked numerous times, but I still could not find any good solution. Hence, asking it again if anyone can help !!
我知道这个问题已被多次询问,但我仍然找不到任何好的解决方案。因此,再次询问是否有人可以帮助!!
I am trying to change a my working directory inside a shell script with help of a variable. But I get " No such file or directory"
everytime.
我试图在变量的帮助下更改shell脚本中的工作目录。但我每次都会得到“没有这样的文件或目录”。
#!/bin/bash
echo ${RED_INSTANCE_NAME} <-- This correctly displays the directory name
cd $RED_INSTANCE_NAME <-- This line gives the error
Now, when I try to give the actually directory name instead of using the variable, shell changes the directory without issues
现在,当我尝试提供实际目录名而不是使用变量时,shell会更改目录而不会出现问题
cd test <-- No error
Does anyone knows what can be the issue here ? Please help !!
有谁知道这里有什么问题?请帮忙 !!
5 个解决方案
#1
12
You variable contains a carriage return. Try saying:
您的变量包含回车符。试着说:
cd $(echo $RED_INSTANCE_NAME | tr -d '\r')
and it should work. In order to remove the CR from the variable you can say:
它应该工作。为了从变量中删除CR,您可以说:
RED_INSTANCE_NAME=$(echo $RED_INSTANCE_NAME | tr -d '\r')
The following would illustrate the issue:
以下将说明问题:
$ mkdir abc
$ foo=abc$'\r'
$ echo "${foo}"
abc
$ cd "${foo}"
: No such file or directory
$ echo $foo | od -x
0000000 6261 0d63 000a
0000005
$ echo $foo | tr -d '\r' | od -x
0000000 6261 0a63
0000004
$ echo $'\r' | od -x
0000000 0a0d
0000002
#2
6
Try
cd "$RED_INSTANCE_NAME"
Also, make sure the path makes sense to the current directory where cd
command is executed.
此外,请确保该路径对执行cd命令的当前目录有意义。
#3
3
One way to encounter your described problem is to have a tilde (~
) in the variable name. Use the absolute path or $HOME
variable instead. Note that using $HOME
will require double quotations.
遇到所描述问题的一种方法是在变量名中加上波浪号(〜)。请改用绝对路径或$ HOME变量。请注意,使用$ HOME将需要双引号。
# doesn't work
$ vartilde='~/'
$ cd $vartilde
-bash: cd: ~: No such file or directory
# works
$ varfullpath='/Users/recurvirostridae'
$ cd $varfullpath
# works
$ varwithhome="$HOME"
$ cd $varwithhome
#4
1
I don't know what is going wrong for you, but I can offer one piece of general advice:
我不知道你有什么问题,但我可以提供一条一般建议:
cd "$RED_INSTANCE_NAME" # Quote the string in case it has spaces.error
You should nearly always put the "$VARIABLE" in quotes. This will protect from surprises when the value of the variable contains funny stuff (like spaces).
你应该几乎总是把“$ VARIABLE”放在引号中。当变量的值包含有趣的东西(如空格)时,这将避免意外。
#5
1
You can check for carriage returns, ANSI escapes and other special characters with
您可以检查回车,ANSI转义和其他特殊字符
cat -v <<< "$RED_INSTANCE_NAME"
This will show all the characters that echo $RED_INSTANCE_NAME
would just hide or ignore.
这将显示回显$ RED_INSTANCE_NAME隐藏或忽略的所有字符。
In particular, if your error message is : No such file or directory
as opposed to bash: cd: yourdir: No such file or directory
, it means you have a carriage return at the end of your variable, probably from reading it from a DOS formatted file.
特别是,如果您的错误消息是:没有这样的文件或目录而不是bash:cd:yourdir:没有这样的文件或目录,这意味着您在变量的末尾有一个回车符,可能是从DOS读取它格式化文件。
#1
12
You variable contains a carriage return. Try saying:
您的变量包含回车符。试着说:
cd $(echo $RED_INSTANCE_NAME | tr -d '\r')
and it should work. In order to remove the CR from the variable you can say:
它应该工作。为了从变量中删除CR,您可以说:
RED_INSTANCE_NAME=$(echo $RED_INSTANCE_NAME | tr -d '\r')
The following would illustrate the issue:
以下将说明问题:
$ mkdir abc
$ foo=abc$'\r'
$ echo "${foo}"
abc
$ cd "${foo}"
: No such file or directory
$ echo $foo | od -x
0000000 6261 0d63 000a
0000005
$ echo $foo | tr -d '\r' | od -x
0000000 6261 0a63
0000004
$ echo $'\r' | od -x
0000000 0a0d
0000002
#2
6
Try
cd "$RED_INSTANCE_NAME"
Also, make sure the path makes sense to the current directory where cd
command is executed.
此外,请确保该路径对执行cd命令的当前目录有意义。
#3
3
One way to encounter your described problem is to have a tilde (~
) in the variable name. Use the absolute path or $HOME
variable instead. Note that using $HOME
will require double quotations.
遇到所描述问题的一种方法是在变量名中加上波浪号(〜)。请改用绝对路径或$ HOME变量。请注意,使用$ HOME将需要双引号。
# doesn't work
$ vartilde='~/'
$ cd $vartilde
-bash: cd: ~: No such file or directory
# works
$ varfullpath='/Users/recurvirostridae'
$ cd $varfullpath
# works
$ varwithhome="$HOME"
$ cd $varwithhome
#4
1
I don't know what is going wrong for you, but I can offer one piece of general advice:
我不知道你有什么问题,但我可以提供一条一般建议:
cd "$RED_INSTANCE_NAME" # Quote the string in case it has spaces.error
You should nearly always put the "$VARIABLE" in quotes. This will protect from surprises when the value of the variable contains funny stuff (like spaces).
你应该几乎总是把“$ VARIABLE”放在引号中。当变量的值包含有趣的东西(如空格)时,这将避免意外。
#5
1
You can check for carriage returns, ANSI escapes and other special characters with
您可以检查回车,ANSI转义和其他特殊字符
cat -v <<< "$RED_INSTANCE_NAME"
This will show all the characters that echo $RED_INSTANCE_NAME
would just hide or ignore.
这将显示回显$ RED_INSTANCE_NAME隐藏或忽略的所有字符。
In particular, if your error message is : No such file or directory
as opposed to bash: cd: yourdir: No such file or directory
, it means you have a carriage return at the end of your variable, probably from reading it from a DOS formatted file.
特别是,如果您的错误消息是:没有这样的文件或目录而不是bash:cd:yourdir:没有这样的文件或目录,这意味着您在变量的末尾有一个回车符,可能是从DOS读取它格式化文件。