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.

FAQ
Q
What is the difference between flags and options in Linux?
A
Options are additional arguments that modify the behavior of a command. Flags are similar to options, but they are used to enable or disable a specific feature or behavior of a command. Both options and flags are usually specified using a single hyphen (-) or a double hyphen (--), followed by a keyword or letter.
Q
How to use the flag in Oracle?
A
Click the flag count icon to quickly see the flags assigned to you and any flags you've assigned to others. On the Flags page, you can filter the flags to show flags assigned to you or flags you've assigned to others, and you can filter by flag type.
Q
How do you check if a flag is empty in bash?
A
In bash scripting, the -z option checks if a string is null or empty. It can be used in an if statement, with the syntax if [ -z "$variable" ]. This option can help prevent errors in scripts and strings. In this example, we've declared a variable and assigned it an empty
Q
How to use the flag in Linux?
A
Using flags in Linux is very simple. All you need to do is add the flag after the command, separated by a space. For example, to list all files in a directory, including hidden ones, you would use the command ls -a. To copy a file recursively, you would use the command cp -r source_file destination_directory
Q
How to use flags in a Bash script?
A
Flags in Bash scripts are command-line options or switches that modify the behavior of the script when it is executed. These flags, distinguished as short flags (prefixed with a single hyphen - ) or long flags (prefixed with two hyphens --), provide users with a means to customize script execution.