1

The following bash script, written and tested on Linux, does not even start on OS X when called.

#!/bin/bash
#
#   Some comments
#
#
function usage {
    echo ""
    echo "Usage: thisscript <SOURCE_DIRECTORY> <TARGET_DIRECTORY>"
    echo ""
    echo "<SOURCE_DIRECTORY>    the directory where the this "
    echo "              directory resides (default is /usr/dis)"
    echo ""
    echo "<TARGET_DIRECTORY>    the destination directory"
    echo ""
}

function notDarwin {
    mv -f $CUR_DIR/* $NEW_DIR/
    ln -sf "$NEW_DIR/ee/sc/scrp" "/usr/bin/scrp"
    ln -sf "$NEW_DIR/ee/etc/conffile.conf" "/etc/conffile.conf"
    exit 0
}

function isDarwin {
    mv -f $CUR_DIR/* $NEW_DIR/
    ln -sf "$NEW_DIR/ee/sc/scrp" "/usr/local/bin/scrp"
    cp "$NEW_DIR/ee/etc/conffile.conf" "/etc/conffile.conf"
    exit 0
}

#
#   =============================================
#   ================== MAIN =====================
#   =============================================
#

CUR_DIR=${1%/}
NEW_DIR=${2%/}

if [ ! -d "$CUR_DIR" ]; then
    echo ""
    echo "blah blah"
    usage
    exit 1
fi

if [ ! -d "$NEW_DIR" ]; then
    echo ""
    echo "The target directory supplied does not exist. Creating target directory $NEW_DIR"
    mkdir "$NEW_DIR"
    if [ $? -ne 0 ]; then
        echo "Could not create target directory. Exiting..."
        exit 1
    else
        echo "Directory $NEW_DIR created"
    fi
    echo ""
fi

UNAME=$(uname)
if [ $UNAME == "Darwin" ]; then
    isDarwin
else
    notDarwin
fi

It throws the following syntax error when run as sudo bash script.sh "arg1" "arg2" on macOS with bash 3.2

'script.sh: line 7: syntax error near unexpected token `{
'script.sh: line 7: `function usage {

I am rather new to OS X, maybe there is a gotcha I am missing. The script ran fine on Linux...

Thanks

1
  • 1
    You're clearly having a problem with end of line characters! you have LF/CR end of lines. Use the options of your editor to have the file saved with Unix end of lines. See stackoverflow.com/tags/bash/info and especially the first point: Check whether your script or data has DOS style end-of-line characters. Commented Oct 8, 2015 at 13:13

2 Answers 2

1

Linux and modern OS X expect lines to end with LF (line feed) characters. If your lines end with CR + LF, then you will run into problems.


Some other general pointers:

The function syntax is non-standard. You should use the standard syntax, supported by all POSIX-compliant shells:

Change:

function usage {

to:

usage() {

and I suspect that all will be well.

As an aside, you should quote all of your parameter expansions (you've missed a couple). It's also considered good practice to use lowercase variable names, as uppercase ones are used by the shell and you run the risk of clashing with them.

Sign up to request clarification or add additional context in comments.

5 Comments

Tried that too, doesn't help... :/ the error now goes line 7: usage() {
I can't see anything else that would potentially cause a problem in your script. To be honest, the function syntax should be supported in bash on either platform anyway...Maybe this is an issue with newlines?
dunno, I inspected the script and it has CR and LF on every line... I transferred the file to windows and used Notepad++
In addition, I ran the script on a different Mac machine, same mistake... baffling for me
@Dragan it shouldn't have both; it should only have LF on modern OS X.
0

This problem usually occurs when the shell script is generated on windows or other system.

Steps: Just run this command on terminal.

bash-5.2$ dos2unix your_script.sh

you may need to install dos2unix. For which use

bash-5.2$ brew install dos2unix

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.