Bash 조건문

Bash, or the Bourne Again Shell, is a popular shell and command language in Linux. It allows users to interact with the operating system through the command line interface. One of the key features of Bash is its ability to execute conditional statements, which enable users to create decision-making processes within their scripts. In this blog post, we will explore the various types of Bash conditional statements and provide examples of how they can be used in Linux.

IF 조건문

The if statement is used to execute a block of code based on a specified condition. It follows the syntax:

if [ condition ]; then
    # code to be executed if condition is true
fi

Here, [ condition ] is an expression or command that evaluates to either true or false. If the condition is true, the code inside the if block is executed; otherwise, it is skipped.

Example:

Let’s say we want to check if a file named example.txt exists. We can use the if statement to accomplish this:

if [ -f example.txt ]; then
    echo "The file exists."
fi

In this example, if the file example.txt exists, the message “The file exists.” will be printed.

IF-ELSE 조건문

The if-else statement allows us to execute different blocks of code based on the condition. It follows the syntax:

if [ condition ]; then
    # code to be executed if condition is true
else
    # code to be executed if condition is false
fi

If the condition is true, the code inside the if block is executed; otherwise, the code inside the else block is executed.

Example:

Let’s modify our previous example to include the else block:

if [ -f example.txt ]; then
    echo "The file exists."
else
    echo "The file does not exist."
fi

Now, if the file example.txt exists, the message “The file exists.” will be printed; otherwise, the message “The file does not exist.” will be printed.

IF-ELIF-ELSE 조건문

The if-elif-else statement allows us to handle multiple conditions sequentially. It follows the syntax:

if [ condition1 ]; then
    # code to be executed if condition1 is true
elif [ condition2 ]; then
    # code to be executed if condition2 is true
else
    # code to be executed if all conditions are false
fi

The conditions are checked sequentially from top to bottom. If a condition is true, the corresponding block of code is executed, and the rest of the statements are skipped.

Example:

Let’s say we want to check if a number is positive, negative, or zero. We can use the if-elif-else statement to achieve this:

if [ $num -gt 0 ]; then
    echo "The number is positive."
elif [ $num -lt 0 ]; then
    echo "The number is negative."
else
    echo "The number is zero."
fi

In this example, $num is the variable holding the number we want to check. If the number is positive, the message “The number is positive.” will be printed; if it’s negative, the message “The number is negative.” will be printed; otherwise, the message “The number is zero.” will be printed.

Bash conditional statements provide a powerful mechanism for executing code based on specific conditions in Linux. By understanding how to use if, if-else, and if-elif-else statements, you can create sophisticated scripts that perform different actions based on the state of your system.