0% found this document useful (0 votes)
0 views

Shell_Script

Uploaded by

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

Shell_Script

Uploaded by

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

1.Hello World Script.

->Code:

#!/bin/bash

echo "Hello, World!"

Output:

2. Script to ADD two numbers.

-> Code:

#!/bin/bash

echo "Enter the first number: "

read -r num1

echo "Enter the second number: "

read -r num2

sum=$((num1 + num2))

echo "The sum is: $sum"

Output:

3. Check if a Number is Even or Odd.

-> Code:

#!/bin/bash
echo "Enter a number: "

read number

if (( number % 2 == 0 ))

then

echo "$number is Even"

else

echo "$number is Odd"

fi

Output:

4. Print Numbers from 1 to 10 Using a Loop.

-> Code:

#!/bin/bash

for i in {1..10}; do

echo $i

done

Output:
5. Check if a File Exists.

-> Code:

#!/bin/bash

echo "Enter the file name: "

read filename

if [ -f "$filename" ]; then

echo "File exists."

else

echo "File does not exist."

Fi

Output:

6. Find the Largest of Three Numbers.

->Code

#!/bin/bash

echo "Enter the first number: "

read num1
echo "Enter the second number: "

read num2

echo "Enter the third number: "

read num3

if (( num1 >= num2 && num1 >= num3 ))

then

echo "$num1 is the largest"

elif (( num2 >= num1 && num2 >= num3 ))

then

echo "$num2 is the largest"

else

echo "$num3 is the largest"

fi

Output:

7. Display the Current Date and Time.

->Code

#!/bin/bash

echo "Current date and time: $(date)"

Output:
8. Create a Directory.

->Code

#!/bin/bash

directory_name="Pradipta_3008"

mkdir "$directory_name"

# Check if the directory was created successfully

if [ -d "$directory_name" ]; then

echo "Directory '$directory_name' created successfully."

else

echo "Failed to create directory '$directory_name'."

fi

Output:

9. Check if a String is Palindrome.

->Code

#!/bin/bash

echo "Enter a number:"

read num

original_num=$num

reverse=0

while [ $num -gt 0 ]; do

last_digit=$((num % 10))
reverse=$((reverse * 10 + last_digit))

num=$((num / 10))

done

if [ $original_num -eq $reverse ]; then

echo "$original_num is a palindrome."

else

echo "$original_num is not a palindrome."

fi

Output:

10. Calculate the Factorial of a Number.

->Code

#!/bin/bash

echo "Enter a number:"

read num

factorial=1

if [ $num -lt 0 ]; then

echo "Factorial is not defined for negative numbers."

else

for (( i=1; i<=num; i++ ))

do
factorial=$((factorial * i))

done

echo "The factorial of $num is $factorial."

fi

Output:

You might also like