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
# 1  
Old 09-05-2006
Bad day !! test condition failed --need a one liner to do --help

Hi all this is simple but bad day for me nothing work out ..

Problem is that I wan to check the argument passed to my script and accordignly exit or setup ENV variable

I have a script name src_cpcp_preproc.sh
i want to pass 2 argumet from command line argumet and check it in the script on following way :

src_cpcp_preproc.sh cpc P #P -for production,Q-qa and D-for development so $2 could be either of these three

I want to write a one liner which can check the three condition in a if statement

condition are if $# is less then 2 && either one of $2 is not P || Q || D then i should quit !
else i should set envment variable .

I had written if[ ($# -lt 2) && ("$2" -ne "P" ) ||("$2" -ne "Q") || ("$2" -ne D)]
then
echo "Use : ...."
exit 11
fi

Tell me where i am wrong ?
# 2  
Old 09-05-2006
What does || mean in your condition ?
# 3  
Old 09-05-2006
| mean the or condition
# 4  
Old 09-05-2006
Try this i am doing in opposite way.
Code:
if [ $# -eq 2 -a "$2" = "P" -o $# -eq 2 -a "$2" = "Q" -o $# -eq 2 -a "$2" = "D" ]
then
echo "OKAY usage"
else
echo "improper usage"
exit 
fi

The above code is checking if no of parameters are two and the second param should be P,Q or D.
# 5  
Old 09-05-2006
-a for and
-o for or
# 6  
Old 09-05-2006
how about the following
Code:
#!/usr/bin/ksh

if [[ $# -le 2 ]] && [[ $2 != "P" || $2 != "Q" || $2 != "D" ]] then
echo "Improper Usage"
exit 1;
else
echo "Success"
exit 1;
fi

Output
(/export/home/)->test.ksh P Q D
Success
(/export/home/)->test.ksh P
Improper Usage

BTW, I think your code has an error, I thought you only use -ne for integer and "=" for string. Perhaps that is why it did not work for you.
# 7  
Old 09-06-2006
ahtat it is not working the way i am expecting
this is working anly when i am giving -> .test.sh P Q D
success
but i want the following thing ->test.sh scr P
success
-> test.sh scr Q
success
-> test.sh xxs D
success
first parameter could be any thing but when the second one is either P OR Q OR D
then it should show success !
surprise although you code seems to be work but it is not giving the out put the way it should . can you please tell me what could gone wrong with this ?
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