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

Shell Scripting

The document outlines 10 basic shell scripting examples, including scripts for printing 'Hello, World!', adding two numbers, checking if a number is even or odd, and finding the largest of three numbers. It also covers creating directories, checking file existence, displaying the current date and time, checking for palindromes, and calculating factorials. Each example includes the corresponding bash code for implementation.

Uploaded by

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

Shell Scripting

The document outlines 10 basic shell scripting examples, including scripts for printing 'Hello, World!', adding two numbers, checking if a number is even or odd, and finding the largest of three numbers. It also covers creating directories, checking file existence, displaying the current date and time, checking for palindromes, and calculating factorials. Each example includes the corresponding bash code for implementation.

Uploaded by

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

10 basic shell scripting

1. Hello World Script.


=>
Code : #!/bin/bash
echo "Hello, World!"

2. Script to Add Two Numbers.


=>
Code :
#!/bin/bash
echo "Enter the first number: "
read num1
echo "Enter the second number: "
read num2
sum=$((num1 + num2))
echo "The sum is: $sum"

3. Check if a Number is Even or Odd.


=>
Code :
#!/bin/bash
echo "Enter a number: "
read num
if [ $((num % 2)) -eq 0 ]; then
echo "$num is even."
else
echo "$num is odd."
fi
4. Print Numbers from 1 to 10 Using a Loop.
=>
Code :
#!/bin/bash
for i in {1..10}; do
echo $i
done

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

6. Find the Largest of Three Numbers.


=>
Code : #!/bin/bash
echo "Enter three numbers: "
read num1
read num2
read num3
if [ $num1 -ge $num2 ] && [ $num1 -ge $num3 ]; then
echo "$num1 is the largest."
elif [ $num2 -ge $num1 ] && [ $num2 -ge $num3 ];
then
echo "$num2 is the largest."
else
echo "$num3 is the largest."
fi
7. Display the Current Date and Time.
=>
Code :
#!/bin/bash
echo "Current date and time: $(date)"

8. Create a Directory.
=>
Code :
#!/bin/bash
echo "Enter directory name to create: "
read dir_name
mkdir -p $dir_name
echo "Directory '$dir_name' created successfully.”

9. Check if a String is Palindrome.


=>
Code :
#!/bin/bash
echo "Enter a string: "
read str
if [ "$(echo $str | rev)" == "$str" ]; then
echo "$str is a palindrome."
else
echo "$str is not a palindrome."
fi

10. Calculate the Factorial of a Number.


=>
Code :
#!/bin/bash
echo "Enter a number: "
read num
factorial=1
for (( i=1; i<=num; i++ )); do
factorial=$((factorial * i))
done
echo "The factorial of $num is: $factorial”

You might also like