0% found this document useful (0 votes)
32 views3 pages

OS Assignment-03

The document contains 6 questions asking to write shell programs to perform tasks like displaying executable files in a directory, files starting with .bash, recursively removing empty files, displaying child processes of a given PID, fetching data of a particular student from a file, and displaying the last column of each record in a file if it exists.

Uploaded by

780ANAY GUPTA
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)
32 views3 pages

OS Assignment-03

The document contains 6 questions asking to write shell programs to perform tasks like displaying executable files in a directory, files starting with .bash, recursively removing empty files, displaying child processes of a given PID, fetching data of a particular student from a file, and displaying the last column of each record in a file if it exists.

Uploaded by

780ANAY GUPTA
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/ 3

OS Lab Assignment-03

ANAY GUPTA | 2005780


Q1. Write a shell program to display the name and count of all executable files in the given directory.

#!/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

You might also like