error with TEST command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers error with TEST command
# 1  
Old 12-27-2011
error with TEST command

Hi,

I have written a script to compare 2 string values.. below is the script.. it throws an error - ./compare_str.sh[5]: test: amount1|amount2: unknown operator..


as both are string values, I am using a = operator in the test command.

can some one help me figure out where the issue is?
Code:
header_data_file=`head -1 ABC_INVENTORY`
ABC_INVENTORY1="amount1|amount2"
flag=`test [ $ABC_INVENTORY1 = $header_data_file ]`
echo $flag

the value for header_data_file is - amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2

and ABC_INVENTORY1 value is amount1|amount2


thanks!

Last edited by Scott; 12-27-2011 at 05:18 PM.. Reason: Please use code tags
# 2  
Old 12-27-2011
test and [ are synonymous. You don't need both.

Code:
$ test 1 -eq 1 && echo REALLY?!
REALLY?!
$ test [ 1 -eq 1 ] && echo REALLY?!
ksh: 1: unknown test operator

Besides that, if you're reading from (parsing) a file, you'll need more logic than a simple test.
# 3  
Old 12-27-2011
thanks scott. can you please elaborate on the logic part? Do you mean to check if the file contains data before doing a head -1 or something else?
# 4  
Old 12-27-2011
Nope. I think I just made that up - missed that you had used head to already read the file Smilie

Um. So flag should show whether the two match? Like:

Code:
flag=$( [[ $ABC_INVENTORY1 = $header_data_file ]] && echo Yes || echo No)

# 5  
Old 12-27-2011
hi scott,

yes, the flag value should show if the strings match or not.. I have the same values for both the strings but it didnt show a Yes, it always show a No....
script I have is:


Code:
header_data_file=`head -1 ABC_INVENTORY`
ABC_INVENTORY1="amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2"
flag=$( [[ $ABC_INVENTORY1 = $header_data_file ]] && echo Yes || echo No)
echo $flag

thanks!

Last edited by Scott; 12-27-2011 at 07:15 PM.. Reason: PLEASE, use code tags
# 6  
Old 12-27-2011
Please, try to debug your code.

After:
Code:
header_data_file=`head -1 ABC_INVENTORY`

add:
Code:
echo "$header_data_file"

and compare (and show us) the output.

(and, please start using code tags. There's a video demo here: Using Code Tags)
# 7  
Old 12-27-2011
Hi Scott, thanks much for your responses..

below is the code:

PHP Code:
header_data_file=`head -1 ABC_INVENTORY`
echo 
$header_data_file
ABC_INVENTORY1
="amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2"
echo $ABC_INVENTORY1
#flag=`[ $ABC_INVENTORY1 =  $header_data_file ]`
flag=$( [[ $ABC_INVENTORY1 $header_data_file ]] && echo Yes || echo No)
echo 
$flag 
Output from the code:

$ ./compare_str.sh
amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2
amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2
No

though ABC_INVENTORY and header_data_file are matching, the result is No which means not matching..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with error "por: 0403-012 A test command parameter is not valid."

Hi, im asking for help with the next script: echo ^; then if then printf "\033 query1.sh: export TERM=vt100 export ORACLE_TERM=at386 export ORACLE_HOME=/home_oracle8i/app/oracle/product/8.1.7 export ORACLE_BASE=/home_oracle8i/app/oracle export... (8 Replies)
Discussion started by: blacksteel1988
8 Replies

2. Shell Programming and Scripting

if condition error: test: 0403-004 Specify a parameter with this command

Hi all, I would like to ask if there's something wrong with my if - else condition, i couldn't run the script perfectly due to the error in my if - else condition stating that "test: 0403-004 Specify a parameter with this command." below is the snippet of my script if && && ] then echo... (5 Replies)
Discussion started by: jihmantiquilla
5 Replies

3. Shell Programming and Scripting

Test command

hello, i'v trying to use the TEST command and i have some problems with it. i am trying kill all proccess wich is greater than 25. i started with - ps -f | grep -v TTY | awk '{print $4}' but i dont know how to proceed from here.. 10x a lot, Daniel. (11 Replies)
Discussion started by: dadiT
11 Replies

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

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. Programming

Test command name

I'm very new to C and could use a little help. I'm testing to make sure the command is running as it's proper name, if not then fail. if (strcmp(argv, "xinit") != 0) { fprintf(stdout, "name = %s length = %d\n",argv,l); usage(0); } This works if the command is... (3 Replies)
Discussion started by: nck
3 Replies

7. Shell Programming and Scripting

Help with test command

Plese help me on the below query. for j in *.20071231* *.ctl *.dat do ( if then cp "$base/*.*" "$base1" fi ) done My requirement is for all files that has extension *.20071231* *.ctl *.dat should be copied to another folder. But those with caaa.20071231.log... (7 Replies)
Discussion started by: sussane
7 Replies

8. AIX

Test command

Hello, I am trying to add some tests to existing code. The code already contains some test commands. An example is as follows... ] then Does anyone know the purpose of the double equals? I would have used a single equals sign... (2 Replies)
Discussion started by: JWilliams
2 Replies

9. Shell Programming and Scripting

Help regarding Error message: A test command parameter is not valid

Hi I am getting few messages when trying to run my script from the following lines in the script if test then // SomeCode fi The messages are as follows: testing.sh: OBLIGOR_GROUP_ID: 0403-012 A test command parameter is not valid. testing.sh:... (5 Replies)
Discussion started by: skyineyes
5 Replies

10. UNIX for Dummies Questions & Answers

the TEST command

Hi everyone, I am new to UNIX and scripting, and I have some problems with the test command. when i try to execute the command: test 20070327.gz > 20070320.gz i try to make a charachter string comparison between the two strings or the two files, to make sure that 20070327.gz is greater than... (2 Replies)
Discussion started by: marwan
2 Replies
Login or Register to Ask a Question