Linux Shell 脚本使用变量更改目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19281328/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:02:48  来源:igfitidea点击:

Shell script change directory with variable

linuxbashshellunix

提问by Vivek

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 脚本中的工作目录。但我" 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 !!

有谁知道这里有什么问题?请帮忙 !!

采纳答案by devnull

You variable contains a carriage return. Try saying:

You 变量包含一个回车。试着说:

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

回答by Alexander L. Belikoff

Try

尝试

cd "$RED_INSTANCE_NAME"

Also, make sure the path makes sense to the current directory where cdcommand is executed.

此外,请确保路径对cd执行命令的当前目录有意义。

回答by Adrian Ratnapala

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”放在引号中。当变量的值包含有趣的东西(如空格)时,这将防止意外。

回答by that other guy

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_NAMEwould just hide or ignore.

这将显示所有echo $RED_INSTANCE_NAME将隐藏或忽略的字符。

In particular, if your error message is : No such file or directoryas 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 格式的文件中读取的。

回答by trailing slash

One way to encounter your described problem is to have a tilde (~) in the variable name. Use the absolute path or $HOMEvariable instead. Note that using $HOMEwill 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

回答by david jimenez

I ran into a different issue. My "cd $newDir" was failing because I added logging into my script. Apparently if you add a pipe to any cd command it does nothing or gets gobbled up.

我遇到了一个不同的问题。我的“cd $newDir”失败了,因为我在脚本中添加了登录。显然,如果您向任何 cd 命令添加管道,它什么也不做或被吞噬。

Wasted 3 hours figuring that out.

浪费了 3 个小时才弄清楚。

newDir=$oldDir/more/dirs/

cd $newDir #works

cd $newDir | tee -a log #does nothing

cd $newdir | echo hi    #does nothing

So cd with any pipe does nothing. No idea why cd fails. The pipe means finish what command you doing then feed any output to next command. This is on RHEL 7.
I was trying to log all my commands and hit this nice error. Figured I'd post it in case anyone else hits it.

所以 cd 与任何管道什么都不做。不知道为什么 cd 失败。管道意味着完成您正在执行的命令,然后将任何输出提供给下一个命令。这是在 RHEL 7 上。
我试图记录我的所有命令并遇到这个很好的错误。我想我会发布它以防其他人点击它。