AMP AMP

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.

FAQ
Q
How do I pass the result of one script as an argument to another script?
A
You can pass the result of one script to another using command substitution. For example:
./script1.sh $(./script2.sh)
Q
Can I pass a variable number of arguments to a Bash script on Rocky Linux 9.2?
A
Yes, you can pass various arguments to a Bash script. You can use special variables like $@ (all arguments as separate strings) and $* (all arguments as a single string) to handle and process variable numbers of arguments more flexibly.
Q
How can I check the number of arguments passed to a Bash script?
A
You can check the number of arguments passed to a Bash script using the $# variable, representing the total number of arguments.
Q
Is it possible to use named options (flags) as arguments in a Bash script on Rocky Linux 9.2?
A
Yes, you can use named options (flags) as arguments by parsing the arguments in your script. You can use tools like getopts or manually parse the arguments using conditional statements to handle options and their associated values.
Q
What are the arguments in a Bash script?
A
Arguments in a Bash script are values provided to the script when it is executed from the command line. They are used to pass input data or parameters to the script.