bash cat 无法打开文件

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

cat can not open a file

bashksh

提问by vikas ramnani

In a script (Say myScript.ksh) I have a variable which contains path to a file.

在脚本(比如 myScript.ksh)中,我有一个包含文件路径的变量。

file=/home/vikas/input.ksh

in my code a need something from that file so I am doing a normal cat and grep on this file by doing something like this

在我的代码中,需要该文件中的某些内容,因此我正在通过执行以下操作在该文件上执行普通 cat 和 grep

myVariable=`cat $file | grep "on this line" | cut -d"'" -f2`

Now everytime I launch myScript.ksh, I get a error

现在每次启动 myScript.ksh 时,我都会收到错误消息

cat: cannot open /home/vikas/input.ksh

But if I do same thing direcly on terminal I am able to see content of the file input.ksh

但是如果我直接在终端上做同样的事情,我就能看到文件 input.ksh 的内容

I dont know what I am doing wrong, can somebody please help me ?

我不知道我做错了什么,有人可以帮助我吗?

采纳答案by vikas ramnani

yeah finally the problem solved, though I found the solution weired and could not understand why it worked. Actually I was reading this variable from some other file and it was something like this

是的,问题终于解决了,尽管我发现解决方案很奇怪并且无法理解它为什么起作用。实际上我是从其他文件中读取这个变量,它是这样的

file=$HOME/vikas/input.ksh 

the problem was with $HOME what I did I removed $HOME from variable file and added that again by doing this

问题出在 $HOME 我所做的我从变量文件中删除了 $HOME 并通过这样做再次添加了它

file=`echo $file | awk 'BEGIN { FS="/"; OFS="/" } {=""; print 
file=/home/vikas/input.ksh
}'` file=$HOME$file

and after that I did the cat $file and it worked

之后我做了 cat $file 并且它起作用了

回答by Miquel

Try and flip your slashes, Linux uses /instead of \

尝试翻转你的斜线,Linux 使用/而不是\

回答by Wug

Use forward slashes, not backslashes

使用正斜杠,而不是反斜杠

ls -l /home/vikas/input.ksh

Edit: OK, so that wasn't the problem.

编辑:好的,所以这不是问题。

Here's what we know at this point:

这是我们目前所知道的:

  • He's using the correct slash.
  • If he hardcodes the filename, the script works.
  • Script is running as a different user (I think?).
  • 他正在使用正确的斜线。
  • 如果他对文件名进行了硬编码,则脚本会起作用。
  • 脚本以不同的用户身份运行(我认为?)。

Please indicate which user the script runs as, and post the output of:

请指明脚本以哪个用户身份运行,并发布以下输出:

##代码##

Also, what is the output of file myScript.ksh?

另外,输出是file myScript.ksh什么?