Assign 2 ND
Assign 2 ND
The directory structure of UNIX, absolute path, relative path, hidden files
Commands---echo, cmp, diff, wc, grep, tail, od, file, du,, sed,awk
1. Create a text file sourcefile using an editor and count the number of
characters,words and lines in it.
3. Copy the above file into another destinationfile and edit the second file by
inserting a few more words, lines in it.
4. Compare sourcefile with destinationfile using cmp and diff and note the
difference between the two commands.
7. How to go back to your home directory from any other place in the directory
structure?
8. How to retrieve the previous Shell commands that you had used in the current
log-in session?
9. Display the calendar of a particular month.
11. Create a executefile using an editor which the following shell commands in
sequence: Who, sort, calendar, cat, grep with appropriate parameters / arguments
( for example cat requires a file name …)
15. Write a shell script which takes a sequence of filenames as arguments and does
the following on each of them:
Display the contents of the file; Count the number of characters, words, and lines
in it; Display only those lines that contains the word “the” in it; Display only the
last “5” lines of it
CODE:
Shsc1.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o1=$(cat "$filename")
echo "The contents of the file are:"
echo $o1
else
echo "File not found"
fi
shsc2.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o2=$(wc "$filename")
echo "The number of words,lines and characters are:"
echo $o2
else
echo "File not found"
fi
shsc3.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o3=$(grep -i "the" "$filename")
echo "The lines having 'the' in the file are: "
echo $o3
else
echo "File not found"
fi
shsc4.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o4=$(tail -5 "$filename")
echo "The last 5 lines of the file are:"
echo $o4
else
echo "File not found"
fi
16. Refine the above script to accept the word to be looked for and the number of
lines to be displayed.
Shsc5.sh:
#!/bin/bash
if [ -r "$filename" ]; then
o1=$(grep -i "$word" "$filename")
o2=$(sed -n "${num}p" "$filename")
echo "The output is:"
echo "$o1"
echo "$o2"
else
echo "File not found"
fi
17. Modify the above script to also display the first ” n” lines of any file.
Shsc6.sh:
#!/bin/bash
if [ -r "$filename" ]; then
if [ "$num" -lt 0 ]; then
o2=$(head "$filename" "$((-num))")
else
o2=$(sed -n "${num}p" "$filename")
fi
1) Create an employee database (.txt file) which contains employee personal record. The
personal record includes name, DOB ( DD/MM/YYYY), address, phone number.
a) Print all Employee record that contain a phone number with an
Extension
e) Print all the Mobile number and the land line number with name
f) Find the eldest and the youngest employee from the record set.
g) Find the age of all the employee.
SHELL SCRIPTS:
Code:
#/bin/bash
read -p "Enter file name:" file
if [ -f "$file" ] ;then
echo "File exists"
else
echo "File does not exist"
fi
2. Display the content of the file if it exists else display an error message.
Code:
#/bin/bash
read -p "Enter file name:" file
if [ -f "$file" ] ;then
cat $file
else
echo "File does not exist"
fi
3. Check whether a number is prime or not.
Code:
#!/bin/bash
read -p "Enter a number:" number
isprime=true
for ((i=2;i<$number;i++))
do
if [ $((number%i)) -eq 0 ]
then
isprime=false
break;
fi
done
if [ "$isprime"=true ]
then
echo "$number is prime"
else
echo "Not a prime number."
fi
4. Factorial of a number.
Code:
#!/bin/bash
fact=1
read -p "enter a number:" num
for ((i=1;i<=$num;i++))
do
fact=$((fact*i))
done
echo "factorial:" $fact
Code:
read -p "Enter a number:" number
sum=0
temp=$number
while [ $temp -gt 0 ]
do
digit=$((temp%10))
sum=$((sum+digit))
temp=$((temp/10))
done
echo "The sum of digits of the number: $sum"
Code:
#!/bin/bash
read -p "Enter a string:" word
reverse=$(echo "$word" | rev )
if [ "$word" == "$reverse" ]
then
echo "The word $word is a palindrome"
else
echo "Not a palindrome."
fi
7. Count the number of characters ,words and lines in it and display each in seperate line.
Code:
#!/bin/bash
read -p "Enter the file name:" file
if [ -f $file ]
then
echo "No of words in the file:"
wc -w <$file
echo "No of characters in the file:"
wc -m <$file
echo "No of lines in the file:"
wc -l <$file
fi
8.modify the above scrip to accept one or more file to command line and repeat the
above.
Code:
#!/bin/bash
for file in "$@"
do
echo "No of words in the file $file :"
wc -w <$file
echo "No of characters in the file $file :"
wc -m <$file
echo "No of lines in the file $file :"
wc -l <$file
done
9. Accept the word as an input. Count and print the no of lines the word is present.
Code:
#!/bin/bash
read -p "Enter the file name:" file
read -p "Enter the word:" word
if [ -f $file ]
then
count=$(grep -c "$word" $file )
echo "The word $word is present in $count lines in the file $file."
else
echo "Error."
fi
10. Write a script to display current path.
Code:
#!/bin/bash
echo "printing the current working directory:"$PWD
11. Shell script that list the files by modification and addition time.
Code:
#!/bin/bash
#!/bin/bash