d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 500 Fg For Easy Bash Scripting!
Add Reply New Topic New Poll
Member
Posts: 30,556
Joined: Jun 20 2014
Gold: 1,500.00
Apr 17 2017 07:03pm
This is a beginner Linux class. I am just struggling with this assignment here. To receive the Fg the shell script must work and complete all the tasks below!

Whoever posts here first with the correct information will get the fg.



Scenario: You are the system administrator for a Unix/Linux system. Your users are primarily basic users. As you know, the ‘cp’ command allows you to copy multiple files to a single directory, such as:

cp abc xyz file1 file2 /tmp

This example command will copy the files abc, xyz, file1, file2 to the /tmp directory. Users on your system want the ability to copy a single file or directory to multiple locations. You need to write a script to accomplish this action because the ‘cp’ command does not allow you to do this. The command shell you will write looks like:

copyall file /tmp /usr/local/tst /home/test/1

Note that the above is just an example; you should not code the shell script with these exact parameters. In this example, the file ‘file’ will be copied to the directories /tmp, /usr/local/tst, and /home/test/1. Thus, a single file may be copied to multiple destinations. The output of this shell script should look like the following:

copyall: File file was copied to /tmp /usr/local/tst /home/test/1 on Wed Nov 9 23:07:08 CST 2011 by george123

In addition, the above line should be added (appended) to the file /tmp/copyall.

You should use the date and time that the copy was attempted; the above is just an example. The ‘george123’ should be replaced by the user running the command.

The specifics of the shell script are noted below:

1. The shell script must run as either a ksh or bash shell script; no other scripting languages are allowed.
2. If no parameters are given to the ‘copyall’ then the shell must print/echo a usage message explaining what is required to execute the shell
3. The first parameter of the shell script must be a file and it must exist. It must also be readable. If it is not, the shell script should exit with a status of 99.
4. If the shell script executed correctly it should exit with a status of ‘0’.
5. Under no circumstances should the file /tmp/copyall be overwritten; you add information to the file at the end.
6. Use the ‘echo’ command rather than the built-in ‘print’ command.
7. Put a series of comments at the top of the shell script indicating who wrote the shell script and the date it was written. Put a series of comments explaining what the shell script is supposed to do. Therefore, you name must be in the shell script itself.
8. All parameters after the first one must be directory targets, must be writable, and must exist. If any targets fail these checks print an appropriate error message and exit with a status of 99.
9. For full credit, when the shell script is run it must not produce any warning or error messages from the script interpreter.
10. Partial credit will be given based on how much the shell script does what was required. If nothing is turned in then no credit will be given.
11. The shell script is not designed to be interactive; do not ask the user for any information.
12. Make sure the permissions on your shell file are set so that they are private to you.
13. Make no assumptions about the number of targets; you need at least one but there can be any number of targets to copy the file to.

This post was edited by Biased on Apr 17 2017 07:03pm
Member
Posts: 30,556
Joined: Jun 20 2014
Gold: 1,500.00
Apr 17 2017 07:39pm
To make this even easier, these are the steps to take in English - now I just need these converted into actual code:

1.
Create a file
#Comments
exit 0
chmod 700 ____________ (file)
(echo $?)

2.
Check number of parameters
if p < 2
echo Usage $0 file dir1 dir2 dir3 (name of shell script)
exit 99

3.
Test $1 = file + readable (chmod 700)
If all_ok go to step 4
Print error
Exit 99

4.
Shift > $* > directories

5.
Loop thru $*

6.
Check for dir
Writeable
‘Cp’ command

7.
Print audit message

8.
Go to step 5

9.
End
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 17 2017 09:54pm
I advise you to run the script through your own tests in case I missed something. I tested with a non existent file, a file chmod 000, a non existent directory, a directory chmod 000. All test cases were caught and handled.

Code
abduct@Nymph ~/school $ cat script.sh
#!/bin/bash
#Written by: abduct
#Written on: Mon Apr 17 19:16:31 2017
#This script takes a single file and copies it to many locations in a one to many relation

#Set log directory
LOG_FILE="/home/abduct/school/copyall.log"

function echo_and_log {
echo "(`date`)[$USER] copyall: $@"
echo "(`date`)[$USER] copyall: $@" >> $LOG_FILE
}

#Check required paramters, if they are unmet display usage
if [ $# -lt 2 ]; then
echo "Usage: $0 file ./directory ./directory2 ..."
exit -1
fi

#Check to see if the file exists and is a regular file, as well as if the file is readable
#by the user
if [ ! -f "$1" ] || [ ! -r "$1" ]; then
echo_and_log "$1 is not a file or you do not have read permissions for this file"
exit 99
fi

#Iterate all the directories and check if the directory exists and if we have write
#permissions
for i in ${@:2}
do
if [ ! -d "$i" ] || [ ! -w "$i" ]; then
echo_and_log "$i is not a directory or you do no have write permissions for this directory"
exit 99
fi
done

#Iterate through the argument glob skipping the first paramter (the file to be copied)
#This allows us to use $1 (first argument holding the file name) as well as $i
#(the directory name which changed each interation) as arguments to the cp binary
for i in ${@:2}
do
cp "$1" "$i"
done

#Display the result of the copy as well as store this result in our logfile
echo_and_log "File $1 was copied to ${@:2}"

exit 0


Code
abduct@Nymph ~/school $ cat copyall.log
(Mon Apr 17 19:46:59 2017)[abduct] copyall: File test was copied to ./a ./b


This post was edited by AbDuCt on Apr 17 2017 10:16pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll