Lets' create a shell script with a simple function to log messages , this shell script will be sourced from other shell script .
[root@oel01db Shell-Scripting]# cat utils.sh
log_message() {
local level=$1
local message=$2
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
case "$level" in
"INFO") echo -e "[$timestamp] \e[32mINFO\e[0m: $message" ;;
"ERROR") echo -e "[$timestamp] \e[31mERROR\e[0m: $message" ;;
*) echo -e "[$timestamp] DEBUG: $message" ;;
esac
}
[root@oel01db Shell-Scripting]#
Create a simple script to check the existance of a user and its primary group and let's source above script .
[root@oel01db Shell-Scripting]# cat deploy.sh
#!/bin/bash
# 1. Source the utility functions
# Make sure utils.sh is in the same directory!
source ./utils.sh
# 2. Capture the first argument
USERNAME=$1
# 3. Validation: Check if the user provided an argument
if [ -z "$USERNAME" ]; then
log_message "ERROR" "No username provided. Usage: $0 <username>"
exit 1
fi
log_message "INFO" "Checking existence of user: $USERNAME"
# 4. Logic: Search for the username in /etc/passwd
if id "$USERNAME" &>/dev/null; then
log_message "INFO" "User '$USERNAME' exists on this system."
# Optional: Show their primary group
USER_GROUP=$(id -gn "$USERNAME")
log_message "INFO" "Primary group for $USERNAME: $USER_GROUP"
else
log_message "ERROR" "User '$USERNAME' does not exist."
exit 1
fi
[root@oel01db Shell-Scripting]#
In Bash, if doesn't just check for "true" or "false." It specifically checks the Exit Status of the command that follows it.
If the exit status is 0 (Success), it runs the code after
then.If the exit status is anything else (Failure), it jumps to the
elseblock.
[root@oel01db Shell-Scripting]# cat utils.sh
log_message() {
local level=$1
local message=$2
local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
case "$level" in
"INFO") echo -e "[$timestamp] \e[32mINFO\e[0m: $message" ;;
"ERROR") echo -e "[$timestamp] \e[31mERROR\e[0m: $message" ;;
*) echo -e "[$timestamp] DEBUG: $message" ;;
esac
}
if ! ( return 2> /dev/null); then
log_message "hi" "hello there"
fi
[root@oel01db Shell-Scripting]# cat deploy.sh
#!/bin/bash
# 1. Source the utility functions
# Make sure utils.sh is in the same directory!
source ./utils.sh
# 2. Capture the first argument
USERNAME=$1
# 3. Validation: Check if the user provided an argument
if [ -z "$USERNAME" ]; then
log_message "ERROR" "No username provided. Usage: $0 <username>"
exit 1
fi
log_message "INFO" "Checking existence of user: $USERNAME"
# 4. Logic: Search for the username in /etc/passwd
if id "$USERNAME" &>/dev/null; then
log_message "INFO" "User '$USERNAME' exists on this system."
# Optional: Show their primary group
USER_GROUP=$(id -gn "$USERNAME")
log_message "INFO" "Primary group for $USERNAME: $USER_GROUP"
else
log_message "ERROR" "User '$USERNAME' does not exist."
exit 1
fi
[root@oel01db Shell-Scripting]#
[root@oel01db Shell-Scripting]# sh utils.sh
[2025-12-23 13:57:16] DEBUG: hello there
[root@oel01db Shell-Scripting]#
[root@oel01db Shell-Scripting]# sh deploy.sh mahesh
[2025-12-23 13:57:25] INFO: Checking existence of user: mahesh
[2025-12-23 13:57:25] INFO: User 'mahesh' exists on this system.
[2025-12-23 13:57:25] INFO: Primary group for mahesh: mahesh
[root@oel01db Shell-Scripting]#
The command return is only valid inside a function or a sourced script.
When you run
sh utils.shdirectly: utils.sh is executed as a standalone script and return is NOT allowed in a normal script. The!(NOT) operator turns that failure into a "true" condition, so the code inside theifblock executes. This is why you see "hello there".When you run
source ./utils.sh(inside deploy.sh): The script is being loaded into the current shell environment. In this context,returnis a valid command. Becausereturnsucceeds, the!operator makes the condition "false," and the code inside theifblock is skipped.
The "Failed" Return
When you run sh utils.sh, the shell executes the line ( return 2> /dev/null ).
The shell says: "You're trying to
return, but you aren't inside a function or a sourced script!"It generates an error exit status (usually 2 in most shells, but any non-zero number counts as a failure).
The
!(logical NOT) takes that failure and turns it into True.The
ifblock executes.
No comments:
Post a Comment