Script Help

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Script Help
# 1  
Old 10-31-2010
Script Help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

I need some herlp with the following requirements for a script:

Write a single script file that accomplishes the following tasks:
1. If invoked without any parameters it must echo a Usage message.
2. For every parameter in the script call, determine:
a. If the parameter is a file calculate the number of lines in that file and store the result in a file called ‘file.result’.
b. If the parameter is a directory, calculate the number of files in that directory (hint: use ‘ls -1’) and store the result in ‘directory.result’.
c. Both of these files must contain all of the results for all parameters, i.e., you cannot just keep the last file or directory information.
d. If the parameter is neither a file nor a directory, print an error message indicating the parameter is not readable/searchable.
e. If all parameters were successfully processed the script must exit with a status of 0.
f. Any problems with any parameters – the script should exit with a status of 99.
g. The files ‘file.result’ and ‘directory.result’ should be written in the shell script user’s home directory (not the current directory).
h. Under no circumstances should the files ‘file.result’ and ‘directory.result’ be completely overwritten. All shell executions must add to those files, never overwrite them.
i. If a directory or file is used as a parameter and it is not accessible an error message must print and the parameter skipped as a result.
j. The report files (file.result and directory.result) should contain the executing user’s id as part of the result.
k. Any temporary files you create should be stored in the /tmp directory and deleted upon shell exit.
3. In terms of the shell script itself, you must document (by a series of comments) what the shell script does and who wrote it (the author – you). Therefore, your name must be in the shell script itself.
4. no syntax errors or warnings should appear when executed.
5. The amount of partial credit will be based on how much the shell script accomplishes according to the above rules.
6. Not turning in anything results in a grade of “0”.
7. The shell script must be written in either ‘bash’ or ‘ksh’ only. No other scripting languages will be accepted for this assignment.
8. You may use any auxiliary utilities you deem necessary inside the shell script to accomplish the action according to the above rules. Everything must be self-contained in the single shell script.
9. Use the ‘echo’ command to print messages out of the shell script rather than the sometimes builtin ‘print’ command.
10. The shell is not designed to be interactive; do not ask the user for any information.
11. Make sure the permissions on your shell are private to the owner and no other permissions are set.
1. in the reports, along with the user name, put the date and time of the request for that user.

2. Relevant commands, code, scripts, algorithms:

This information is included in #1

3. The attempts at a solution (include all code and scripts):

Code:
#author: Brett Stoddard
if [ -f $file ]
then
echo $file is a file, and the contents have been written to file.result
wc -l $file | more >> file.result
elif [ -d $file ]
then
echo $file is a directory, and the contents have been written to directory.result
echo ls $file | wc -l echo requested by $USER >> directory.result
else
echo $file is not a regular file or a directory
fi

I have tried a couple different times but just kept overwriting the file.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Milwaukee Area Technical College, Milwaukee, WI, USA, Jerry Lieberthal
Note: Without school/professor/course information, you will be banned if . ITNET-161 linux/unix overview

I can not find a link to the direct course - but here is a link to the degree page that includes the course - It will not let me post a link
you post here! You must complete the entire template (not just parts of it).

Last edited by DukeNuke2; 10-31-2010 at 11:58 AM..
# 2  
Old 10-31-2010
Hi Brett,
Code:
  wc -l $file | more >> file.result

this will work, but the more statement does not do anything
Code:
echo ls $file | wc -l echo requested by $USER >> directory.result

this should be two statements:
Code:
ls "$file" | wc -l >> directory.result
echo "requested by $USER"

it is good practice to put double quotes around variable references.
# 3  
Old 10-31-2010
Reply - thank you so much.!!

can you also help with some of the others.. I spent like 6 hours yesterday tryin gto get it to work.. no luck..

I am driving to NC today (in about an hor or so) for training and have been trying to get all my other classes out of the way as well as I will be taking security+ exam on friday and wanted to focus on that.


I could not figure out how to do the exit statements that are asked or the error if the file or directory is unreadable?

This was my last resort as I was trying to figure it out myself... it is due today - even for partial credit - So I figured I would solicite online help - 5 or six hours googling etc..

again thanks

---------- Post updated at 10:27 AM ---------- Previous update was at 10:25 AM ----------

thanks for thre info about the quotes - that makes sense
# 4  
Old 10-31-2010
You already figured out where to generate the error message yourself (after the else) .
If you want to exit the script with error code 99 then use:
Code:
exit 99

The user's home directory is ~ . Just issue:
Code:
ls -ld ~/*

for example...
Both ksh and bash also provide the $HOME variable

Last edited by Scrutinizer; 10-31-2010 at 12:03 PM..
# 5  
Old 10-31-2010
cool thanks so much

I will assemble it all when I get to NC tonight..

Date and time to include those it would be $DATE and $TIME along with the $USER, correct?

in a few of our example scripts I have seen exit 0

what is the differrence between exit 0 and exit 99?

will the script know if there are errors - to exit with 99 and if not exit with a 0?

sorry - I am totally new to scripting. it is cool - but not easy at first

again thanks
# 6  
Old 10-31-2010
point f and g seem to be contradicting each other, but perhaps you know what they mean. Maybe they only want you to exit if there are no parameters, or perhaps they want you to process all the other parameters and when they are all processed, only then exit with code 99.

---------- Post updated at 15:54 ---------- Previous update was at 15:41 ----------

To get the date or the time use the date command. The USER variable typically contains the user name, but you cannot count on it. Use a tool like id or whoami for that:
id -un gives the user name . id -u gives the user id. The id is a number but some people mix user id's and user names up so maybe they require the name and not the id (or both). What you can use depends on your system what is available.

If you are using bash then there are the variables $UID and $EUID (see man bash).
# 7  
Old 10-31-2010
Yeah I think they mean that if I create the script inproperly and therfore an error exists then a code of 99 should be the result

if I run the code and everything runs as it is supposed to it should exit with a 0

again -thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

2. Shell Programming and Scripting

Shell script works fine as a standalone script but not as part of a bigger script

Hello all, I am facing a weird issue while executing a code below - #!/bin/bash cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset sh UKBA_publish.sh UKBA 28082015 3 if then echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
Discussion started by: ektubbe
2 Replies

3. UNIX for Dummies Questions & Answers

Calling a script from master script to get value from called script

I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below. #masterscript.sh ./callingscript.sh echo $fileExist #callingscript.sh echo "The script is called"... (2 Replies)
Discussion started by: Raj Roy
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question