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

更多相关文章

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

随机推荐

  1. javascript中的属性类型
  2. [javascript 实践篇]——那些你不知道的
  3. 如何测试从实时网站提取数据的AJAX应用程
  4. Objective-C方法/函数调用。 (来自javascr
  5. Javascript函数的4种调用方法详解
  6. React.js中的setState vs replaceState
  7. 选择无线电输入时,无法提交带有JavaScript
  8. 【JavaScript】中两个小括号 ()() 是什么
  9. jQuery的几大难点
  10. 利用javascript实现遍历xml文件的代码实