In: Computer Science
Create a bash script file named assessment-script-a that:
1. Accepts any number of file names on the command line
2. For each file name provided,
delete any line that contains the string:
qwe.rty
When the changes are completed, the script should display the
total number
of files scanned, and the total number of files changed.
Example Command:
assessment-script-a file.a file.b file.c file.d file.e
Example Output:
5 files scanned, 3 files changed
3. Your script should include a series of comments that
fully
describes your script, including the purpose of each line.
You
may use a separate comment block or include the comments on
each line of code.
Email 2
Subject: Shell Scripting Assessment B
File: assessment-script-b
Create a bash script file named assessment-script-b that:
1. Accepts NO command line parameters or options
2. Kills all processes owned by the userid that is running
the
script, EXCEPT for
(a) The login shell for the session executing the script
(b) The ancestors of the login shell for the session executing the
script
(c) The processes running or controlled by the script
This is a 'clean up' script that likely will be useful in
future
classes to kill "stray processes". You don't want the script
to
kill your current session - hence the (a) and (b) requirements -
nor do
you want the script to kill itself - hence the (c)
requirement.
You DO want the script to kill all other processes associated
with
your userid.
One easy way to test your script is to start a number of
background
jobs, and make sure your script kills the background jobs.
Another
way to test your script is to start a second login session, and
make
sure your script kills the second session.
Your script does not need to produce any output.
3. Your script should include a series of comments that fully
describes your script, including the purpose of each line.
You
may use a separate comment block or include the comments on
each line of code.
Please find the following two shell scripts.
Program 1:
#!/bin/bash
#variable to count file count
totalcount=0
#count modified files
filecount=0
#check the number of cmd line args
if [ $# == 0 ]
then
#print the usage
echo "Usage: $0 file1 file2 . . "
exit
fi
#traverse the list of input files
for file in $*
do
#increase the file count
totalcount=$((totalcount+1))
#variabel to check whether the file has pattern
exp=
exp=`grep -c "qwe\.rty" $file`
#if file has any pattern
if [ $exp -ne "0" ]
then
#increase the modified count
filecount=$((filecount+1))
#remove the pattern from file
grep -v "qwe\.rty" $file >tmp_$$
#move the file to original file name
mv tmp_$$ $file
fi
done
#print the statistics of file updates
echo "$totalcount files scanned, $filecount files
changed"
Output:
USER>./assessment-script-a as gh
2 files scanned, 1 files changed
USER>
USER>
USER>cat as
dsfsd
f
ds
fsd
USER>cat gh
USER>
Screen Shot:
Program 2:
#!/bin/bash
#set -x
#get the current process pid
curPid=$$
#assign current pid to pid_list
pid_list=$curPid
#function to list all the
ancestorsPids()
{
#recursive call until we reach ppid as 0
if [[ $1 -ne 0 ]]
then
#print the ppid for pid
ppid=`ps -ef| grep ^$USER | grep $1 |awk -v pid="$1" '/./ {if($2 ==
pid) print $3}'`
#append the ppid to pid_list
pid_list="$pid_list $ppid"
#recursive call to find the ppid
ancestorsPids $ppid
fi
}
#invoe the function to collect ppid
ancestorsPids $curPid
#for loop to all the pid of a user
for i in `ps -ef| grep ^$USER | awk '/./{print $2}'`
do
found=0
#if the pid is found in ancestor list we ignore it dont kill
it
for j in $pid_list
do
if [ $i == $j ]
then
#its a ancestor pid do kill it
found=1
break
fi
done
#this pid of a user is not a ancestor, so kill it
if [ $found -eq 0 ]
then
#kill the process of a user
kill -9 $i 2>/dev/null
fi
done
Output:
Note: I have ran three process in back ground and killed all the three.
USER>
USER>./while.sh &
[1] 3726
USER>log
USER>
USER>./while.sh &
[2] 3728
USER>log
USER>
USER>log
USER>./while.sh &
[3] 3731
USER>log
USER>
USER>log
USER>
USER>log
./assessment-script-b log
[1] Killed ./while.sh
[2]- Killed ./while.sh
[3]+ Killed ./while.sh
USER>
Screen Shot: