0

Ok so I want to write a bashfile system that tells a larger program that a file outputs this error message from chmod.

bash: ./\[bashfile-name\].sh: /bin/bash^M: bad interpreter: No such file or directory

Here's the relevant part of the code, format is bashfile.

error=$(chmod +x $fdnm 2\>&1 \> /dev/null)

echo "value for error was "$error

if \[ $error = "bash "$filename": /bin/bash^M: bad interpreter: No such file or directory" ]  
^^this is line 24^^

then 
   echo "patch would be done"


else
   echo "the file appears to be executable."
fi

Got this as output

value for error was chmod: cannot access '/media/usb/run-bi.sh'$'\r': No such file or directory
the file appears to be executable.

with these errors

./bad-interpreter.sh: line 24: [: too many arguments
chmod: missing operand after ‘+x’
Try 'chmod --help' for more information.

please debug. Thank you all.

Citadel
  • 1
  • 3
  • `$'\r'` is how bash writes a carriage return. In DOS and Windows tools, lines are ended with CRLF sequences (aka `\r\n`); in UNIX tools, it should be only a LF with no CR. – Charles Duffy Oct 28 '22 at 23:31
  • $fdnm is a variable that contains something like /fileaddress/bashfile.sh – Citadel Oct 28 '22 at 23:31
  • Use `dos2unix` to convert your script to UNIX form. – Charles Duffy Oct 28 '22 at 23:31
  • Also, `2>&1` should just be `2>&1`, no backslashes. Consider running your script through http://shellcheck.net/ and fixing what it finds. – Charles Duffy Oct 28 '22 at 23:32
  • the problem is that `fdnm` contains not only the filename, but also a carriage return at the end of that filename; that's the `\r` the error is talking about. – Charles Duffy Oct 28 '22 at 23:32
  • (as a separate matter, `if [ $error = "..." ]` should be `if [ "$error" = "..." ]` -- you need to quote your parameter expansions, not just strings). – Charles Duffy Oct 28 '22 at 23:33
  • I run 'fdnm=/media/usb/run-bi.sh' to dec.are the fdnm variable. – Citadel Oct 28 '22 at 23:53
  • Yes, but if you run that in a file saved with DOS newlines it gets a CR on the end. The fixes in the linked duplicate will work; so will the `dos2unix` program I suggested 25 minutes ago. – Charles Duffy Oct 28 '22 at 23:57

0 Answers0