0% found this document useful (0 votes)
33 views6 pages

Sample Programs - LFD

Uploaded by

Kshitij Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Sample Programs - LFD

Uploaded by

Kshitij Bhatia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Linux for Devices

20 programs on Shell Scripting


1. Read, store and display use input using Bash Script

#!/bin/bash
echo "Please enter some text:"
read user_input
echo "You entered: $user_input"
stored_input=$user_input
echo "Stored input: $stored_input"

2. Concatenating multiple variables


#!/bin/bash
# Declaration of variables
name='My name is Tom.'
age='My age is 12.'
# Concatenation
info="${name} ${age}"
echo "Result: $info"

3. Perform arithmetic operations using shell script


#!/bin/bash

num1=10
num2=20
sum=$(($num1+$num2))
echo "The Sum is: $sum"

4. Generating random number between two given numbers


#!/bin/bash

read -p "Enter minimum range:" min


read -p "Enter maximum range:" max

r_num=$(( $RANDOM % ($max - $min + 1) + $min ))


echo "Random Number: $r_num"

5. Check a number is even or odd


#!/bin/bash

read -p "Enter a number:" num


if [ $((num%2)) == 0 ]
then
echo "The number is even"
else
echo "The number is odd"
fi
6. Perform arithmetic operation based on user input(use conditional statement)
!/bin/bash
read -p "Enter a number:" num1
read -p "Enter a smaller number:" num2
read -p "Enter an operand:" op

if [ $op == + ]
then
echo "$num1 + $num2 = $((num1+num2))"
elif [ $o == - ]
then
echo "$num1 - $num2 = $((num1-num2))"
elif [ $op == * ]
then
echo "$num1 * $num2 = $((num1*num2))"
elif [ $op == / ]
then
echo "$num1 / $num2 = $((num1/num2))”
else
echo "Operator not listed"
fi

7. Perform logical operation based on user input


#!/bin/bash

read -p "Enter two values: " val1 val2


read -p "Enter an operation(and/or/not) to perform:" op

case $op in
and)
if [[ $val1 == true && $val2 == true ]]
then
echo "Result: true"
else
echo "Result: false"
fi;;
or)
if [[ $val1 == true || $val2 == true ]]
then
echo "Result: true"
else
echo "Result: false"
fi;;
not)
if [[ $val1 == true ]]
then
echo "Result: false"
else
echo "Result: true"
fi;;
*) echo "Invalid operator."
Esac
8. To check if a given email is valid or not
#!/bin/bash

read -p "Enter an email ID: " id


if [[ $id =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]
then
echo "This is a valid email ID!"
else
echo "This is not a valid email ID!"
fi

9. To check whether a given URL is valid or not


#!/bin/bash

read -p "Enter a URL: " url


if [[ $url =~ ^(http|https)://[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]
then
echo " This is a valid URL!"
else
echo "This is not a valid URL!"
fi

10. Perform any three string operation


One is given as sample here, perform other two aswell

a) String length
#!/bin/bash

str="My name is Tom!"


len=${#str}
echo "The length of the string is: $len"

11. Print the multiplication table


#!/bin/bash

read -p "Enter a number: " num


for (( i=1; i<=10; i++ ))
do
echo "$num x $i = $((num*i))"
done

12. Sum of digits


#!/bin/bash

read -p "Enter a number: " num


sum=0
while [ $num -gt 0 ]
do
dig=$((num%10))
sum=$((sum+dig))
num=$((num/10))
done
echo "The sum of digits of the given number: $sum"

13. Factorial of number


#!/bin/bash

read -p "Enter a number: " num


temp=1
for (( i=1; i<=$num; i++ ))
do
temp=$((temp*i))
done
echo "The factorial of $num is: $temp"

14. Check whether the entered string is palindeome or not


#!/bin/bash

Palindrome () {

s=$1
if [ "$(echo $s | rev)" == "$str" ]
then
echo "The string is a Palindrome"
else
echo "The string is not a palindrome"
fi
}
read -p "Enter a string: " str
Palindrome "$str"

15. Check whether the number is prime or not


#!/bin/bash

Prime () {
num=$1
if [ $num -lt 2 ]
then
echo "The number $num is Not Prime"
return
fi
for (( i=2; i<=$num/2; i++ ))
do
if [ $((num%i)) -eq 0 ]
then
echo "The number $num is Not Prime"
return
fi
done
echo "The number $num is Prime"
}
read -p "Enter a number: " num
Prime "$num"

16. Fahrenheit to Celsius


#!/bin/bash

Celsius () {
f=$1
c=$((($f-32)*5/9))
echo "Temperature in Celsius = $c°C"
}

read -p "Enter temperature in Fahrenheit:" f


Celsius $f

17. Find the area of any 3 geometrical shape


One is given for ur ref
#!/bin/bash

Area() {
width=$1
height=$2
area=$(($width * $height))
echo “Area of the rectangle is: $area”
}

read -p "Enter height and width of the ractangle:" h w


Area $h $w

18. Prepare a grading system


Grade() {
score=$1
if (( $score >= 80 )); then
grade="A+"
elif (( $score >= 70 )); then
grade="A"
elif (( $score >= 60 )); then
grade="B"
elif (( $score >= 50 )); then
grade="C"
elif (( $score >= 40 )); then
grade="D"
else
grade="F"
fi
echo “The grade for mark $s is $grade”
}

read -p "Enter a score between 1-100:" s


Grade $s

19. Sorting array


#!/bin/bash

arr=(24 27 84 11 99)

echo "Given array: ${arr[*]}"


arr=($(echo "${arr[*]}" | tr ' ' '\n' | sort -n | tr '\n' ' '))
echo "Sorted array: ${arr[*]}"

20. Removing an element from the array


#!/bin/bash

arr=(24 27 84 11 99)

echo "Given array: ${arr[*]}"


read -p "Enter an element to remove: " val
arr=("${arr[@]/$val}")
echo "Resultant array: ${arr[*]}"
___________________________________________________________________________
__

Descriptive

Installation procedure of Linux


Any 10 unix/linux commands with explanation

You might also like