I am trying to troubleshoot a bash script and bash doesn’t seem to be working as I would expect:
In the examples below I have 2 trivial bash scripts. One has “#!/bin/bash” starting it and the other does not.
c@localhost:~/qt/qt-everywhere-src-6.7.2$ cat cnf
#!/bin/bash
echo "In cnf"
jc@localhost:~/qt/qt-everywhere-src-6.7.2$ ./cnf
bash: ./cnf: cannot execute: required file not found
jc@localhost:~/qt/qt-everywhere-src-6.7.2$ cat jcplay
#!/bin/bash
echo "jcplay"
jc@localhost:~/qt/qt-everywhere-src-6.7.2$ ./jcplay
jcplay
jc@localhost:~/qt/qt-everywhere-src-6.7.2$
The common element is that these were created in the file. ~/qt/qt-everywhere-src-6.7.2 and seems to show that “#!/bin/bash” makes the file fail.
As an experiment I created another file in my home directory, jcplay1
#!/bin/bash
echo "in jcplay1"
jc@localhost:~$ ./jcplay1
in jcplay1
jc@localhost:~$
c@localhost:~$ cp jcplay1 /qt/qt-everywhere-src-6.7.2/
cp: cannot create regular file '/qt/qt-everywhere-src-6.7.2/': No such file or directory
jc@localhost:~$ cp jcplay1 ~/qt/qt-everywhere-src-6.7.2/
jc@localhost:~$ cd ~/qt/qt-everywhere-src-6.7.2
jc@localhost:~/qt/qt-everywhere-src-6.7.2$ chmod +x jcplay1
jc@localhost:~/qt/qt-everywhere-src-6.7.2$ ./jcplay1
in jcplay1
jc@localhost:~/qt/qt-everywhere-src-6.7.2$
Is there a rule here I don’t understand, or a bug?
If you notice the file jcplay exhibits the same behavior.
I admit it is a poor choice of names. What it still shows is that if the first line is “#!/bin/bash” and it is created in the ~/qt/qt-everywhere-src-6.7.2 directory, it fails.
It seems as if perhaps bash does not work correctly with Windows formatted files. Is that so?
That should not matter, because the OP specified the path to the file: ./
The error message required file not found seems to indicate that the file /bin/bash cannot be found. May be there are some weird characters after #!/bin/bash.
Hmm, line endings? Let’s test that:
$ cat cnf
#!/bin/bash
echo "In cnf"
$ chmod u+x cnf
$ ./cnf
In cnf
$ hexdump -C cnf
00000000 23 21 2f 62 69 6e 2f 62 61 73 68 0a 65 63 68 6f |#!/bin/bash.echo|
00000010 20 22 49 6e 20 63 6e 66 22 0a | "In cnf".|
0000001a
$ unix2dos cnf
unix2dos: converting file cnf to DOS format...
$ ll cnf
-rwxr--r-- 1 joe users 28 jun 30 22:42 cnf
$ hexdump -C cnf
00000000 23 21 2f 62 69 6e 2f 62 61 73 68 0d 0a 65 63 68 |#!/bin/bash..ech|
00000010 6f 20 22 49 6e 20 63 6e 66 22 0d 0a |o "In cnf"..|
0000001c
$ ./cnf
bash: ./cnf: cannot execute: required file not found
Well well, who would have thought?
Note that the Unix-style line ending is just 0a (line-feed), DOS-style is 0d0a (carriage-return, line-feed).
So I guess your problem is where your file originates, Linux or Windows. Just check with hexdump -C or xxd. Or even better: with the file command:
$ file cnf
cnf: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
$ dos2unix cnf
dos2unix: converting file cnf to Unix format...
$ file cnf
cnf: Bourne-Again shell script, ASCII text executable