Bad day !! test condition failed --need a one liner to do --help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bad day !! test condition failed --need a one liner to do --help
# 8  
Old 09-06-2006
There is nothing wrong but the way you want to put condition.I guess you want to pass exactly two parameters and check if the second one is P Q or D and then show success else improper usage.
Try this code or the one i posted yesterday
Code:
if [[ $# -eq 2 ]] && [[ $2 = "P" || $2 = "Q" || $2 = "D" ]] then
echo "Success"
exit 0; 
else
echo "Improper usage"
exit 1; 
fi

# 9  
Old 09-06-2006
thanks dhruv for finding time ..It work the way i want but actually i need the previous code mention by AHAT yesterday.

I run that code but see the Out Put :
$ ./t1.sh scr P
Improper Usage
$ ./t1.sh scr D
Improper Usage
$ ./t1.sh scr Q
Improper Usage

But according to the yesterday code : when $# less then 2 and $2 is not either of P,Q,D it should print Improper use !!Right ??
But even if i am providing right argument why it is showing "Improper Use "
I am looking at the BUG but could not find ! can you tell me ?

Last edited by jambesh; 09-06-2006 at 05:42 AM.. Reason: update
# 10  
Old 09-06-2006
if [[ $# -eq 2 ]] && [[ $2 != "P" || $2 != "Q" || $2 != "D" ]] then
echo "Failure "
exit 0
else
echo "Success usage"
exit 1
fi

Input : t1.sh scr P
Out put : Failure

Input t1.sh scr Q
output: Failure


Any clue ?

Last edited by jambesh; 09-06-2006 at 06:03 AM.. Reason: .
# 11  
Old 09-06-2006
Ok Try with this

Code:
if [[ $# -eq 2 ]] && ! [[ "$2" = "P" || "$2" = "Q" || "$2" = "D" ]] then
echo "Failure "
exit 0
else
echo "Success usage"
exit 1
fi

# 12  
Old 09-06-2006
Clever & Great ! worked as expected

! Outside the bracket work but when it was with the inner condition not worked. any reason you found for that ?.
Although seems simple but one can make this mistake and may be trapped for a time.
thanks a lot thanks for the help.
# 13  
Old 09-06-2006
!= requires &&

Quote:
if [[ $# -eq 2 ]] && [[ $2 != "P" || $2 != "Q" || $2 != "D" ]] then
is logically incorrect. It will always equate to true because of '||'. It means $2 is not equal to P or $2 is not equal to Q or $2 is not equal to D. If $2 is P then $2 not equal to Q,D is true, so on....

Quote:
if [[ $# -eq 2 ]] && [[ $2 != "P" && $2 != "Q" && $2 != "D" ]] then
&& is required whenever multiple != conditions are to be checked.

Last edited by vish_indian; 09-06-2006 at 08:02 AM.. Reason: spacing
# 14  
Old 09-06-2006
Thank you vish, for pointing out my logical error. Seems like I am having off day yesterday as well. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/bash one liner replacement for a if condition

Hi. I wrote this small bash script, i want to compare second column from file1 with file2 if a pattern matches. Files are small and I am sure that pattern occurs only once. I think this can be rewritten into a awk one liner. Appreciate if someone could give me idea. Whole NR FNR confuse me :o ... (6 Replies)
Discussion started by: ctrld
6 Replies

2. UNIX for Advanced & Expert Users

Rngd: failed fips test

I have recently enabled the process rngd, but after couple of days i see it got stopped automatically . Below is what i could find from messages file . can someone shed light on what could be the reason and how can this can be taken care? Apr 1 08:12:05 sap01 rngd: failed fips test Apr 1... (3 Replies)
Discussion started by: radha254
3 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

Need a condition to account for failed nslookups

I need some help creating a condition for looking up hosts. I have this master host file that has data in the following columns: FQDN primary IP secondary IP third IP I need the hostnames to feed into another script I use for provisioning users. The FQDN doesn't always work for... (2 Replies)
Discussion started by: MaindotC
2 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

test and if condition

Guys look at this: i have to write a script that takes a file as an argument. The script should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this. READ WRITE EXECUTE OWNER LEE.BALLANCORE YES YES NO... (9 Replies)
Discussion started by: ciroredz
9 Replies

7. Red Hat

validate test failed

hi everybody, I am new in Linux. I have successfully installed knoppix in my laptop, however, when I want to install ns2.26, some errors occurs in validation test.. It returned "Some Test failed" and it give some comands to re-run the test. I have already set the path before the validation test.... (1 Reply)
Discussion started by: newbie06
1 Replies

8. Shell Programming and Scripting

How can you Test for bad number

Anyone know the code to test for bad numbers? For example in ksh... Want the user to input an integer, but they input characters. read number while (number = letter) read number //until valid number is inputted thx for any help!! :confused: (3 Replies)
Discussion started by: gsxpower
3 Replies

9. Shell Programming and Scripting

bad if test

:confused: Bad if test: if returns an error I have changed filename to "filename"s su "$filename" 'filename'' '$filename'' "./filename" "./$filename" error is usually why cant I test the filename? Thanks (4 Replies)
Discussion started by: sharkze
4 Replies
Login or Register to Ask a Question