Question

In: Computer Science

Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...

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.

Solutions

Expert Solution

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:


Related Solutions

Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Write a bash shell script that takes exactly one argument, a file name. If the number...
Write a bash shell script that takes exactly one argument, a file name. If the number of arguments is more or less than one, print a usage message. If the argument is not a file name, print another message. For the given file, print on the screen its content.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a bash script file that tests each file entry in the current directory. It should...
Write a bash script file that tests each file entry in the current directory. It should determine if it is a file or directory. If it is a file, it will determine if it is readable or not. If it is readable, it will display only the first 4 lines to the terminal in sorted order (just sort the first 4 lines). If it is a directory, it should display the contents of that directory (the files and subdirectories). NOTE:...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
how to create a script file on puTTy script pp1.txt
how to create a script file on puTTy script pp1.txt
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT