-2

Below is what I'm attempting to run on my remote machine, and I'm getting 'Unexpected end of file'.

However, if i take the lines and paste them into the CLI, it executes normally.

# if [ -f running.script ] ; then echo 'Script already running.'
> else echo 'Script not running.' ; touch running.script
<ervers_status | awk 'NR==1 {print $3}' | tr -d '\r')
<ervers_status | awk 'NR==2 {print $3}' | tr -d '\r')
> if [ "$sip1" = 0 ] ; then echo "Sip Server 1 UP" ; fi
> if [ "$sip1" = 1 ] ; then echo "Sip Server 1 DOWN" ; fi
> if [ "$sip1" = 2 ] ; then echo "Sip Server 1 IDLE" ; fi
> if [ "$sip2" = 0 ] ; then echo "Sip Server 2 UP" ; fi
> if [ "$sip2" = 1 ] ; then echo "Sip Server 2 DOWN" ; fi
> if [ "$sip2" = 2 ] ; then echo "Sip Server 2 IDLE" ; fi
> rm running.script
> fi
Script not running.
Sip Server 1 UP
Sip Server 2 IDLE
#sh script.sh
script.sh: line 14: syntax error: unexpected end of file
# cat script.sh
#!/bin/sh
if [ -f running.script ] ; then echo 'Script already running.'
else echo 'Script not running.' ; touch running.script
    sip1=$(cat /var/mand/sipservers_status | awk 'NR==1 {print $3}' | tr -d '\r')
    sip2=$(cat /var/mand/sipservers_status | awk 'NR==2 {print $3}' | tr -d '\r')
    if [ "$sip1" = 0 ] ; then echo "Sip Server 1 UP" ; fi
    if [ "$sip1" = 1 ] ; then echo "Sip Server 1 DOWN" ; fi
    if [ "$sip1" = 2 ] ; then echo "Sip Server 1 IDLE" ; fi
    if [ "$sip2" = 0 ] ; then echo "Sip Server 2 UP" ; fi
    if [ "$sip2" = 1 ] ; then echo "Sip Server 2 DOWN" ; fi
    if [ "$sip2" = 2 ] ; then echo "Sip Server 2 IDLE" ; fi
    rm running.script
fi
#

I've included the below capture of my NP++ showing exactly what's in the script.

enter image description here

7
  • I would suspect one or more of the lines has an invisible character -- probably a trailing \r. Or possibly the last line is missing a newline. The final fi would be a good candidate. List the script using cat -vet script.sh - \r shows up as ^M, newline as $. Cut and paste will probably be removing such characters. Commented Nov 16, 2022 at 21:21
  • I agree with Paul, I copied your script and it runs without error. Commented Nov 16, 2022 at 21:30
  • i.imgur.com/VTJQzPN.png this is from NP++ Commented Nov 16, 2022 at 21:32
  • 1
    You've created or edited the script on a Windows machine. Don't do that. It's quite possibly related to your sipservers_status command outputting a status value with a trailing CR too. Commented Nov 16, 2022 at 23:15
  • 1
    You image .png shows that every line ends with CR LF which is Windows-speak for \r\n. You need to remove every occurrence of CR with dos2unix, or tr, or your favourite editor. Commented Nov 17, 2022 at 0:43

1 Answer 1

1

TL;DR

CR LF line endings needs to be converted to Unix LF.


I've included the below capture of my NP++ showing exactly what's in the script.

Actually it is not exactly as shown, each line has an extra control code at the end of each line. You need to remove those.

To remove the Windows file formatting via one of the following methods:

  • vi file then type :1,$s/^M/ where ^M is actually holding control and hitting v then m.
  • Install dos2unix and do dos2unix file.
1
  • 1
    Thanks so much, that fixed it. And when I said exactly as shown, I meant the screenshot was exactly what it was, not that the screenshot confirmed it was correct. I had no idea carriage returns were windows specific. Commented Nov 17, 2022 at 5:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.