Linux and Shell Scripting
Linux and Shell Scripting
Linux-LAB Manual
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 "---------------------------------"
RESULT
The above shell script is verified and executed successfully.
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 “-------------------------------------”
echo “current logged number of users and all available shells” who
echo “-------------------------------------”
echo “cpu information” $(lscpu)
echo “-------------------------------------”
echo “show memory information” $(free)
echo “-------------------------------------”
OUTPUT:
RESULT :
The above program is verified and executed successfully.
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
OUTPUT:
RESULT:
The above program to to Script to Implement Pipes, Redirection and Tee Commands is
executed and verified Successfully.
Quit")
break
;;
*) echo invalid option;;
esac
done
OUTPUT
RESULT
The above shell script is verified and executed successfully.
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
RESULT :
The above program is verified and executed successfully.
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 :
OUTPUT:
RESULT:
The above program is verified and executed successfully.
AIM :
To write a linux and shell program to find the sum of the digits of a given number.
ALGORITHM:
PROGRAM:
temp=$n
sd=0
sum=0
start()
{
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
sum=$(( $sum + $sd ))
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
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:
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 :
AIM :
To write a linux and shell program to script for palindrome checking.
ALGORITHM:
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
OUTPUT:
RESULT :
AIM :
To write a linux shell program to print the multiplication table of the given argument
using for loop.
ALGORITHM:
PROGRAM:
OUTPUT:
RESULT