logo
down
shadow

How to exit a bash function that expects a return


How to exit a bash function that expects a return

By : Panos Maroudas
Date : November 22 2020, 01:01 AM
should help you out I'm not completely clear on the how/why bash's exit keyword is inconsistant when calling a function that expects a return vs. not expecting a return. , When you call:
code :
foo=$(exitfunc)
foo=`exitfunc`


Share : facebook icon twitter icon
Given a main function and a cleanup function, how (canonically) do I return an exit status in Bash/Linux?

Given a main function and a cleanup function, how (canonically) do I return an exit status in Bash/Linux?


By : Tom McLellan
Date : March 29 2020, 07:55 AM
With these it helps Return the exit code from main. As the user of your script, I want to know if it did its job, not whether it cleaned up after failure. In fact, I expect programs/scripts clean up properly when they fail, so returning 0 would be misleading.
Lazily-written scripts just return with whatever exit code their last command happened to have. Well-written ones make sure to return a meaningful exit code.
code :
WORKING_DIR=$(mktemp -d)

cleanUp() {
    rm -rf "$WORKING_DIR"
}

# Run cleanUp() when the script exits, whether that's from a SIGTERM/SIGINT or
# just exiting normally.
trap cleanUp EXIT
bash: return exit code from function and print out from her, but not the exit code

bash: return exit code from function and print out from her, but not the exit code


By : Roshan Silva
Date : March 29 2020, 07:55 AM
I hope this helps you . I have a function like this , please use return 1 return 0 not echo
Unable to return NULL in a function that expects an integer return type

Unable to return NULL in a function that expects an integer return type


By : Kalpana Pink
Date : March 29 2020, 07:55 AM
With these it helps If the return type is int, you can't return a NULL. To show an error, you could instead return a special value like zero or -1, if you check for that value in any calling function. Lots of functions return nonnegative numbers on success, or -1 on error.
NULL cannot be stored in an int variable, unlike in SQL, for example. If you ignore the warning and return NULL anyway, then NULL will be casted to zero. The calling function won't be able to tell whether you returned NULL or zero.
Unable to trap ctrl-c to exit function exit bash script

Unable to trap ctrl-c to exit function exit bash script


By : Elciyas
Date : March 29 2020, 07:55 AM
I wish this help you I finally got it. The issue apparently was that I was trap'ing ctrl-c inside a function that ran in the background. Moving "trap ctrl_c INT" out of the "working" function and into the main script solved the issue.
code :
ctrl_c() { kill %%; exit; }


trap ctrl_c INT
working &
task 10
kill -s SIGPIPE %%
echo "`tput rc`task 10 completed"
Bash: why does 'return 0' in parenthesis doesn't exit the function

Bash: why does 'return 0' in parenthesis doesn't exit the function


By : user319148
Date : October 04 2020, 12:00 PM
I hope this helps you . It's not being ignored; it's executing in a subshell. It's not immediately obvious to me if it fails, because it's not being used in a function in that shell, or if it "returns" from that subshell to the function.
To fix this, use {...}, not (...), so that return executes in the same shell as the rest of the function.
code :
foo (){
  local seconds="$1"
  [[ "$seconds" -eq "0" ]] && { echo "equals 0"; return 0; }

  echo "Didn't exit with 'return 0'"
  return 1
}
Related Posts Related Posts :
  • Security of su root in bash script
  • how to replace a string at a specific position in a csv file using bash
  • how to delete duplicate lines in a text file in unix bash?
  • Compare a variables in bash
  • How do I use sed on a string?
  • Using bash variables for range in sed
  • convert a DURATION HH:MM:SS to seconds in bash
  • Shell make system paths click-able when printed out by my shell script
  • I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements
  • AWK between 2 patterns - first occurence
  • Nginx on docker - daemonize and attach to bash
  • Delete or Replace nth line in a file after a pattern match using shell scripts
  • Actual return code for SCP
  • Bash script to split file into two files while changing output file during processing
  • Pass command line arguments via sbatch
  • variable substitution (what do you call/name this method)?
  • Call function from another shell script
  • How to Grep and Replace With Exclusions
  • shell command to remove characters after a special character in bash/shell
  • How do I run a script for 1st working day of every month in cron?
  • Bash Unexpected End of File Error After Installing RVM
  • Nested Loop with increment on inner loop?
  • video orientation detection in bash
  • Interrupt sleep in bash with a signal trap
  • Want file rename script to add number at end with fixed length
  • What's the DRYest way to replace a file with a symlink in bash
  • How to run Bitcoin's autogen.sh file?
  • Renaming files in folder and subfolders using a list- Bash Script/Unix
  • Bash command to compare more than two files
  • If It is a directory append text
  • What's wrong with my WHILE-DO loop, or why my script can't find the file I'm giving it on a plate?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org