4

I am trying to run the following script.

#!/bin/csh
# USAGE EXAMPLE: solve_mysteries.scr ops6.txt 2
# USAGE EXAMPLE: solve_mysteries.scr allops.txt 1800
set opsfile = $1
set maxtime = $2
set f = $3

set outfile = brute_solutions.dat
set outfile2 = brute_constant.dat
set outfile3 = brute_formulas.dat
if -f $outfile /bin/rm $outfile
if -f $outfile2 /bin/rm $outfile2
if -f $outfile3 /bin/rm $outfile3

echo Trying to solve mysteries with brute force...

echo Trying to solve $f...
echo /bin/cp -p $f mystery.dat
/bin/cp -p $f mystery.dat
echo $opsfile arity2templates.txt mystery.dat results.dat >args.dat

timeout {$maxtime}s ./symbolic_regress3.x

I get the following output

^M: Command not found.
^M: Command not found.
Trying to solve mysteries with brute force...
^M: Command not found.
...ing to solve ../example_data/example2.txt_train
 mystery.dat./example_data/example2.txt_train
/bin/cp: cannot stat '../example_data/example2.txt_train'$'\r': No such file or directory
^M: Command not found.
timeout: invalid time interval ‘60\rs’
Try 'timeout --help' for more information.
^M: Command not found.

Any idea why I am getting this "^M: command not found" error?

  • 3
    Your script has Windows (CRLF) line-endings `^M` is a representation of the carriage return character – steeldriver Jun 06 '20 at 22:20
  • Thank you do you have any suggestions how to fix this issue – Betaglutamate Jun 06 '20 at 22:23
  • As @steeldriver said , the text file's line endings are in the crlf format but I think it's a problem with csh . In bash CRLFs work fine. – Parsa Mousavi Jun 06 '20 at 22:24
  • see for example [What is `^M` and how do I get rid of it?](https://unix.stackexchange.com/questions/32001/what-is-m-and-how-do-i-get-rid-of-it) or one of the similar questions such as [Bash/Korn shell script edited on Windows throws error '…^M: not found'](https://unix.stackexchange.com/questions/56024/bash-korn-shell-script-edited-on-windows-throws-error-m-not-found) – steeldriver Jun 06 '20 at 22:25
  • There's also a good Q/A about this on stackoverflow: ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Jun 07 '20 at 02:09

2 Answers2

3

cat -v script shows you that at least some of the lines have a CR (carriage return) character, probably at the end.

Your script is a text file in DOS/Windows format. You need to convert it to Unix format (i.e. remove the CRs). This can be done by editors or e.g. the tool dos2unix.

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
0

If you can download Sublimetext, which is an text editor, you can change the line endings from View->LineEndings->Unix

Then save the file and try running it again.

Fra93
  • 149
  • 4