Search code examples
linuxbashunixsystem

Unix System Command Exit Code


I'm trying to use the Unix System command to kick off a script and I receive the following error message.

sh: location/script.ksh: No such file or directory

The error message is expected but what I cant figure out is how to make the system command exit with a failure.

The code I'm using is below, would anyone happen to know where im going wrong or what I am missing?

elif [ ${VAR} -gt 0 ]; then

**grep for string** |
awk -v job_text="${VAR2}" \
-v job_seq_no=0 \
'BEGIN { FS = ";" } ; \
{ command_pos=index($1,job_text)+length(job_text) } ;\
                      { job_seq_no++ } ; \
                      { system (substr($1,command_pos,length($1)-command_pos))}'

Im not much of a scripter so sorry if this is a dumb question or not possible.

Thanks in advance!

Cameron


Solution

  • Are you trying to exit the script? You need to note the return value of system within awk, and then report that to the shell as the exit status of awk. Just store the value returned by system in a variable, and pass that variable as an argument to exit. For a simple example, consider:

    $ cat a.sh
    #!/bin/bash
    
    if echo "$1" | awk '{r=system $0} END {exit r}'; then
            echo awk succeeded
    else
            echo awk failed >&2
            exit 1
    fi
    
    $ ./a.sh false
    awk failed
    $ ./a.sh true
    awk succeeded
    

    If you just want to exit awk, you could use

    awk '{r=system (...); if (r!=0){exit r}}'