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

Create an executable bash script named sync. You may find file name pattern matching and the...
Create an executable bash script named sync. You may find file name pattern matching and the basename command useful. In fact, the sync-skeleton file contains part of the solution to this problem. You may choose to use it anyway you wish. You'll have to create your own test directories (using the mkdir command) and files (using the touch command) to test this script. You should research the options to the cp command in its main page. The script takes TWO...
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.
Create a Python file num_utils.py that includes: A function sum_of_nums that accepts 1 or more number...
Create a Python file num_utils.py that includes: A function sum_of_nums that accepts 1 or more number parameters and returns their sum without displaying anything A function product_of_nums that accepts 1 or more number  parameters and returns their product without displaying anything A function average_of_nums  that accepts 1 or more number  parameters and returns their average without displaying anything Create a Python file test_num_utils.py that: Imports num_utils Defines a function test_num_utils that demonstrates the use of the utilities in num_utils.py Executes test_num_utils
Create a Python file named num_sum.py that contains: The definition of two functions: volume - accepts...
Create a Python file named num_sum.py that contains: The definition of two functions: volume - accepts three parameters and returns the product of them, but displays nothing sum_of_nums - accepts 1 or more numbers and returns their sum, but displays nothing A print function call that includes a call to volume, passing 2, 3, and 4 A print function call that includes a call to sum_of_nums, passing 1, 2, 3, 4, and 5
Create, and write, three people's names into an external file named "names.txt."  You should overwrite the file...
Create, and write, three people's names into an external file named "names.txt."  You should overwrite the file if it already exists. Read those three people's names into the program from the text file and print them out to the end user's console. Append three more people's names to the external file named "names.txt." Read those six people's names from the text file into the program and print them out to the end user's console. Then try: Get the filename from the...
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT