B.Sc Computer Science
Linux and Shell Programming - Lab
Bharathiar University
Program 1- Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.
Source Code
# Create sample files
echo "Welcome all of you." > f1.txt
echo "Thanks for coming" > f2.txt
# Display the contents of file 1
echo "Contents of file 1:"
cat f1.txt
echo
# Display the contents of file 2
echo "Contents of file 2:"
cat f2.txt
echo
# Copy file 1 to file3
cp f1.txt f3.txt
echo "file 1 copied to file 3."
# Display the contents of file 3
echo "Contents of file 3:"
cat f3.txt
echo
# Rename file 3 to sample.txt
mv f3.txt sample.txt
echo "file 3 renamed to sample.txt."
# Display the contents of sample.txt
echo "Contents of sample.txt:"
cat sample.txt
echo
# Compare file 1 and sample.txt
cmp -s f1.txt sample.txt
if [ $? -eq 0 ]; then
echo "file 1 and sample.txt are identical."
else
echo "file 1 and sample.txt are different."
fi
echo
# Display the word count of file 1
echo "Word count of file 1: $(wc f1.txt)"
echo
# Split file 1 into two parts
split -n 2 f1.txt file_
echo "file 1 split into two parts: file_aa and file_ab."
echo "Contents of file_aa:"
cat file_aa
echo
echo "Contents of file_ab:"
cat file_ab
echo
# Display the differences between file1.txt and file2.txt
diff f1.txt f2.txt
echo
# Clean up: Remove temporary files
rm f1.txt f2.txt sample.txt file_aa file_ab
echo "Temporary files removed."
Sample OUTPUT:
No comments:
Post a Comment