How to Pass Arguments using Bash Script on Rocky Linux 9.2
To Pass Arguments using Bash Script on Rocky Linux 9.2
Introduction:
In a Bash script, you can pass arguments to the script when you run it from the command line. These arguments are typically used to provide input data or options to the script. You can access these arguments within your Bash script using special variables and then process them as needed.
Procedure:
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: Creating a script file named argscript.sh by using the vim text editor
[root@Linuxhelp ~]# vim argscript.sh
Insert the following Lines.
#!/bin/bash
for var in "$@"
do
if [ "$var" == "Aqsa" ] || [ "$var" == "Rasa" ]
then
echo " Authenticated User Detected."
else
echo " Unauthorized user Detected."
fi
done
Step 3: Give the executable permission for the script file by using the below command
[root@Linuxhelp ~]# chmod +x argscript.sh
Step 4: 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
-rwxr-xr-x. 1 root root 189 Oct 9 12:26 argscript.sh
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Desktop
drwxr-xr-x. 2 root root 47 Oct 9 11:32 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
drwxr-xr-x. 2 root root 6 Oct 7 18:30 sample
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Templates
drwxr-xr-x. 2 root root 6 Oct 7 20:15 testdir
drwxr-xr-x. 2 root root 6 Aug 13 22:33 Videos
Step 5: Execute the script file along with username which is mentioned in the script file by using ./command
[root@Linuxhelp ~]# ./argscript.sh Aqsa
Authenticated User Detected.
Step 6: Again run the script along with the username which is not mentioned in the script file
[root@Linuxhelp ~]# ./argscript.sh Nishanth
Unauthorized user Detected.
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to Pass Arguments using Bash Script on Rocky Linux 9.2. Your feedback is much welcome.
Comments ( 0 )
No comments available