How to pass multiple arguments to multiple flags using Bash Script on Rocky Linux 9.2
To Pass Multiple Arguments To Multiple Flags Using Bash Script On Rocky Linux 9.2
Introduction:
In Bash scripting, flags are used to specify options for a script. Arguments are the values that are provided to those flags. When passing multiple arguments to a flag, you can either use a space-separated list of arguments or an array.
Procedure Steps:
Step 1: Check the OS version by using the below command.
[root@Linuxhelp ~]# cat /etc/os-release
NAME="Rocky Linux"
VERSION="9.2 (Blue Onyx)"
ID="rocky"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Rocky Linux 9.2 (Blue Onyx)"
ANSI_COLOR="0;32"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:rocky:rocky:9::baseos"
HOME_URL="https://rockylinux.org/"
BUG_REPORT_URL="https://bugs.rockylinux.org/"
SUPPORT_END="2032-05-31"
ROCKY_SUPPORT_PRODUCT="Rocky-Linux-9"
ROCKY_SUPPORT_PRODUCT_VERSION="9.2"
REDHAT_SUPPORT_PRODUCT="Rocky Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.2"
Step 2: Create a script file named script.sh by using vim text editor.
[root@Linuxhelp ~]# vim script.sh
Insert the following script to Pass Multiple Arguments to Multiple Flags using Bash Script
# Define the options (flags)
options="x:y:z:"
# Parse the command-line arguments
while getopts "$options" opt; do
case $opt in
x)
x_args="$OPTARG"
;;
y)
y_args="$OPTARG"
;;
z)
z_args="$OPTARG"
;;
?)
echo "Invalid option: -$OPTARG"
exit 1
;;
esac
done
# Print the extracted arguments
echo "x_args: $x_args"
echo "y_args: $y_args"
echo "z_args: $z_args"
Step 3: Long list the files by using the below command
[root@Linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Documents
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Downloads
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Public
-rw-r--r--. 1 root root 515 Nov 28 22:49 script.sh
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Templates
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Videos
Step 4: Now give the executable permission to the script file by using the below command
[root@Linuxhelp ~]# chmod +x script.sh
Step 5: Again long list the files to check the permission of the script file which has given.
[root@Linuxhelp ~]# ll
total 8
-rw-------. 1 root root 1039 Aug 13 22:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Documents
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Downloads
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Music
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Pictures
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Public
-rwxr-xr-x. 1 root root 515 Nov 28 22:49 script.sh
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Templates
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Videos
Step 6: Now run the script without flags by using the ./ command.
[root@Linuxhelp ~]# ./script.sh
x_args:
y_args:
z_args:
Our script shows the empty value
Step 7: Again run the script with the flags which is mentioned in the script file by using the below command
[root@Linuxhelp ~]# ./script.sh -x WELCOME -y TO -z LINUXHELP
x_args: WELCOME
y_args: TO
z_args: LINUXHELP
Now our script executes the correct value.
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to pass multiple arguments to multiple flags using Bash Script on Rocky Linux 9.2. Your feedback is much welcome.
Comments ( 0 )
No comments available