简体   繁体   English

无法在 bash 脚本中打开任何文件:没有这样的文件或目录

[英]can't open any files in bash script: no such file or directory

I am trying to open a text file in a bash script on ubuntu 20.04.我正在尝试在 ubuntu 20.04 上的 bash 脚本中打开一个文本文件。 The bash script looks as follows: bash 脚本如下所示:

#!/bin/bash

"gedit /home/usr/textfile.txt"

I get the following output when running the bash script:运行 bash 脚本时,我得到以下 output :

./test.sh: line 3: gedit /home/usr/textfile.txt: No such file or directory

when entering the command into the terminal by hand, everything works just fine.手动将命令输入终端时,一切正常。 I've tried using a multitude of different shebangs, but that didn't help.我试过使用多种不同的shebangs,但这没有帮助。 I guess it's got to be a fairly obvious mistake here.我想这一定是一个相当明显的错误。

"gedit /home/usr/textfile.txt" is a string and not a command name to execute string you can do $("gedit /home/usr/textfile.txt") or remove quote around "gedit /home/usr/textfile.txt" 是一个字符串,而不是执行字符串的命令名,您可以执行 $("gedit /home/usr/textfile.txt") 或删除引号

#!/bin/bash

gedit /home/usr/textfile.txt

Any command you'll want to enter in a shell script shouldn't have quotes like you showed above.您要在 shell 脚本中输入的任何命令都不应该像上面显示的那样带有引号。 Try:尝试:

#!/bin/bash
set -euxo pipefail
gedit /home/usr/textfile.txt

The -euxo pipefail is really helpful and I include it in all of my shell scripts. -euxo pipefail非常有用,我将它包含在我所有的 shell 脚本中。 You can find more info about it here, https://transang.me/best-practice-to-make-a-shell-script/ .你可以在这里找到更多信息, https://transang.me/best-practice-to-make-a-shell-script/

Also, since you're scripting a file, you might want to add in either a sed command to search and edit or an echo to edit the file inside of the script.此外,由于您正在编写文件脚本,您可能需要添加sed命令来搜索和编辑,或者添加 echo 来编辑脚本内的文件。

read -r -d '' /home/usr/textfile.txt << EOF
Hello new line
EOF

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

相关问题 无法打开文件&#39;demo.py&#39;:[Errno 2] 通过 bash 脚本运行时没有这样的文件或目录 - Can't open file 'demo.py': [Errno 2] No such file or directory when running through a bash script 无法在bash中使用cat打开目录中的文件 - Can't open files in directory using cat in bash bash-递归脚本看不到子目录中的文件 - bash - recursive script can't see files in sub directory Bash脚本,返回awk:无法打开文件〜/ .ssh / config - Bash script, returns awk: can't open file ~/.ssh/config Bash脚本:目前,我在一个目录中应该有500个文件,如果缺少任何文件,如何停止bash脚本? - Bash Scripting: I currently am supposed to have 500 files inside a directory, how can I stop a bash script if any files are missing? Bash在脚本中找不到正确的目录 - Bash can't find right directory in script Bash脚本:没有这样的文件或目录 - Bash script: No such file or directory bash脚本中“没有这样的文件或目录” - “No such file or directory” in bash script 虽然使用 bash 脚本编辑文件有效,但最后显示错误为“sed:无法读取”没有这样的文件或目录“ - While using bash script to edit file works but at last shows error as "sed: can't read "No such file or directory" Bash:无法调用函数; 没有相应的文件和目录 - Bash: can't call a function; No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM