KandZ – Tuts

We like to help…!

Linux CLI 58 ๐Ÿง shell functions part 2

a - shell functions return codes
function return codes are used to indicate the success or failure of a function's execution.
also known as exit statuses
a return code of 0 indicates successful execution
while any other value indicates an error or failure.
return 0 โ†’ returns 0 for successful execution
return 1 โ†’ return 1 for non succesful execution
check_success $? โ†’ $? captures the return code

b - shell function exit command and local variables
exit command terminates a script execution based on some condition
You can also return code using exit commad
exit 0 โ†’ terminates execution with return code 0
exit 1 โ†’ terminates execution with return code 1
local variables are used to define variables within a specific scope or context inside a function.
These variables are not accessible outside the function
They do not affect or interfere with global variables of the same name.
local result=$1 โ†’ declares a local variable with the name result

c - shell functions error handling
Error handling is an essential aspect of writing robust shell scripts
strategies for implementing effective error handling in functions within your shell scripts:
using return codes
using set -e โ†’ automatically fails on error
using trap โ†’ Trap errors and handle them manually
Using set -u โ†’ automatically fails on undefined variables
set -e โ†’ exits immediately if a command exits with a non-zero status.
set -u โ†’ fails on an undefined variable.
trap 'echo "An error... โ†’ traps errors and handle them manually.

Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!

Leave a Reply

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock