Shell脚本更改带变量的目录
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
更多相关文章
- linux系统更改目录和文件的权限总结
- 漂亮的LinuxC注释转换器--(2.2)目录级联转换最终实现
- 在linux bash do循环中保持变量的值
- 堆栈/帧指针作为外部变量
- 《Linux命令行与shell脚本》笔记--第5章:使用Linux环境变量
- 在Linux用tar归档压缩文件时忽略某些文件和目录
- Ubuntu系统环境变量详解
- Debian下环境变量设置
- linux目录结构详细介绍