Monday, April 25, 2011

Errors on beginner bash script

Hi guys, could u help me out? I run this on cygwin as ./test.sh and I get unexpected end of file on line 51. Any thoughts?

Thx in advance

LAST EDIT: final version 'runnin on CygWin, the problem was with the line break, CrLf instead Lf.

#!/bin/sh
##################################################
## USAGE
##################################################
if [ $# -ne 1 ]; then
    echo "1>Use Extractor: $0 <MODO DE OPERACAO>"
    echo "2>Use Extractor: $0 <MODO DE OPERACAO> <DATA INICIAL> <DATA FINAL>"
    exit 127
else
    if [ $1 -lt 0 ]; then
     if [ $1 -gt 1 ]; then
      echo "2>Use Extractor: $0 <MODO DE OPERACAO> <DATA INICIAL> <DATA FINAL>"
      exit 127
     fi
    fi
fi
##################################################
## VARS
##################################################

    ##########################################
    ## AUX
    ##########################################
    set java_cmd=""
    set java_cmd_emergencial=""
    set java_cp=""

    ##########################################
    ## JAR
    ##########################################
    set db_user=""
    set db_pwd=""
    set conn_string=""
    set work_dir=""
    set output_dir=""


##################################################
## PARAMETROS
##################################################
set mode=$1
set data_ini=""
set data_fim=""
if [ $# -eq 3 ]; then
    set data_ini = $2
    set data_fim = $3
fi

##################################################
## CHAMADA DO JAR
##################################################
java  "$java_cp" "$java_cmd" "$mode" "$db_user" "$db_pwd" "$conn_string" "$work_dir" "$output_dir"
From stackoverflow
  • "set" isn't bash syntax. Instead of "set foo = bar", you want "foo=bar".

    Note: I wrote the corrected form without spaces for a reason. It has to be written that way. Your updated question is still wrong.

    Kamia : with or without the set I still get the same error, thx tho.
    anon : And note there must be no spaces around the '=' so 'foo = bar' would be wrong.
    Kamia : ahhhh thx for the tip
    Paul Tomblin : @Neil, that's why I wrote it that way.
    anon : @paul, I know, but the OP didn't notice...
  • Runs for me without errors on MacOS X 10.5.6, except for this line:

    if [ $1 -lt 0 -o gt 1 ];
    

    should be

    if [ $1 -lt 0 -o $1 -gt 1 ];
    

    I make no comment on whether the rest of the script actually does what it's supposed to...

    Kamia : thanks, forgot about the second param comparation, still same error :/
    Alnitak : what version of bash? with the above fix I get no error with the single parameter invocation using Bash version 3.2.39(1)-release on Fedora Code 10.
    Kamia : GNU Bash, version 3.2.48(21)-release (i686-pc-cygwin)
  • You'll probably want to quote the required fields on the last line. As right now it'll be a blank instead of "".

    eg

    java  "$java_cp" "$java_cmd" "$mode" "$db_user" "$db_pwd" "$conn_string" "$work_dir" "$output_dir"
    

    edited to atleast run java on mysystem:

    #################################################
    #!/bin/sh
    ##################################################
    ## USAGE
    ##################################################
    if [ $# -ne 1 ]; then
        echo "1>Use Extractor: $0 <MODO DE OPERACAO>;"
        echo "2>Use Extractor: $0 <MODO DE OPERACAO> <DATA INICIAL> <DATA FINAL>"
        exit 127
    else
        if [ $1 -lt 0 -o $1 -gt 1 ]; then
            echo "1>O Parametro <MODO DE OPERACAO> deve ser 0 (Cartorios) ou 1 (Transacoes);"
            exit 127
        fi
    fi
    ##################################################
    ## VARS
    ##################################################
    
        ##########################################
        ## AUX
        ##########################################
        java_cmd=""
        java_cmd_emergencial=""
        java_cp=""
    
        ##########################################
        ## JAR
        ##########################################
        db_user=""
        db_pwd=""
        conn_string=""
        work_dir=""
        output_dir=""
    
    
    ##################################################
    ## PARAMETROS
    ##################################################
    mode=$1
    data_ini=""
    data_fim=""
    if [ $# -eq 3 ]; then
        data_ini=$2
        data_fim=$3
    fi
    
    ##################################################
    ## CHAMADA DO JAR
    ##################################################
    java  $java_cp $java_cmd $mode $db_user $db_pwd $conn_string $work_dir $output_dir
    
    Kamia : not necessary, once the script is ok the vars will always be filled, thanks for the tip
  • If nothing else double-check that you have a newline at the end of the last line in the script.

    Kamia : 1st thing i've done, before that ive aded a test script only with a if-else-fi, gave me the same error, i'm beginning to think the error is on the cygwin
    Kamia : Although your answer isn't about the code itself, helped me up to find the error, the problem was the editor not saving on unix format (lf instead of crlf) now its all solved.
    Aaron Digulla : Interesting; for CR/LF errors, it should report that in line 1...
    Subtwo : In my experience CR/LF errors are usually quite hard to find. It is not like the first thing you think about.
    Kamia : the error beside minor changes on the script was indeed very subtle.
    Christian Witts : Use an editor that lets you switch on viewing EoL characters like SciTE, makes checking that trivial. :)

0 comments:

Post a Comment