简体   繁体   English

Shell脚本更改带变量的目录

[英]Shell script change directory with variable

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. 我试图在变量的帮助下更改shell脚本中的工作目录。 But I get " No such file or directory" everytime. 但我每次都会得到" No such file or directory"

#!/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 !! 请帮忙 !!

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

Try 尝试

cd "$RED_INSTANCE_NAME"

Also, make sure the path makes sense to the current directory where cd command is executed. 另外,请确保该路径对执行cd命令的当前目录有意义。

One way to encounter your described problem is to have a tilde ( ~ ) in the variable name. 遇到所描述问题的一种方法是在变量名中加上波浪号( ~ )。 Use the absolute path or $HOME variable instead. 请改用绝对路径或$HOME变量。 Note that using $HOME will require double quotations. 请注意,使用$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

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. 你应该几乎总是把“$ VARIABLE”放在引号中。 This will protect from surprises when the value of the variable contains funny stuff (like spaces). 当变量的值包含有趣的东西(如空格)时,这将避免意外。

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. 这将显示echo $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. 特别是,如果您的错误消息是: No such file or directory而不是bash: cd: yourdir: No such file or directory ,这意味着你在变量的末尾有一个回车符,可能是从DOS读取它格式化文件。

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

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