error with TEST command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers error with TEST command
# 8  
Old 12-27-2011
For me, the same code works good.
Code:
pandeeswaran@ubuntu:~/Downloads$ sed 'q' ABC_INVENTORY
amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2

pandeeswaran@ubuntu:~/Downloads$ sed -n 'p' script.sh
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
pandeeswaran@ubuntu:~/Downloads$ ./script.sh
amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2
amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2
Yes

My guess is ,in the file you should have some non printable characters.Thats why it's not matching.
Just remove those by using the below sed one liner:
Code:
header_data_file=`head -1 ABC_INVENTORY|sed  -e 's/^[\t]*//'`

The final code will be:
Code:
pandeeswaran@ubuntu:~/Downloads$ sed -n 'p' script.sh
header_data_file=`head -1 ABC_INVENTORY|sed  -e 's/^[\t]*//'`
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
pandeeswaran@ubuntu:~/Downloads$ ./script.sh
amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2
amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2
Yes
pandeeswaran@ubuntu:~/Downloads$

IHTH(I hope this helps).
# 9  
Old 12-27-2011
Works for me. I tried the same code snippet on GNU Bash 4.2.10.

Include set -x after hash-bang line and see if you can debug.
# 10  
Old 12-28-2011
Let's check the record for control characters etc: (Command is designed to make control characters visible).

Code:
sed -n 'l;q' ABC_INVENTORY


Last edited by methyl; 12-28-2011 at 01:37 PM..
# 11  
Old 12-28-2011
I removed a [] from the 6th line and it worked.. below is the script..

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 
# 12  
Old 12-28-2011
After reviewing the posts on this thread, I believe that the main issue is the lack of double quote characters round strings which contain pipe characters. I'm not convinced that your most recent version (post #11) is sound (but it does appear to work).

This will probably be easier follow and easier to maintain:
Code:
header_data_file=`head -1 ABC_INVENTORY`
ABC_INVENTORY1="amount1|amount2|amount3|amount4|city|country|name1|name2|number1|number2"
if [ "${ABC_INVENTORY1}" = "${header_data_file}" ]
then
        flag="Yes"
else
        flag="No"
fi
#
echo "flag=${flag}"

./scriptname
flag=Yes


Footnote: I got the complete opposite result from pandeesh in post #8 ... as I think did Vijay81.
The script tested in post #8 can be corrected by putting double quotes round the string variables, which is itself best practice.

Last edited by methyl; 12-28-2011 at 02:03 PM..
 
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