Scripting

腳本中的“^M:找不到命令”

  • June 7, 2020

我正在嘗試執行以下腳本。

#!/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

我得到以下輸出

^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.

知道為什麼我會收到此“^M: command not found”錯誤嗎?

cat -v script向您展示了至少一些行有一個 CR(輸入)字元,可能在末尾。

您的腳本是 DOS/Windows 格式的文本文件。您需要將其轉換為 Unix 格式(即刪除 CR)。這可以由編輯器或例如工具來完成dos2unix

如果你可以下載 Sublimetext,它是一個文本編輯器,你可以從 View->LineEndings->Unix 更改行尾

然後保存文件並嘗試再次執行它。

引用自:https://unix.stackexchange.com/questions/591331