Home > Software design >  can't open any files in bash script: no such file or directory
can't open any files in bash script: no such file or directory

Time:01-21

I am trying to open a text file in a bash script on ubuntu 20.04. The bash script looks as follows:

#!/bin/bash

"gedit /home/usr/textfile.txt"

I get the following output when running the bash script:

./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. I guess it's got to be a fairly obvious mistake here.

CodePudding user response:

"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

#!/bin/bash

gedit /home/usr/textfile.txt

CodePudding user response:

Any command you'll want to enter in a shell script shouldn't have quotes like you showed above. 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. You can find more info about it here, 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.

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