2
#!/bin/bash
if [ "$1" = "boot" ]
then
     if [ -f /var/log/boot.log ]
     then
         echo /var/log/boot.log
     elif [ -f /var/log/boot ]
     then
         echo /var/log/boot
     fi
fi

This shows the output:

: command not foundline 8: GetLogfileName.sh: line 15: syntax error
near unexpected token `elif' 'etLogfileName.sh: line 15: `        
elif [ -f /var/log/boot ]

What is going wrong here?

Jens
  • 69,818
  • 15
  • 125
  • 179
Meena
  • 21
  • 1
  • 3
  • The script as you show it works fine in `bash` on my system. I think @glennjackman may be on to something with the carriage returns. – lurker Sep 11 '13 at 13:09
  • Inspect file as hex dump and check if newlines are just 0A, or Dos-style 0D 0A. My favorite command for this: most -b script.sh – hyde Sep 11 '13 at 13:12

2 Answers2

6

The garbled error message indicates your file has carriage returns before the newline. Did you edit your script on Windows? Either use your text editor to save the file without carriage returns or run the script through dos2unix (or perhaps d2u)

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • +1: That's my suspicion too. The OP has been edited a few times, so it may be hard to tell if there's another issue involved. – David W. Sep 11 '13 at 16:13
2

If you are using vi editor, set ":set ff=unix", save the file, and re-execute it.

This file format (ff) set command tells vi to use LF-only line endings when the file is saved.

konyak
  • 10,818
  • 4
  • 59
  • 65