Argument check


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Argument check
# 1  
Old 08-17-2014
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:

Code:
if [[ "$1" != ("-e" || "-d") ]]; then

        echo -e "\nPlease check your first augument. You used \"$1\"
which is not recognized.  Please see usage:"
        usage
        exit 1

else

        echo -e "\narg 1 is good to go.."

fi

Can anyone point me in the right direction? Any help is greatly appreciated.

Thanks,

Herb
# 2  
Old 08-17-2014
The correct syntax depends heavily on the shell that you are using. Please post. What would you conclude from the error message that you certainly got but did not post either?
# 3  
Old 08-17-2014
Close, try changing your if condition to the following:

Code:
if [[ "$1" != @("-e"|"-d") ]]; then

These 2 Users Gave Thanks to pilnet101 For This Post:
# 4  
Old 08-17-2014
RudiC - My apologies, I'm writing in bash.

pilnet101 - I made your suggested modification as:

Code:
if [[ "$1" != @("-e"|"-d") ]]; then

        echo -e "\nPlease check your first augument. You used \"$1\"
which is not recognized.  Please see usage:"
        usage
        exit 1

else

        echo -e "\narg 1 is good to go.."

fi

I receive the following error:

Code:
syntax error in conditional expression: unexpected token `('
syntax error near `@("'
`if [[ "$1" != @("-e"|"-d") ]]; then'

Did I write it incorrectly?

TIA,

Herb

Last edited by Scrutinizer; 08-17-2014 at 05:18 PM.. Reason: additional code tags
# 5  
Old 08-17-2014
Surprisingly enough, pilnet101's proposal worked in my GNU bash, version 4.3.11(1). I'm glad I learned something today (although a bit disappointed as I didn't find that feature in bash's man page)!

As your bash doesn't like that construct, try e.g. (amongst other options):
Code:
if [[ "$1" != "-e" && "$1" != "-d" ]]; then


Last edited by RudiC; 08-17-2014 at 03:05 PM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-17-2014
The extended pattern became default behavior inside double square brackets in bash version 4.1, so I imagine OP's bash version is an older one.
# 7  
Old 08-17-2014
Thanks RudiC. Yes my bash version is quite older: GNU bash, version 3.2.25(1).

I was able to get the desired behavior with your suggested modification.

I appreciate your help.

Herb
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a script to check if an argument is valid shell variable

I need a script that should print 'yes' if the argument is a valid shell variable name else 'No' if it is not a valid shell variable. A valid one begins with an alphabet or percentage (%) character and is followed by zero or more alphanumberic or percentage (%) characters. For example: $... (6 Replies)
Discussion started by: pingiliarjun
6 Replies

2. Shell Programming and Scripting

Check for spaces in input argument!

Hi guys, I have created a csh script which allows user to pass input argument with the script like: cluster_on_lev3.csh -t <value> -p <value> Example: cluster_on_lev3.csh -t 2.3 -p 0.05 Now I want to create an error code where if user passes input argument without spaces , the code... (16 Replies)
Discussion started by: dixits
16 Replies

3. Shell Programming and Scripting

Shell script that check the argument passed to it and prints error if test condition is not met

I want to make a script that check for the argument passed to it and generates an error in case any character/string argument passed to it. I am using below code, but its not working. can anyone help. #!/bin/bash if ]; then echo 'An integer argument is passed to the script hence... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

4. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

6. UNIX for Dummies Questions & Answers

Problem: Need to check if a string argument contains "-"

I am passing a string as argument. Need to check if it contains "-". If it contains "-" then check if it contains "-r" .If Yes then print some message else check if it contains "-t".If yes print some message. How this check can be done using shell script? How I can do this by using IF OR... (7 Replies)
Discussion started by: nehagupta2008
7 Replies

7. UNIX for Dummies Questions & Answers

Check for Empty Command Argument

I have a script that when called can have 1 or 2 command arguments. If only 1 command argument is passed into the script how can I check that the second argument is null? I am working in Korn shell in a UNIX environment. Example of script call with 2 arguments: % statreport 0300 1430 ... (6 Replies)
Discussion started by: Nysif Steve
6 Replies

8. Shell Programming and Scripting

check if argument is an ip address in bash/sh

Hi all, Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh? Thanks, Marc (3 Replies)
Discussion started by: marcpascual
3 Replies

9. 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

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