简体   繁体   English

用可变的bash shell打开目录

[英]open directory with a variable bash shell

I'am reading in a variable that will contain a version of a certain file (Ex.: V1.0.10) by the following command. 我正在读取一个变量,该变量将通过以下命令包含某个文件的版本(例如:V1.0.10)。

read Version

and there is a possibility that that variable contains dots and I remove them by the next command: 并且该变量可能包含点,并且我可以通过下一条命令将其删除:

New_Version=`echo $Version | sed -e 's/\.//g'`

but if I use this variable later on in the script, nothing changes at this variable, and I just use the cd command: 但是,如果稍后在脚本中使用此变量,则此变量没有任何变化,而我仅使用cd命令:

cd /data/group/$New_Version

or 要么

cd /data/group/"$New_Version"

Then the error: No such file or directory... : line ...: cd:/data/group/V1010. 然后出现错误:没有这样的文件或目录...:行...:cd:/ data / group / V1010。 I double checked, the files exists, the name is correct but he doesn't find or recognize the directory? 我仔细检查过,文件是否存在,名称正确,但是他找不到或识别目录?

What am I doing wrong? 我究竟做错了什么?

Hope someone can help! 希望有人能帮忙!

Thanks 谢谢

UPDATE: The OP says that the problem was a hidden character in his input. 更新:OP表示问题出在他的输入中是隐藏字符。 This answer does not describe how to solve that problem. 该答案没有描述如何解决该问题。 Nonetheless, the OP has marked this answer as accepted. 但是,OP已将此答案标记为已接受。 See the comments of Charles Duffy for the actual solution to the OP's problem. 有关OP问题的实际解决方案,请参阅Charles Duffy的评论。

Caveat: I am taking everything in your problem description literally, which leads to the answer below. 警告:我将按照您的描述描述中的所有内容,从字面上理解,得出下面的答案。 If you provide examples of the strings that will be passed through $Version it would help clarify the issue. 如果您提供将通过$Version传递的字符串的示例,则将有助于阐明问题。


As I understand it you're reading in the full path of a file in your variable with read Version . 据我了解,您正在使用read Version变量中文件的完整路径。 Now if you say echo $Version you should get /path/to/foo.bar . 现在,如果您说echo $Version您应该得到/path/to/foo.bar

I don't think you'd want to cd into the file /path/to/foo.bar . 我认为您不希望cd进入文件/path/to/foo.bar You'll get an error: Not a directory , because it's a file, not a directory. 您会得到一个错误: Not a directory ,因为它是文件,而不是目录。

Now, consider what sed -e /\\.//g will do to the pathname. 现在,考虑sed -e /\\.//g对路径名的作用。

echo "/path/to/foo.bar" | sed -e '/\.//g'
/path/to/foobar

Does /path/to/foobar actually exist? /path/to/foobar实际上存在吗? No, because foo.bar was a file. 否,因为foo.bar是文件。 You'll get an error: No such file or directory , because the foobar directory does not exist. 您会得到一个错误: No such file or directory ,因为foobar目录不存在。

If I understand what you are trying to do, you are trying to extract the directory that contains the file specified by $Version . 如果我了解您要执行的操作,那么您将尝试提取包含$Version指定的文件的目录。 The command dirname /path/to/foo.bar will return /path/to . 命令dirname /path/to/foo.bar将返回/path/to So you want to set New_version=$( dirname "$Version" ) , at which point you should be able to cd $New_version . 因此,您要设置New_version=$( dirname "$Version" ) ,此时应该可以cd $New_version

PS Make sure $Version is reading in an absolute path name, not a relative name, so that it's independent of where you run the script from. PS确保$Version读入的是绝对路径名,而不是相对名,以便它与运行脚本的位置无关。

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

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