0% found this document useful (0 votes)
10 views9 pages

Unix/Linux Shell Scripting Examples

lab

Uploaded by

rs24ec01
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)
10 views9 pages

Unix/Linux Shell Scripting Examples

lab

Uploaded by

rs24ec01
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

OS ASSIGNMENT 2

Implement basic shell scripting concepts used in Unix/Linux


environments
1. Write a shell script that accepts a single character as input and
determines whether it is:
(i) a lowercase alphabet
(ii) an uppercase alphabet
(iii) a digit
(iv) a special symbol
(v) a vowel

CODE:
#!/bin/bash
echo "Hello World"
echo "enter character"
read ch
echo $ch
if [[ $ch =~ [AEIOU] ]]; then
echo "You entered an upper case vowel"
elif [[ $ch =~ [aeiou] ]]; then
echo " You entered a lower case vowel"
elif [[ $ch =~ [A-Z] ]]; then
echo "You entered an upper case alphabet"
elif [[ $ch =~ [a-z] ]]; then
echo "You entered a lower case alphabet"
elif [[ $ch =~ [0-9] ]]; then
echo "You entered a digit"
else
echo "You entered a symbol"
Fi
[Link] a shell script utilizing the case .. esac control structure to
perform the following tasks:
(i) Find and display the number of users currently logged into the
system
(ii) Print the calendar for the current year
(iii) Display the current date and time

CODE:
echo "Choose an option:"
echo "1. Number of users currently logged in"
echo "2. Calendar for the current year"
echo "3. Current date and time"
read -p "Enter your choice (1-3): " choice

case $choice in
1)
echo "Number of users currently logged in:"
who | wc -l
;;
2)
echo "Calendar for the current year:"
gnome-calendar
;;
3)
echo "Current date and time:"
date
;;
*)
echo "Invalid choice. Please enter 1, 2, or 3."
;;
esac

4. Write a shell script to count and display the number of files present
within a specified directory.

CODE:
[Link] a shell script to check whether the specified path refers to
a directory or not.

CODE:
read -p "Enter the path to check: " path

if [ -d "$path" ]; then
echo "Yes, '$path' is a directory."
else
echo "No, '$path' is NOT a directory."
fi
4. Write a shell script to count and display the number of files present
within a specified directory.

CODE:
#!/bin/bash
read -p "Enter the path :" path
cd "$path"
ls -l|grep ^d|wc -l

5. Write a shell script to copy the contents of one file to another file.
Code:
#!/bin/bash
read -p "Enter the path to copy" path1
echo
read -p "Enter the path to paste" path2
echo
cp -r "$path1" "$path2"
6. Write a shell script to accept two numbers as command-line arguments
and display their sum.

Code:
#!/bin/bash
read -p "Enter the first number:" n1
read -p "Enter the second number:" n2
sum=$((n1+n2))
echo $sum

7. Write a shell script to determine the largest number among three values
supplied as command-line arguments.
Code:
#!/bin/bash
read -p "Enter first number: " n1
read -p "Enter second number: " n2
read -p "Enter third number: " n3
if [[ $n1 -gt $n2 && $n1 -gt $n3 ]];
then
echo "First number is greatest"
elif [[ $n2 -gt $n3 && $n2 -gt $n1 ]];
then
echo "Second number is greatest"
elif [[ $n3 -gt $n2 && $n3 -gt $n1 ]];
then
echo "Third number is greatest"
else
echo "Invalid Input"
Fi
8. Write a shell script to implement a simple calculator that performs
basic arithmetic operations (+, −, ×, ÷, %) using command-line arguments.
Code:
#!/bin/bash
echo ".......Basic Calculator......."
read -p "Enter first number :" n1
read -p "Enter second number :" n2
echo "Operation Menu: "
echo "1) Addition"
echo "2) Subtraction"
echo "3) Multiplication"
echo "4) Division"
echo "5) Modulus"
read -p "Enter operation :" op
case $op in
1)
result=$((n1+n2));;
2)
result=$((n1-n2));;
3)
result=$((n1*n2));;
4)
result=$((n1/n2));;
5)
result=$((n1%n2));;
*)
echo "Invalid Input"

esac

echo "$result"

You might also like