How to use Flags on Bash Script on Oracle Linux
To Use Flags On Bash Script On Oracle Linux
Introduction :
In Bash, flags serve as optional arguments for scripts, allowing users to customize their functionality. These flags are typically represented by a single letter or a combination of letters, prefixed with a hyphen (-), and can be used to alter the script's behavior or provide supplementary information.
Procedure :
Step 1: Check the OS-version
[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.4"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.4"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.4"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:4:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.4
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.4
Step 2: Create the file
[root@linuxhelp ~]# vi flag_script1
#!/bin/bash
echo "Hello, World!"
# Use the -h flag to print help
if [ "$1" = "-h" ]; then
echo "This is a help message."
exit 0
fi
Step 3: Change the permissions
[root@linuxhelp ~]# chmod +x flag_script1
Step 4: Run the script without flag
[root@linuxhelp ~]# ./flag_script1
Hello world!
Step 5:Call the script with the -h flag
[root@linuxhelp ~]# ./flag_script1 -h
Hello world!
This is a help message
Step 6: Create the another one file
[root@linuxhelp ~]# vi flag_script2
#!/bin/bash
# Use the -n flag with a value
if [ "$1" = "-n" ]; then
echo "The name is $2"
exit 0
fi
Step 7: Change the permissions of the file
[root@linuxhelp ~]# chmod +x flag_script2
Step 8: Run the script
[root@linuxhelp ~]# ./flag_script2 -n john
The name is john
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to use flags on Bash Script in Oracle Linux. Your feedback is much welcome.
Comments ( 0 )
No comments available