[英]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.