Make a file accept only two arguments from the command line

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Make a file accept only two arguments from the command line
# 1  
Old 02-18-2015
Question Make a file accept only two arguments from the command line

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:
1) The script is executed in the Korn shell.
2) Name the shell script file is asg6s.
3) The asg6s file is executable and accepts exactly two arguments from the command line.
4) The asg6s file accepts any two named files as the two arguments that are text files.
5) Create a file containing “Message Holder” as the text within the file. This file is entered on the command line as the first argument.
6) The second argument is a file with whatever name you select that accepts the output from the asg6s file.
7) The command line must have the following: asg6s [input file name] [output file name] before any processing can occur. The input file must contain the words detailed in 5) above. The output file is created at the time the command line is executed, not before.
8) The script first checks that there are exactly two arguments on the command line. If the command line does not have two arguments, display an error message that includes a suggestion of the pattern on the command line and exit the script.
9) If the condition in 8) is met, then the script must independently check that the file named by the first argument both exists and contains some text. If the command line does not have as its first argument a file that exists and contains some text, display an error message that includes the name of the file and exit the script.


2. Relevant commands, code, scripts, algorithms:

Command line argument

Hello I am having a lot of trouble understanding for one, what I am supposed to do and two how I am supposed to implement this.

Obviously I am missing something. I think the main thing is I am not setting up the file to accept only two arguments from the command line. Any help would be appreciated. Even if it is links to help me understand what I am not understanding. Thanks

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

Code:
#!/bin/ksh
#Reroute to directory of used files
cd /class/
#Make the asg6s accept only two arguements
if [[ $# -ne 2 ]];then
    print "Does not equal two arguments"
    exit
fi
#Creates file containing Message Holder
cat message
Message Holder
#File that accepts the output of file message
cp message message1
#Determine if first argument is empty or null
if [ -z "$1"]
    echo "File does not exist"
fi

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

University of Maryland University College
3501 University Blvd. East
Adelphi, MD 20783

CMIS 325 6380 UNIX with Shell Programming (2152)

Professor: Thomas Timko

Last edited by ProgMan2015; 02-21-2015 at 12:54 PM..
# 2  
Old 02-18-2015
You seem to have an issue creating that text file.
See man ksh section "Redirection". (i assume its named the same as in man bash)

Just for the habbit, inconsistent use of single and double brackets for condition checking.
In BASH, the official standard (POSIX) is to use single brackets, eventhough double brackets work just quite as good on some systems it doesnt on others.
I assume the same for KSH, so i would recomend to use only single brackets, precicly as you done (incl the quoting) in the last if block.

Which besides is missing its 'then' part.
Maybe this fixes something?

Hope this helps to get you started.
# 3  
Old 02-18-2015
Also your references the first positional parameter, but not the second one. Otherwise lookup "positional parameters" in "man ksh" .
# 4  
Old 02-18-2015
There is also a space issue with testing if the variable $1 is empty.
Obviously an empty string cannot exist as a file, but assuming a file exists only because a variable contains something can lead to unexpected behaviour.
# 5  
Old 02-18-2015
In addition to what sea and Scrutinizer have already said, also note that there must be spaces between the arguments to the test expression command (AKA [ expression ]), including the closing ].

Also look at the man page for the cat command (man cat). You can use cat to put text into a file, but not the way you have done it. The text to go into your output file needs to be fed into cat by piping data into cat, redirecting input from a file containing the text, using a here-document, or naming the file containing your input data as a file operand. You might also consider redirecting the output from a Korn shell echo or printf built-in command to put a line of text into a file using redirection.

You have set up your script to be run by the Korn shell and checked that there are exactly two arguments given to your script. That takes care of items #1 and part of #3 in your assignment. Smilie

We can't tell what you have named your script nor whether or not you have made it executable from what you have shown us.

I don't see anything in your assignment that says that a file named message exists in your assignment directory nor that you are supposed to create a file named message1.
# 6  
Old 02-21-2015
Progress

ok I made some progress:

Code:
 
 #!/bin/ksh
 #Reroute to directory of used files
 cd /class/
 #Create function to handle exit function
 function usage{
 echo "Usage $0 inputfile outputfile"
 echo "The inputfile must have the text \"${must_have_text}\""
 exit 1
 }
 #Make the asg6s accept only two arguements
 if [ $# -ne 2 ]; then
 echo "Does not equal two arguments"
 usage
 fi
 #Creates file containing Message Holder
 #Have a variable for the message
 must_have_text="Message Holder"
 #Determine if first argument is empty or null
 #Create variables
 infile=$1
 outfile=$1
 if [ ! -f "${infile}"]; then
 echo "File ${infile} does not exist"
 usage
 fi
 if [ $(grep -c "${must_have_text}" ${infile}) -eq 0; then
 echo "${infile} must have 1 or more times the text ${must_have_text}"
 usage
 fi
 cp ${infile} ${outfile}

I keep getting this error though: ./asg6s: line 3: syntax error at line 6: `echo' unexpected

Last edited by ProgMan2015; 02-21-2015 at 12:54 PM..
# 7  
Old 02-21-2015
Hi,

Look at the syntax:
Code:
function varname { list ;}

There is a space between the varname and the opening brace.
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Make a file accept only two arguments from the command line

DELETED (2 Replies)
Discussion started by: ProgMan2015
2 Replies

2. UNIX for Dummies Questions & Answers

Command - filename as arguments - make executable to all users.

Edit: Sorry. Mistakenly posted - please delete (3 Replies)
Discussion started by: Reddax
3 Replies

3. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

4. Shell Programming and Scripting

Perl : accept multiple user entered command line arguemnts

I am trying to create a script that will accept multi input from the user (really just me), then execute those command on a remote device. My question is if the I enter "No" at the confirmation point "Are these statements correct y or n ?", what is the best way to go back and start over ? I... (3 Replies)
Discussion started by: popeye
3 Replies

5. UNIX for Dummies Questions & Answers

script unix which accept two arguments

does anyone can help me with this homework, please..I am beginner in linux and I don't how to do it :( Create a script scanner.sh which will accept two arguments: the first argument is the DNS name or the IP address of a system, or a network address or an IP range, the second argument is... (1 Reply)
Discussion started by: gennyy
1 Replies

6. Shell Programming and Scripting

ksh script that will accept arguments

Hi, I am not very skilled using ksh scripts. How do I create a ksh script that will accept arguments and use them in the script ? I need to make this: Run this command with this argument: ./mykshprogram.ksh madsen and sometimes I need to do this: Run the ksh again with 2... (3 Replies)
Discussion started by: hasselhaven
3 Replies

7. Homework & Coursework Questions

trouble understanding file option and command line arguments

Hi, I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves... (1 Reply)
Discussion started by: heywoodfloyd
1 Replies

8. Homework & Coursework Questions

I need to make a script that has 4 command line arguments to function properly.

I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Discussion started by: Wookard
3 Replies

9. Shell Programming and Scripting

How to read arguments to make command

In the make file update updateq: ------------------- ---------- i want the makefile to display some messages when user gives "make update", but totally quite wehn user enters "make updateq". Can u tell me how to read these argument in makefile. $1 doesnt work:( (3 Replies)
Discussion started by: vikashtulsiyan
3 Replies

10. Shell Programming and Scripting

Make new arguments for echo command

Hi everybody, i want to make an argument at echo command that takes a alpharithmetic and returns it reversed. How this can be done? plus what makefile changes are needed (0 Replies)
Discussion started by: Panteras
0 Replies
Login or Register to Ask a Question