OS Assignment-03
OS Assignment-03
#!/usr/bin/bash
fcount=0
for file in *
do
if [ -f $file -a -x $file ]
then
fcount=$(($fcount + 1))
ls -l $file
fi
done
echo "Total executable files = $fcount"
Q2. Write a shell program to display all the files starting with .bash and available in your home
directory.
#!/usr/bin/bash
cd
for file in *.sh
do
ls -l $file
done
Q3. Write a shell program to recursively remove all the empty files from a given directory.
#!/usr/bin/bash
for file in *
do
find -empty -type d -delete
find -empty -type f -delete
done
Q4. Write a shell program to display all the child processes for a given parent process id.
#!/usr/bin/bash
read -p "Enter PPID: " p
echo "PID of all child elements"
pgrep -P $p
Q5. Write a shell program to fetch the data of a particular student (based on the row information,
i.e., ex. The student present in the 3rd row of the file) from a stored file. Then calculate and
display his total mark and the aggregate percentage secured in a semester. The file contains
student roll no, name , and marks of five differ subjects in a semester.
#!/usr/bin/bash
read -p "Enter file name (filename.txt): " filename
if [ -f $filename ]
then
read -p "Enter row number: " row
cat $filename | head -n $row | tail -n 1 | rev | cut -f 1-5 | rev > res.txt
total=0
for word in `cat res.txt`
do
(( total += word ))
done
per=`expr $total / 5`
echo "Total marks scored: $total"
echo "Aggregate percentage: $per%"
else
echo "File doesn't exist."
fi
Q6. Write a shell program to take the input of a file name and check whether the file exists or
not. If the file exists then display the last column of every record of the file (Assume that no.
of columns in each record of the file may vary) in ascending order.
#!/usr/bin/bash
read -p "Enter file name (filename.txt): " filename
if [ -f $filename ]
then
sed -ri '/\s+$/s///' $filename
cat $filename | rev | cut -f1 | rev | sort
else
echo "File doesn't exist."
fi