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

Linux and Shell Scripting

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 views

Linux and Shell Scripting

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/ 22

lOMoARcPSD|36955333

Linux-LAB Manual

B.Sc. Computer Science (Bharathiar University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Elango ([email protected])
lOMoARcPSD|36955333

CORE LAB 5: LINUX AND SHELL PROGRAMMING LAB

1. Simulate the file commands : rm, cp, mv, diff, split, cat, cmp, wc.

AIM
To simulate the file commands : rm, cp, mv, diff, split, cat, cmp, wc.

ALGORITHM
STEP 1: Start the terminal.
STEP 2: Open the editor → gedit and type the commands necessary for the program.
STEP 3: Use rm command to delete or remove a file.
STEP 4: Use cp command to copy the contents of a file.
STEP 5: Use diff command to display the difference in two files.
STEP 6: Use mv command to move the contents of one file to another.
STEP 7: Use split command to split the files based on the instruction given.
STEP 8: Use cat command to concatenate two files and display their contents.
STEP 9: Use cmp command to compare any two files.
STEP 10: Use wc command to count the number of words in a file and display it.
STEP 11: Compile and run the program.
PROGRAM
touch file1.txt file2.txt file3.txt file4.txt file5.txt
echo "computer" > file1.txt
echo "laptop" > file2.txt
echo "computer laptop" > file3.txt
echo "computer laptop
device" > file4.txt
echo "computer laptop
electronic devices" > file5.txt
rm -rf file1.txt
echo "file1 successfully deleted"
echo "---------------------------------"
cp file2.txt file3.txt
echo "file2 successfully copied to file3"
echo "---------------------------------"

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

echo "Content of file3"


cat file3.txt file4.txt
echo "---------------------------------"
echo "Total No.of lines in a file3"
wc file5.txt
echo "---------------------------------"
echo "Compare two files file4 and file5"
diff file4.txt file5.txt
echo "---------------------------------"
echo " Split the file in terms of lines"
echo "---------------------------------"
split -l 1 file5.txt
echo "---------------------------------"
echo "file moved to directory"
mkdir fi
mv -f file5.txt fi
echo "---------------------------------"
cmp file3.txt file4.txt
OUTPUT

RESULT
The above shell script is verified and executed successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

2. Script to show the system configuration.

AIM:
To write a Linux and Shell Program to Script to show system configuration.
ALGORITHM:
Step-1 : Open the terminal.
Step-2 : Open text editor -> gedit -> .Save the file with .sh extension.
Step-3 : Type the necessary commands.
Step-4 : Type command “whoami” is used to display the current login user.
Step-5 : Type command “SHELL” is used to display the current shell.
Step-6 : Type command “HOME” is used to display home directory.
Step-7 : Type command “uname” is used to display the operating system type
Step-8 : Type command “PATH” is used to display the current path setting.
Step-9 : Type command “pwd” is used to display the current working directory.
Step-10 : Type command “who” is used to display the currently logged number of users
and all available shells.
Step-11 : Type command “lscpu” is used to display the cpu information.
Step-12 : Type command “free” is used to display the memory information.
Step-13 : Compile and run the program.
Step-14 : Stop the process.

PROGRAM

echo “current login user” $(whoami)


echo “-------------------------------------”
echo “current shell” $SHELL
echo “-------------------------------------”
echo “OS type” $(uname)
echo “-------------------------------------”
echo “home directory” $HOME
echo “-------------------------------------”
echo “current path setting” $PATH
echo “-------------------------------------”
echo “current working directory” $(pwd)

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

echo “-------------------------------------”
echo “current logged number of users and all available shells” who
echo “-------------------------------------”
echo “cpu information” $(lscpu)
echo “-------------------------------------”
echo “show memory information” $(free)
echo “-------------------------------------”

OUTPUT:

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

RESULT :
The above program is verified and executed successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

3.Script to implement the following: Pipes, Redirection and Tee Commands.


AIM:
To write a Linux and Shell Program to Script to Implement Pipes, Redirection and Tee
Commands.
ALGORITHM:
STEP 1:Open the Terminal.
STEP 2:Open the text editor-gedit.Save the file with .sh extension.
STEP 3:Type the commands necessary for the script.
STEP 4:Use piping functions for both input and output functions.
STEP 5:Use redirect symbol (>) to formulate redirection in file job1.sh
STEP 6:Rediection is done for correct output,error output and all output.
STEP 7:Use tee command to copy data from input as well as output to each file.
STEP 8:Compile and run the program.
STEP 9:Stop the process.

PROGRAM:
echo " Pipe Symbol Connect one or more process"
ls -1 *.sh | wc -l
echo "Redirect the output"
echo "------------------------------------"
echo "Redirect correct output to newfile"
sh job1.sh > correctoutput
echo "------------------------------------"
echo "Redirect error output to newfile"
sh job1.sh 2> erroroutput
echo "------------------------------------"
echo "Redirect all output to newfile"
sh job1.sh &> alloutput
echo "------------------------------------"
echo "tee copies data from standard input to each FILE, and also to standard output"
ls -1 *.txt | wc -l | tee count.txt

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

OUTPUT:

RESULT:
The above program to to Script to Implement Pipes, Redirection and Tee Commands is
executed and verified Successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

4.Script to display current date,username ,list of files by getting user choice.


AIM
To write a linux and shell program to display current date, username ,list of files by getting
user choice.
ALGORITHM
STEP 1: Start the terminal.
STEP 2:Open the editor →gedit and type the commands necessary for the program.
STEP 3:PS3 is used for selecting value.
STEP 4:Use $ {options[@]} to get options.
STEP 5:Use $(date) to display current date.
STEP 6:Use $(whoami) to display user name.
STEP 7:Use $(ls) to list files and directories.
STEP 8:The esac keyword is used to end case.
STEP 9:Compile and run the program.
PROGRAM
PS3='------------------------
Please enter your choice: '
options=("Option 1""Option 2""Option 3""Quit")
echo "------------------------"
echo "------------------------"
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "Date is " $(date)
;;
"Option 2")
echo "Your username is "
echo $(whoami)
;;
"Option 3")
echo "To list all files & diretories"
echo $(ls)
;;

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

Quit")
break
;;
*) echo invalid option;;
esac
done

OUTPUT

RESULT
The above shell script is verified and executed successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

5.Script to Implement the Filter Commands

AIM :
To write a linux and shell program to script to implement filter commands.

ALGORITHM:
Step-1 : Open the terminal.
Step-2 : Open text editor -> gedit -> .Save the file with .sh extension.
Step-3 : Type the necessary filter commands.
Step-4 : Type command “cut” is used to extract specified column or character from a
file.
Step-5 : “wc” is used to count number of lines or words or characters.
Step-6 : “cat” is used to concatenate two files.
Step-7 : “grep” filter is used to search a file from specified pattern of characters and
display line.
Step-8 : “tr” command is used to convert the content of a file from lower case to upper
case.
Step-9 : Compile and run the program.
Step-10 : Stop the process.

PROGRAM:
touch f1.txt f2.txt f3.txt
echo "welcome to newyork " > f1.txt
echo "hiii hlooo" > f2.txt
echo "Sri Krishna Adithya College OfArts And Science" > f3.txt
echo "hiii.....
how r u....
hii...i m fine" > f4.txt
cat f1.txt f2.txt
echo "cat filter is implemented"
wc -l f3.txt
echo "wc filter is implemented"
grep -n "hii" f4.txt
echo "grep filter is implemented"
cut -c 1-4 f4.txt

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

echo "cut filter is implemented"


tr "[:lower:]" "[:upper:]" < f3.txt
echo "tr filter is implemented"
OUTPUT:

RESULT :
The above program is verified and executed successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

6.SCRIPT TO REMOVE FILES WHICH HAS FILE SIZE AS ZERO BYTES

AIM :
To write a linux and shell program to script to remove files which has size as zero
bytes.
ALGORITHM :
Step-1 : Open the terminal.
Step-2 : Open text editor -> gedit -> .Save the file with .sh extension.
Step-3 : Type the necessary filter commands.
Step-4 : Create a file which has a size as zero bytes.
Step-5 : Read the file using read command.
Step-6 : Use rm command to remove files.
Step-7 : fi statement is the fundamental control statement that allows shell to make
decision.
Step-8 : Compile and run the program.
Step-9 : Stop the process.

PROGRAM :

echo "Enter any file name: "


read filenm
if [ -e $filenm ]
then
echo $filenm" File exist"
if [ -s $filenm ]
then
echo $filenm" File has size > 0" else
rm $filenm
echo $filenm" File is Deleted which has size = 0"
fi
else
echo "File not exist"
fi

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

OUTPUT:

RESULT:
The above program is verified and executed successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

7.SCRIPT TO FIND THE SUM OF THE INDIVIDUAL DIGITS OF A GIVEN


NUMBER

AIM :
To write a linux and shell program to find the sum of the digits of a given number.

ALGORITHM:

Step-1 : Open the terminal.


Step-2 : Open text editor -> gedit -> .Save the file with .sh extension.
Step-3 : Type the necessary commands in script.
Step-4 : Use three variables such as temp,sd,sum.
Step-5 : Use start() as calling function.
Step-6 : Use basic concept of sum of digits to addd individual digits.
Step-7 : Use two while loops to reduce sum to individual digit.
Step-8: Interpret and run the program.
Step-9 : Stop the process.

PROGRAM:

echo "Enter a Number:"


read n

temp=$n
sd=0
sum=0
start()
{
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
sum=$(( $sum + $sd ))

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

done
}

while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
sum=$(( $sum + $sd ))
done
if [ $sum -gt 9 ]
then
n=$sum sum=0
start
fi
echo "sum of digits " $sum

OUTPUT:

RESULT :
The above program is verified and executed successful

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

8.SCRIPT TO FIND THE GREATEST AMONG THE GIVEN SET OF NUMBERS


USING COMMAND LINE ARGUMENTS.

AIM :
To write a linux and shell program to find the greatest among the given set of
numbers using command line arguments.
ALGORITHM:
Step-1 : Open the terminal.
Step-2 : Open text editor -> gedit -> .Save the file with .sh extension.
Step-3 : Type the necessary commands in script.
Step-4 : Use read to get the size of n array.
Step-5 : Use for loop and do loop to calculate the given number.
Step-6 : Use elif command to calculate the smallest and greatest number.
Step-7 : Use separate variable to print the result.
Step-8: Interpret and run the program.
Step-9 : Stop the process.

PROGRAM:

echo "enter size of an array"


read n
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

do
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done
echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"
OUTPUT:

RESULT :

The above program is verified and executed successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

9.SCRIPT FOR PALINDROME CHECKING.

AIM :
To write a linux and shell program to script for palindrome checking.

ALGORITHM:

Step-1 : Open the terminal.


Step-2 : Open text editor -> gedit -> .Save the file with .sh extension.
Step-3 : Type the necessary commands in script.
Step-4 : The variable such as s, temp, rvs is used.
Step-5 : The read is used to get string or number.
Step-6 : The given input is assumed to temp.rev is used to reverse the given string or
number.
Step-7: The result is stored in rvs.
Step-8: If given input is equal to reversed value then the given input is palindrome.
Step-9: If it fails the given input is not palindrome.
Step-10: Interpret and run the program.
Step-11 : Stop the process.

PROGRAM:
echo "Enter the string/number"
read s
echo $s>temp
rvs="$(rev temp)"
if [ $s = $rvs ]
then
echo "It is a palindrome"
else
echo "It is not a palindrome"
fi

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

OUTPUT:

RESULT :

The above program is verified and executed successfully.

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

10.SCRIPT TO PRINT THE MULTIPLICATION TABLE OF THE GIVEN


ARGUMENT USING FOR LOOP .

AIM :
To write a linux shell program to print the multiplication table of the given argument
using for loop.

ALGORITHM:

Step-1 : Open the terminal.


Step-2 : Open text editor -> gedit -> .Save the file with .sh extension.
Step-3 : Type the necessary commands in script.
Step-4 : Use read to get necessary input value as number.
Step-5 : read r is used to given number of multiples.
Step-6 : for loop is used to get multiples one by one as it is incremented by 1.
Step-7: “$i * $n=` expr$i \* $n`” used to display multiplication table.
Step-8: Interpret and run the program.
Step-9 : Stop the process.

PROGRAM:

echo "Enter a Number"


read n
echo "Enter the number of multiples"
read r
for((i=1;i<=r;i++))
do
echo "$i*$n =`expr $i \* $n`"
done

Downloaded by Elango ([email protected])


lOMoARcPSD|36955333

OUTPUT:

RESULT

The above program is verified and executed successfully.

Downloaded by Elango ([email protected])

You might also like