0

I am running a shell script in a Linux environment, and the program reported a problem like this:

root@autodl-container-bdbc119a52-a66f19d9:~/autodl-tmp/2021-ACL-LaSAML# sh our.sh
: not foundour.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
: not found our.sh: 
our.sh: 289: our.sh: Syntax error: end of file unexpected (expecting "then")

Can you help me solve this problem? I am very grateful to you.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    Your script was saved with DOS newlines. That's obvious because of the error message being printed at the beginning of the line instead of the end. – Charles Duffy Jul 26 '23 at 13:28
  • 1
    BTW, please be more cautious in your tagging. If there's no Python in your question, don't tag it "python" -- doesn't matter if the shell script might start Python if you aren't showing us any part of the script that actually does so. Same for NLP, pytorch, and everything else. – Charles Duffy Jul 26 '23 at 13:30

1 Answers1

0
dir

If the scripts is already there:

our.sh: 289 -> please share this line of your script - it's probably some syntax logic issue. (correlated with the system communicate -> expecting "then"))

If not,

then change location to the script and try again.

if [ ]; then
    echo "condition fuliflled"
else
    echo "condition not fullfilled"
fi
Piotr Żak
  • 2,046
  • 5
  • 18
  • 30
  • Hello, I just checked dir and there is indeed our.sh in the current directory. Then line 289 is an empty line with no content, and the line above it is the last line of the shell file. – xiaoxiao Jul 26 '23 at 13:11
  • Ok, please check you syntax of file -> probably it needs fi, or then near 289 line. May you share the last logic block from this file? – Piotr Żak Jul 26 '23 at 13:15
  • `then` isn't honored if it's saved with CRLFs, because the `\r` in `\r\n` makes it `$'then\r'` which doesn't match `then`. That's also consistent with how the OP's error messages are malformed in their transcript. – Charles Duffy Jul 26 '23 at 13:28
  • @CharlesDuffy - i don't understand - \r is return and \r\n is return new line? Whats the reason for that error malforming? – Piotr Żak Jul 26 '23 at 13:34
  • 1
    @PiotrŻak, on DOS/Windows, lines of text are separated with a CRLF sequence -- first a `\r`, then a `\n`, between _every_ two lines you save in a text file. In UNIX it's only a LF -- only `\n` -- with no CR. When you print a CR it sends the cursor to the left column of the line it's already on, when you print a LF it sends the cursor down to the next line (and on a UNIX terminal with default configuration, _also_ sends it to the left, whereas on a default DOS/Windows terminal it moves the cursor only down but not over) – Charles Duffy Jul 26 '23 at 13:41
  • @PiotrŻak, so, when you run a script saved in Windows format on UNIX, (1) the CR is treated as part of the lines you're trying to run because it's not part of the newline sequence there, and (2) it messes up the commands; sometimes it's treated as an extra part of a filename (so you get a "file not found"), sometimes it changes a word that would otherwise be syntax so it's no longer syntax, etc. – Charles Duffy Jul 26 '23 at 13:42
  • (Another difference is that on DOS/Windows, CRLFs only _separate_ lines and the last line isn't expected to have one, whereas on UNIX, LFs _terminate_ lines so a text file isn't valid unless there's a final LF at the end; that's why UNIX tools will sometimes ignore the last line of a text file made on Windows) – Charles Duffy Jul 26 '23 at 13:45