将目录存储为变量供以后在 linux 脚本中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10135639/
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
Storing directory as a variable for later use in linux script
提问by jsjwooowooo
In my script, I am holding the location (path) of a file as a variable.
在我的脚本中,我将文件的位置(路径)作为变量。
For example, fileA
例如,文件A
An example of its contents are
其内容的一个例子是
fileA=/usr/anotherfolder/somefold/"filenamehere"
fileA=/usr/anotherfolder/somefold/"filenamehere"
However, when i call a command on the file in the script such as:
但是,当我在脚本中对文件调用命令时,例如:
cat $fileA
猫 $fileA
or
或者
cat "$fileA"
猫“$fileA”
I get an error saying the file or directory doesn't exist. If I echo $fileA to see what the output is, and then run a cat manually from the terminal, it works fine, don't know what is going wrong. Any help?
我收到一条错误消息,指出文件或目录不存在。如果我 echo $fileA 查看输出是什么,然后从终端手动运行 cat ,它工作正常,不知道发生了什么问题。有什么帮助吗?
Some debug info:
一些调试信息:
- fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
- echo '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' /home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"
- '[' '!' -r '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' ']'
- fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
- echo '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' /home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"
- '[' '!' -r '/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"' ']'
For these particular lines
对于这些特定的线路
Check for readable file
检查可读文件
echo $fileA
if [ ! -r "$fileA" ]
then
o=`expr $o + 1`
echo "$fileA not readable."
continue
fi
回答by kev
If file name is new
(not "new"
), then change
如果文件名是new
(不是"new"
),则更改
fileA='/home/jacob/Desktop/CS35L/WORK/2/hw/test3/"new"'
to
到
fileA=/home/jacob/Desktop/CS35L/WORK/2/hw/test3/new