Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Shell scripting if else

The document provides a comprehensive guide on performing arithmetic operations and conditional statements in Bash scripting. It covers various methods for arithmetic, including 'let', 'expr', and '((...))', along with user input examples for addition, subtraction, multiplication, and division. Additionally, it explains the use of if-else statements for conditional execution, including basic conditions, file existence checks, and logical operators.

Uploaded by

zacksid908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Shell scripting if else

The document provides a comprehensive guide on performing arithmetic operations and conditional statements in Bash scripting. It covers various methods for arithmetic, including 'let', 'expr', and '((...))', along with user input examples for addition, subtraction, multiplication, and division. Additionally, it explains the use of if-else statements for conditional execution, including basic conditions, file existence checks, and logical operators.

Uploaded by

zacksid908
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Your script demonstrates different ways to perform arithmetic operations in Bash.

Below is a
detailed explanation of each section:

1️⃣ Using let for Arithmetic


bash
CopyEdit
let a=5+4
echo $a # Output: 9

let "a = 5 + 4"


echo $a # Output: 9

let a++ # Increments `a` by 1


echo $a # Output: 10

let "a = 4 * 5"


echo $a # Output: 20

Explanation:

• let is used for arithmetic operations.


• let a=5+4 performs addition.
• let a++ increments a by 1.
• let "a = 4 * 5" multiplies 4 and 5.

2️⃣ Using expr for Arithmetic


bash
CopyEdit
expr 10 + 5 # Output: 15
expr 10 - 5 # Output: 5
expr 10 \* 5 # Output: 50 (Backslash is required before `*` to prevent shell
expansion)
expr 10 / 2 # Output: 5
expr 10 % 3 # Output: 1 (Modulus gives the remainder)

Explanation:

• expr is used for arithmetic calculations.


• Spaces are required between operators.
• \* is used for multiplication because * is a special character in the shell.

3️⃣ Using ((
... )) (Double Parentheses)
bash
CopyEdit
a=$(( 4 + 5 ))
echo $a # Output: 9
a=$((3+5))
echo $a # Output: 8

b=$(( a + 3 ))
echo $b # Output: 11

b=$(( $a + 4 ))
echo $b # Output: 12

(( b++ )) # Increments `b` by 1


echo $b # Output: 13

(( b += 3 )) # Adds 3 to `b`
echo $b # Output: 16

a=$(( 4 * 5 ))
echo $a # Output: 20

Explanation:

• $(( ... )) is used for arithmetic operations.


• (( b++ )) increments b by 1.
• (( b += 3 )) adds 3 to b.

4️⃣ Addition & Subtraction (User Input)


bash
CopyEdit
echo "First number: "
read a

echo "Second number: "


read b

c=$((a+b))
echo "Addition: $c"

Explanation:

• read a and read b take input from the user.


• $((a + b)) performs addition.
• The result is stored in c and displayed.

bash
CopyEdit
echo "First number: "
read a

echo "Second number: "


read b
c=$((a-b))
echo "Subtraction: $c"

Explanation:

• Same as addition, but using - for subtraction.

5️⃣ Multiplication & Division (User Input)


bash
CopyEdit
echo "First number: "
read a

echo "Second number: "


read b

c=$((a * b))
echo "Multiplication: $c"

Explanation:

• Uses * for multiplication.

bash
CopyEdit
c=$((a / b))
echo "Division: $c"

Explanation:

• Uses / for division.


• If b = 0, it will cause an error (division by zero is not allowed).

Key Takeaways:

Method Description Example

let Performs arithmetic let a=5+3

expr Performs arithmetic with spaces expr 10 + 5

$(( ... )) Arithmetic expansion a=$((4+5))


Method Description Example

(( ... )) Arithmetic with direct variables (( a++ ))


Your script is attempting to read two numbers from the user and add them together, but there are
a few issues with how quotes are represented (" instead of "). Here’s the corrected
version:

Corrected Script
bash
CopyEdit
#!/bin/bash

echo "First number: "


read a

echo "Second number: "


read b

c=$((a + b))
echo "Addition: $c"

How It Works:

1. echo "First number: " → Prints a prompt for the first number.
2. read a → Reads user input and stores it in a.
3. echo "Second number: " → Prints a prompt for the second number.
4. read b → Reads user input and stores it in b.
5. c=$((a + b)) → Performs addition and stores the result in c.
6. echo "Addition: $c" → Displays the result.

Example Run
bash
CopyEdit
$ ./script.sh
First number:
5
Second number:
7
Addition: 12

Would you like to extend this script with subtraction, multiplication, or division?
4o

You said:

If else condition in shell sription and give me all the coding examples very simple and easy to do
ChatGPT said:

ChatGPT

if-else Condition in Shell Scripting

The if-else statement allows conditional execution of commands. Here are some simple
examples.

1️⃣ Basic if Condition


bash
CopyEdit
#!/bin/bash

num=10

if [ $num -gt 5 ]; then


echo "The number is greater than 5"
fi

Explanation:

• -gt means "greater than".


• The condition checks if num is greater than 5.

2️⃣ if-else Condition


bash
CopyEdit
#!/bin/bash

echo "Enter a number:"


read num

if [ $num -gt 10 ]; then


echo "The number is greater than 10"
else
echo "The number is 10 or less"
fi
Explanation:

• The script asks for user input.


• It checks if the number is greater than 10.
• If true, it prints the first message; otherwise, it prints the second.

3️⃣ if-elif-else (Multiple Conditions)


bash
CopyEdit
#!/bin/bash

echo "Enter a number:"


read num

if [ $num -gt 10 ]; then


echo "The number is greater than 10"
elif [ $num -eq 10 ]; then
echo "The number is exactly 10"
else
echo "The number is less than 10"
fi

Explanation:

• elif means "else if".


• The script checks for three possible cases.

4️⃣ Check If a File Exists


bash
CopyEdit
#!/bin/bash

echo "Enter filename:"


read filename

if [ -f "$filename" ]; then
echo "The file exists"
else
echo "The file does not exist"
fi

Explanation:

• -f checks if a file exists.


5️⃣ Check If a Number is Even or Odd
bash
CopyEdit
#!/bin/bash

echo "Enter a number:"


read num

if (( num % 2 == 0 )); then


echo "The number is even"
else
echo "The number is odd"
fi

Explanation:

• (( num % 2 == 0 )) checks if the number is divisible by 2.

6️⃣ String Comparison


bash
CopyEdit
#!/bin/bash

echo "Enter a word:"


read word

if [ "$word" = "hello" ]; then


echo "You entered hello!"
else
echo "You entered something else."
fi

Explanation:

• = checks if two strings are equal.

7️⃣ Logical Operators


bash
CopyEdit
#!/bin/bash

echo "Enter your age:"


read age

if [ $age -ge 18 ] && [ $age -le 60 ]; then


echo "You are eligible to work."
else
echo "You are not eligible to work."
fi

Explanation:

• && means "AND" (both conditions must be true).


• -ge means "greater than or equal to".
• -le means "less than or equal to".

Key Comparison Operators

Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to
= String equal
!= String not equa

You might also like