0

I'm trying to create a bash script which uses a funcion from a course file that should receive parameters to do something.

but, I'm getting syntax error, tried to research the syntax of a function but couldnt figure out the issue, could you guys help me?

that the function from the config file:

function log_output($log_dir,$NAME_JOB,$cluster_logs_dir) {
    export TERMINO=`date "+%Y-%m-%d %H:%M:%S"`
    export TERMINO_SEG=`date -d "$TERMINO" +%s`
    export DT_INICIO_SEG=`date -d "$DT_INICIO" +%s`
    let RESUL=TERMINO_SEG-DT_INICIO_SEG
    HORAS=$(((RESUL/3600)))
    MIN=$(((RESUL-(HORAS*3600))/60))
    SEG=$((($RESUL%60)))
    echo '=================================================='
    echo "# JOB NAME       : $NAME_JOB"
    echo "# STATUS         : $rc"
    echo "# AGENTE         : $HOST_NAME"
    echo "# INICIO         : $DT_INICIO"
    echo "# TERMINO        : $TERMINO"
    echo "# DURAÇAO        : $HORAS : $MIN : $SEG"
    echo "# ULTIMO_ERRO    : $ULTIMO_ERRO"
    echo '=================================================='

    hdfs dfs -copyFromLocal $log_dir/"$NAME_JOB""_""$TIMESTAMP".log $cluster_logs_dir

}

thats the part of my shell script that I'm trying to use the function

log_output "$log_dir" "$NAME_JOB" "$cluster_logs_dir"

and here is the error I'm getting

/data/workdir/sustentacao_id/desenvolvimento/m292121/encaps_logs/properties/general_config_file: line 16: syntax error near unexpected token `$log_dir,$NAME_JOB,$cluster_logs_dir'
/data/workdir/sustentacao_id/desenvolvimento/m292121/encaps_logs/properties/general_config_file: line 16: `function log_output($log_dir,$NAME_JOB,$cluster_logs_dir) {'
/data/workdir/sustentacao_id/desenvolvimento/m292121/encaps_logs/properties/general_config_file: line 16: syntax error near unexpected token `$log_dir,$NAME_JOB,$cluster_logs_dir'
/data/workdir/sustentacao_id/desenvolvimento/m292121/encaps_logs/properties/general_config_file: line 16: `function log_output($log_dir,$NAME_JOB,$cluster_logs_dir) {'

I tried some approaches and none has worked

gfernandes
  • 193
  • 1
  • 10
  • 1
    Just `log_output() {`; you don't declare parameters, they're just positional (`$1`, `$2`, etc). – Charles Duffy Nov 10 '22 at 16:32
  • BTW, I _really do mean_ `log_output() {`, not `function log_output() {`; see https://wiki.bash-hackers.org/scripting/obsolete, which mentions `function` twice (once in the first table, again in the third). – Charles Duffy Nov 10 '22 at 16:33
  • Also, stay away from all-caps names -- that's the namespace used for variables that reflect or modify the behavior of the shell itself or POSIX-specified tools. Names with at least one lowercase character are reserved for use by applications (like your script); see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html – Charles Duffy Nov 10 '22 at 16:34
  • Also, if you're only targeting new versions of bash, think about replacing all the `/usr/bin/date` use with shell-builtin `printf %()T`; so for example: `printf -v termino_seg %(%s)T -1; printf -v termino '%(%Y-%m-%d %H:%M:%S)T' "$termino_seg"`; faster and more portable (at least, where a new enough bash is available) to avoid relying on external applications for things the shell itself can do built-in. – Charles Duffy Nov 10 '22 at 16:36
  • ok, I got that part, that I shouldn't declare parameters in my function and should not use function NAME_FUNCTION () {} ..thanks...to be honest, in all my tries, I didn't remove the parameters..=/ – gfernandes Nov 10 '22 at 16:41
  • and thanks for all the suggestion, I'll try to assimilate them and accomodate in my script – gfernandes Nov 10 '22 at 16:42
  • Another note -- using `let` for math is legacy (1980s) ksh syntax; bash supports it for backwards compatibility, but the modern POSIX way to do integer math is `resul=$((termino_seg - dt_inicio_seg))`, as most of the rest of your script is doing already. And think about dropping the `export`s unless those variables _really need_ to be in the environment; environment size counts against the same limit as command-line length, so the more data you export, the smaller your maximum allowed command-line length is. – Charles Duffy Nov 10 '22 at 16:48

0 Answers0