How to Pass Multiple Arguments with Multiple Flags in Bash Script on Debian 12
To Pass Multiple Arguments With Multiple Flags In Bash Script On Debian 12
Introduction:
Bash Script uses multiple arguments and flags to customize script behavior. Arguments are values passed to a script, while flags (options) modify the script's execution.
Procedure:
Step 1: Check the OS version by using following command.
root@linuxhelp:~/linuxhelp# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/
Step 2: Create a file and make script for use multiple arguments with multiple flags by using following command.
root@linuxhelp:~/linuxhelp# vim multiargsflags
#!/bin/bash
while getopts "ab:c:d" opt; do
case ${opt} in
a)
echo "Option a is print without any argument"
;;
b)
echo "Option b is print only with argument"
;;
c)
echo "Option c is print only with argument ${OPTARG}"
;;
d)
echo "Option d is print only with argument ${OPTARG}"
;;
?)
echo "Option is invalid"
;;
esac
done
Step 3: Make the Executable permission to the script file by using following command.
root@linuxhelp:~/linuxhelp# chmod +x multiargsflags
Step 4: Long list the files to check the executable permission by using following command.
root@linuxhelp:~/linuxhelp# ls -la
total 36
drwxr-xr-x 2 root root 4096 Jan 23 04:38 .
drwx------ 8 root root 4096 Jan 23 04:38 ..
-rwxr-xr-x 1 root root 349 Jan 23 04:38 multiargsflags
Step 5: Run the script with a flag by using following command.
root@linuxhelp:~/linuxhelp# ./multiargsflags -a
Option a is print without any argument
Step 6: Run the script with b flag without any argument to check if b flag needs argument by using following command.
root@linuxhelp:~/linuxhelp# ./multiargsflags -b
./multiargsflags: option requires an argument -- b
Option is invalid
Step 7: Run the script with b flag and argument by using following command.
root@linuxhelp:~/linuxhelp# ./multiargsflags -b arg1
Option b is print only with argument
Step 8: Run the script with c flag(option) and argument by using following command.
root@linuxhelp:~/linuxhelp# ./multiargsflags -c arg1
Option c is print only with argument arg1
Step 9: Run the script with d flag and argument by using following command.
root@linuxhelp:~/linuxhelp# ./multiargsflags -d arg1
Option d is print only with argument
Step 10 : Run the script with e flag to check e flag is valid or not by using following command.
root@linuxhelp:~/linuxhelp# ./multiargsflags -e
./multiargsflags: illegal option -- e
Option is invalid
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to pass multiple Arguments with Multiple Flags in Bash Script on Debian 12. Your feedback is much welcome.
Comments ( 0 )
No comments available