Check for spaces in input argument!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for spaces in input argument!
# 15  
Old 02-11-2014
Quote:
Originally Posted by sea
Code:
while ( $#argv != 0)
	set flag = $argv[1]; shift;
	switch ($flag)
	case "-dir":
	    set lev3cope = $argv[1]; shift;

Code:
	case "-h":
            set zmax = $argv[1]; shift;
	    breaksw
        case "*":
            echo "Incorrect use of argument: $flag"
            breaksw
	endsw

Besides its CSH which i dont know, i'd understand the 'set' line, as the variable "flag" becomes the value of the array argv[1].
Which in turn would let you use $flag over $argv[1] on each call, or am i misunderstand that set flag = argv[1];shift

Took me just 10 years to get near the though i'd finaly understand 'shift', but this line.

Just trying to learn.
If you look at the usage statements this script prints:
Code:
echo "USAGE: cluster_on_lev3.csh -dir <full_path Lev3 gfeat cope directory> [options]"
echo "USAGE: cluster_on_lev3.csh -dir <full_path Lev3 gfeat cope directory> -t <value> -p <value> -c <value> -l <value> -h <value>"

you might notice that this script requires its options and option arguments to be presented as separate arguments. When a command line is presented as expected, flag will be set to an option introducer -dir, -t, -p, etc. The shift then removes the option introducer from the list of remaining arguments and $argv[1] refers to the option-argument corresponding to that option introducer. The switch statement then sets a variable corresponding to the given option introducer to the value of the option-argument (which is then also shifted off of the remaining list of unprocessed arguments).

Note that each case can do a shift in addition to the shift after picking up the option introducer because every option in this script requires an option-argument.

The command:
Code:
echo "Incorrect use of argument: $flag"

is only printed when an option introducer is found on the command line that does not match an expected option.
This User Gave Thanks to Don Cragun For This Post:
# 16  
Old 02-11-2014
Until now i used either arrays to parse the args (compare item with string '-h') and do action (or set variable) of array(+1), skipping i and i+1 after or even worse (but most used - lazy) hard coded argument positions....

eg - pseudo code - tired:
Code:
i=0
ARGS=($@)
for cur in $(seq 0 1 $#@);do
   ((i++))
   case "${ARGS[$cur]}" in
   "-h") echo "help" ;;
   "-o") echo other
         otherValue=${ARGS[$i]}
         unset ARGS[$i] ARGS[$cur] ;;
   *) echo "no args, or unhandled) ;;
    esac
done

This switch method kind of sounds faster.

Did i understand this properly (simplified?):
We're switching the flag, which is the actual argument trigger/toggle/(lost in translation), while argv[1] is the actual current value to be set as variable?
# 17  
Old 02-11-2014
I was merely trying to explain what this csh script was doing.

I do not approve of this method of parsing command line options and option-arguments.

When using standards conforming shells (such as ksh and bash), you can use the getopts shell built-in so any shell script can parse options just like standards-conforming utilities are required to do. For instance, if a utility has a -h option (which does not take an option-argument) and a -f option (which takes a filename as an option-argument), a shell script should be able to recognize any of the following as equivalent ways to specify both options and to recognize that -x is an operand (not an option):
Code:
scriptname -h -f file -- -x
scriptname -f file -h -- -x
scriptname -h -ffile -- -x
scriptname -ffile -h -- -x
scriptname -hf file -- -x
scriptname -hffile -- -x

You can write a shell script (bash, csh, ksh, etc.) to do this type of command line argument processing without using getopts, but few programmers make it work with the same look and feel as that provided by standard utilities. Why reinvent the wheel or limit the way in which your script recognizes command line options when getopts can be used to make your script handle command line options the same way almost all of the standard UNIX utilities handle command line options?

For an example using the getopts shell built-in, check out the EXAMPLES section of the getopts(1) man page.

Last edited by Don Cragun; 02-11-2014 at 11:29 PM.. Reason: Fix typo.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to iterate a function untill last argument with any order of input?

HI I need to get the function "kick" to get executed in any way the parameters are passed in to the function. The parameters are first stored in a dictionary self.otherlist = {} print self.otherlist self.populateTestList(self.system_type) print... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

2. Shell Programming and Scripting

Argument check

All, I'm writing an argument checking block and running into an error. I want to confirm that $1 is one of two values. Here is what I have: if ]; then echo -e "\nPlease check your first augument. You used \"$1\" which is not recognized. Please see usage:" usage ... (9 Replies)
Discussion started by: hburnswell
9 Replies

3. Shell Programming and Scripting

Utilize input file in place of argument

First I apologize for my ignorance as I am very new to the world of UNIX but love it and have a huge desire to learn it. A question I have is if a Korn script utilizes/relies on an argument to run, can you add these into a file and pipe them to the script without changing anything inside the... (2 Replies)
Discussion started by: djzah
2 Replies

4. Shell Programming and Scripting

Renice command with input argument as a file

Hi all could you help me how to give pids in a file as an argument to renice command. Thanks in Advance.. :) Pradeep (4 Replies)
Discussion started by: nanz143
4 Replies

5. Shell Programming and Scripting

The last argument contains spaces, how do I get it into a variable?

Gooday I have an argument string that contains 15 arguments. The first 14 arguments are easy to handle because they are separated by spaces ARG14=`echo ${ARGSTRING} | awk '{print $14}'` The last argument is a text that may be empty or contain spaces. So any ideas on how I get the last... (23 Replies)
Discussion started by: zagga
23 Replies

6. Shell Programming and Scripting

use input filename as an argument to name output file

I know this is a simple matter, but I'm new to this. I have a shell script that calls a sed script from within it. I want the output of the shell script to be based on the input file I pass as an argument to the original script. In other words... ./script.sh file.txt (script.sh calls sed... (2 Replies)
Discussion started by: estebandido
2 Replies

7. Shell Programming and Scripting

problem with spaces and argument parsing

public class HelloWorld { public static void main(String args) { System.out.println("Welcome, master"); } } and I compiled using javac HelloWorld.java ] Suppose that I execute the following command directly from the shell: java -XX:OnError="gdb - %p" HelloWorld Then it works... (8 Replies)
Discussion started by: fabulous2
8 Replies

8. Shell Programming and Scripting

Check if argument passed is an integers

How do I check if the argument passed to a script is an integer? I am writting a script that will take to integers and want to be able to check before I go on. I am using bourne shell. Thanks in advance (13 Replies)
Discussion started by: elchalateco
13 Replies

9. Programming

How do I input an argument in the main?

----------C program----------------------------- include <stdio.h> int main( int argc, char *argv ) { int i; for( i=0; i<argc; i++ ) printf("%\n", argv); return 0; } I wrote the C program above 'print.c'. Then, I compiled. (gcc -o print.o print.c)... (2 Replies)
Discussion started by: yhosun
2 Replies

10. UNIX for Dummies Questions & Answers

how to check if the argument contain wildcard (*,?) ?

In a script , i would like to check if the argument ( $1, $2 inside the script) contain wildcard (*,? etc). how do i do it? > script_name arg1 arg* $1 (arg1) does not contain wildcard, but $2 (arg* )contains wildcard. how can i tell in script? i need to do this is because : if arg1... (3 Replies)
Discussion started by: gusla
3 Replies
Login or Register to Ask a Question