2147483647 2147483647 - 1 year ago 59
Bash Question

Why would a bash script produce the error "command not founde"?

What is wrong with the 5th line in this script ( I have included the snippet that gives me the error and the actual error is listed in the bottom after the code and a link to complete script)?

#! /bin/bash
while [ $# -gt 0 ]; do # Until you run out of parameters...
case "$1" in
echo "Usage: $0 (options)"
echo "Options:"
echo " --prefix [installation directory]"
echo " --clean [clean all objects and binaries in Oem]"
echo " --help [Display usage]"
shift # Check next set of parameters.

This is the error i get when i run this bash script on linux (REHL5) :

: command not founde 4:
: command not founde 8:
: command not founde 8:
: command not founde 12:
MapGuide Open Source build script for OEM components
'/build_oem.sh: line 17: syntax error near unexpected token `in
'/build_oem.sh: line 17: ` case "$1" in

Please note, that the line number above corresponds to the actual script i am running (i have included a link to that script below)
The original script i am running

Answer Source

From the errors, I'm pretty sure you have carriage returns (aka CR or ^M) at the end of the lines. Windows/DOS text files have carriage return AND linefeed at the end of each line, but unix programs (like bash) just expect a linefeed, and get horribly confused if there's a CR as well. The giveaway is error messages like:

: command not founde 4:

What this really is is ./build_oem.sh: line 4: ^M: command not found, but the carriage return makes the terminal go back to the beginning of the line, and write the end of the message over the beginning of the message:

./build_oem.sh: line 4: 
: command not found
: command not founde 4:

To fix the script, use dos2unix to convert it to proper unix format, then switch to a text editor that saves in unix format.