Script that copies one file to/over another

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Script that copies one file to/over another
# 1  
Old 03-29-2010
Script that copies one file to/over another

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

Write a script that asks for 4 arguments, and the 1st and 3rd need to be -i and -o, and the 2nd/4th need to be file names.

Technically, it's supposed to be run as:
./Lab_14.sh -i input.txt -o output.txt

Depending on how many arguments are used, and what the arguments are, error messages will pop up (ie- too many/few arguments, not an existing filename, etc etc etc)

2. Relevant commands, code, scripts, algorithms:

Required Errorlevels

Errorlevel# Event Information

0 Script functions correctly
10 No arguments provided on command line (=0)
11 First argument missing (=0)
12 First argument present, but not what expected (“-i”)
13 Second argument missing on command line
14 Second argument present, but file does not exist
15 Third argument missing on command line
16 Third argument present, but not what expected (“-o”)
17 Fourth argument missing on command line
18 Too many arguments provided on command line (>4)

Variable Interpolation

The script must be as dynamic as possible. Variables are to be used for command line arguments, user input, and generated output throughout the script. Hard-coded values, where variables can be used, are unacceptable.



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

Code:
#!/bin/bash
echo

proper="Usage:  ./Lab_14.sh -i <infile> -o <outfile>

-i <infile>   The filename containing the existing data.
-o <outfile>  The filename you want the output"

if [ "$1" == "" ] ;then
	echo "You must input the first argument."
	echo
	exit 11
fi

if [ "$1" != "-i" ] ;then
	echo "Error: Expecting \"-i\" for the first argument."
	echo
	exit 12
fi

if [ "$3" != "-o" ] ;then
	echo "Error: Expecting \"-o\" for the 3rd argument."
	echo
	exit 16
fi

if [ "$3" == "" ] ;then
	echo
	echo "You must input the third argument."
	echo
	exit 15
fi

if [ "$2" == "" ] ;then
	echo "You must enter a file name."
	echo
	exit 13
fi

if [ ! -e "$2" ] ;then
	echo "$2 does not exist, please enter a real file."
	echo
	exit 14
fi

if [ "$#" == "0" ] ;then
	echo
	echo "Error: No arguments present at all."
	echo
exit 10
fi

if [ "$#" -ge "5" ] ;then
	echo
	echo "Error: Too many arguments"
	echo
exit 18
fi

if [ "$4" == "" ] ;then
	echo
	echo "Error: Fourth argument is missing."
	echo
	exit 17
fi

if [ -e "$4" ] ;then
	echo
	echo "File \"$4\" exists already.  Overwriting contents."
	echo
fi


if [ "$?" != "0" ] ;then
	echo
	echo $proper
	echo
fi

	cp $2 $4
	echo
	echo "Correct amount of arguments provided ..."
	echo
	echo "First argument was: " $1""
	echo "Second argument was: "$2""
	echo "Third argument was: " $3""
	echo "Fourth argument was: "$4""
fi

exit 0

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

Mohawk College, Hamilton, ON, Canada, Cheryl Weaver, dunno my course number right now

---------- Post updated 03-29-10 at 10:52 AM ---------- Previous update was 03-28-10 at 05:34 PM ----------

ignore my nonsense, I fixed it after a marathon of vi editing Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

I need a korn shell script that renames and copies

I have 68 servers whose /etc/ntp.conf file have the wrong NTP server name and they need to be updated with a different or correct NTP server name. I am doing this on AIX Here is what I would like. and I would need a script that would rename the old /etc/ntp.conf and add a date stamp to it ... (1 Reply)
Discussion started by: jesifra
1 Replies

2. Shell Programming and Scripting

Help configuring sudo for multiple file copies

Hiya, I want to allow some users to copy all filenames of a specific filetype, to a limited directory. 3+ users: need to be able to copy(as root) any *.war file to /usr/local/tomcat/current/webapps/ I tried the following... dmurphy huskar=/tmp/who.sh,/bin/cp *.war... (2 Replies)
Discussion started by: infinitiguy
2 Replies

3. UNIX for Dummies Questions & Answers

multiple copies at once

Dear all, Today I spend almost all my day with something I hope any of you can help me with... I'm trying to write a small script (!/bin/sh) to be able to make 900 copies of one file. Can anyone help me with this? I couldn't figure this out (maybe I need to create some sort of loop) and couldn't... (8 Replies)
Discussion started by: marjocello
8 Replies

4. UNIX for Dummies Questions & Answers

Printing Multiple Copies

Has anyone come accross and solved an issue where only 1 copy of a doc prints after you use the -n flag and specify more then 1. My exact syntax is lp -dprintername -n3 documentname enq command does same thing. Both commands show the 3 copies in the queue but only 1 ever prints I am using AIX... (0 Replies)
Discussion started by: capeme
0 Replies

5. Shell Programming and Scripting

Script to print multiple copies of the same file

Hi, this is my first post so I hope I have placed it in the appropriate section! I have created a looping script to print a text file multiple times. The script works great, and it displays the job numbers of all the prints that result from its execution. The trouble is, only the first job... (1 Reply)
Discussion started by: Alpha7
1 Replies

6. Shell Programming and Scripting

Php script that copies Unix folders?

I work as a website designer where I work on different directories on a server but my boss doesnt give me root permission for security reasons. So I have a temporary folder where I work and he copies the files over as he has root permission. The command he uses is usually like: cp -fR... (4 Replies)
Discussion started by: edtech
4 Replies

7. UNIX for Advanced & Expert Users

Making numerous virtual copies of a single file

I have to put a single/identical file to numerous different directories on a website and update them simultaneously and identically. If I have the master file in directory a/, how can I make a "virtual copy" of this file into an other directory, in a way that any changes to the master file will... (2 Replies)
Discussion started by: poredudue
2 Replies

8. AIX

Printing a number of copies of a txt file

Hi, We have an application running on AIX5.3 that generates text files that are sent to be printed using the lp command. The user can specify the number of copies they want printed, but only one copy ever gets printed. I've checked the lp command that is used, and it correctly specifies... (5 Replies)
Discussion started by: stebradshaw
5 Replies

9. UNIX for Advanced & Expert Users

lp not printing multiple copies

I'm trying to print multiple copies of a file on sun solaris 8 with the lp -n command to no avail. No matter what numeric value I supply -n, I still only get 1 copy of the file printed. The command I'm using is 'lp -d SNY_IT5000-2 -n2 file' The printer is an HP Laserjet 4000 with a... (2 Replies)
Discussion started by: hassan2
2 Replies
Login or Register to Ask a Question