0

I have a function that is called twice with 2 different parameters. The function performs some checks and exits execution if check fails. If check succeeds, the execution continues. However in my case the execution is not exited, and it continues to call the function a second time.

I am calling the function each time and storing the returned value in variables. From what I have understood looking through answers on google and stackoverflow, the problem seems to be the fact that when I am calling the function to store it in the variable, it is running in a subshell and the "exit" just exits execution from the subshell, while the shell script continues executing. I am providing the code below:

check_profile_path() {
    local profileToCheck=$1
    if [ -e "$pdfToolBoxPath/used_profiles/Check$profileToCheck.kfpx" ]; then
        return 0
    else
        outputArr[status]="failed"
        outputArr[message]="profile configuration path for $profileToCheck not found at specified path"
        exitCode=1

        end_execution
    fi
}

check_profile() {
    local profileName=$1
    check_profile_path $profileName

    local containedText=$($someApplicationPath $somePath/used_profiles/Check$profileName.kfpx $fileToCheck)

    echo "<<<<<<<<<< $profileName >>>>>>>>>>"
    echo "$containedText"
    echo ""
}

end_execution() {
    jsonResult=$(create_json)

    echo $jsonResult
    exit $exitCode
}

colorSpaceProfileName="ColorSpace"
resolutionProfileName="Resolution"

colorSpaceCheckResult=$(check_profile $colorSpaceProfileName)
echo "$colorSpaceCheckResult"
resolutionCheckResult=$(check_profile $resolutionProfileName)
echo "$resolutionCheckResult"

The output I recieve from this is:

{"status":"failed","message":"profile configuration path for ColorSpace not found at specified path"}
<<<<<<<<<< Resolution >>>>>>>>>>
ProcessID   8..........

while I expect it to be just:

{"status":"failed","message":"profile configuration path for ColorSpace not found at specified path"}

I cannot set the proper syntax.. Please suggest..

Debopam Parua
  • 460
  • 1
  • 4
  • 24
  • 1
    Then don't `echo` that crap from inside the function. If you want this for diagnostics, diagnostic messages should go to standard error: `echo "diagnostic message" >&2` – tripleee Feb 11 '19 at 09:50
  • 1
    Also, you have many quoting errors. See http://shellcheck.net/ and https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Feb 11 '19 at 09:51
  • Pardon my ignorance, I am new to shell scripting.. I need to pass the text returned from the profile_check to the php script from which this is executed.. How can I return the text other than echoing? – Debopam Parua Feb 11 '19 at 09:58
  • I mean don't `echo` e.g. the `<<<<...>>>>` string to standard output if you don't want it in your results. – tripleee Feb 11 '19 at 10:19
  • Yes, that of course stops displaying the result on the standard output, but it does not satisfy my requirement, which is stopping execution of the code with the first failure.. – Debopam Parua Feb 11 '19 at 10:29

1 Answers1

1

With exit, the current process is ended. You are invoking your functions as, i.e., $(check_profile $colorSpaceProfileName), which means that they are run into their own process, and hence the exit inside the function only leaves this process.

Here are two workarounds:

  • Don't collect the output of the function in this way. Collect them inside the function and store them into a variable, which you can then retrieve on the calling side.

  • Arrange that the functions set a exit code, depending on whether the caller should exit or not, and evaluate the exit code of the function at the calling side, i.e. something like:

    colorSpaceCheckResult=$(check_profile $colorSpaceProfileName)

    (( $? == 2 )) && exit
Debopam Parua
  • 460
  • 1
  • 4
  • 24
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • (Can anybody see why my two codelines are not formatted as code, even though I have intended them by 4 spaces?) – user1934428 Feb 11 '19 at 10:42
  • Yes, I finally went for the first solution you provided.. Thank you.. I was only wondering if may be there was some way to invoke the functions in the current shell itself, and not use a subshell.. I am not sure if that can be done or how it is done, if at all possible.. – Debopam Parua Feb 11 '19 at 10:48
  • As for the codelines, the code formatting around the lines "``` ```" went missing. – Debopam Parua Feb 11 '19 at 10:49
  • @DebopamParua : Thank you for editing my post. Why do I need the triple-backquotes here? Usually it is sufficient to just put at least 4 spaces at the start of the line and the line is formatted as code. – user1934428 Feb 11 '19 at 10:50
  • @DebopamParua : One more thing: You are first collecting the stdout of the functions, and after this, you just echo this result to stdout again. Unless you need this value again later, wouldn't it make more sense to not collect the stdout of the functions at all? In this case, the functions would run in the same shell and the `exit` would behave in the way you intended. – user1934428 Feb 11 '19 at 11:04
  • 2
    The Stack Overflow Markdown parser was recently updated to permit \`\`\` for code formatting as an alternative to indenting by four spaces. See https://meta.stackexchange.com/questions/125148/implement-style-fenced-markdown-code-blocks – tripleee Feb 11 '19 at 11:20