error in if condition


 
Thread Tools Search this Thread
# 1  
Question error in if condition

Hi All,
I need to compare the header of the input files which can be comma demilited, tild(~) or Pipr(|) seperated file.
I am comparing the file header by taking 1st row and comparing it with input row:
Below is the actual codeSmilieksh script)
Code:
if [ $v_avilableheader -ne $v_header ]
    then
      echo  $dat >> $v_logdir/$v_logfile
    echo ":Header's doesn't match for the file $v_available_file_name" >> $v_logdir/$v_logfile
     mv $v_finalsourcefile $v_invalidfiledir
    v_errmessage="Header's doesn't match for the file $v_available_file_name"
exit
fi

=====================================================
output error:
Code:
--------------
+ [ "MEMBER"|"MEMBERDESC"|"LEVEL0"|"LEVEL1" -ne "MEMBER"|"MEMBERDESC"|"LEVEL0"|"LEVEL1" ]

./abcd.sh[208]: "MEMBER"|"MEMBERDESC"|"LEVEL0"|"LEVEL1": syntax error

====================================================

It says syntax error. Please help as to how to i compare both the variables..

Last edited by Corona688; 10-16-2012 at 07:06 PM..
# 2  
Replace
Code:
if [ $v_avilableheader -ne $v_header ]

with
Code:
if [[ $v_avilableheader != "$v_header" ]]

.

Last edited by elixir_sinari; 10-23-2012 at 12:52 AM..
# 3  
Quote:
Originally Posted by abhi_123
Code:
if [ $v_avilableheader -ne $v_header ]

Code:
+ [ "MEMBER"|"MEMBERDESC"|"LEVEL0"|"LEVEL1" -ne "MEMBER"|"MEMBERDESC"|"LEVEL0"|"LEVEL1" ]

Here is the problem: always keep in mind that the shell will do a simple text replacement to replace variables with their contents. This is very versatile on one hand but can lead to unexpected behaviour on the other.

Consider the following lines:

Code:
variable="abc 123"
command $variable

When the shell executes this it shell will first replace "$variable" with "abc 123", only then execute "command abc 123". It would even be possible to "execute" a variables contents directly:

Code:
variable="ls -l"
$variable

This replacement process is done in several different steps. Look up the details under "command evaluation [in the] Korn shell".

Your variable contents now contain the pipe-symbol ("|"), which is part of the regular shell syntax. After the replacement phase the shell will end up with a command which looks like a pipeline (but of course can't be executed). This is the reason for the syntax error.

The trick is to prevent the shell from interpreting the contents of your variables so it can't confuse these with command parts. This is why elixir_sinari suggested the double quotes around the variable names - they do exactly this.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
MySQL thanks works fine now..

thanks it works fine now. Smilie
# 5  
Quote:
Originally Posted by bakunin
Your variable contents now contain the pipe-symbol ("|"), which is part of the regular shell syntax. After the replacement phase the shell will end up with a command which looks like a pipeline (but of course can't be executed). This is the reason for the syntax error.
Just as a quote is not special when it appears during the parameter expansion parsing phase, the pipe symbol is not in any way special here. The parsing step which looks for quotes, pipes, and other operators has completed.
Code:
$ x='"A"|"B"|"C"'  # Unless eval is used, neither the pipes nor double quotes are special
$ echo $x
"A"|"B"|"C"
$ [ $x = '"A"|"B"|"C"' ] && echo MATCH
MATCH
$ $x
-bash: "A"|"B"|"C": command not found

Note that in that last case the entire string is a single token. Quotes and pipes were taken literally.

The OP's syntax error is most likely from passing string operands to an arithmetic operator, -ne.

Regards,
Alister
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #495
Difficulty: Easy
Blocks of code cannot be nested within each other to form sub-blocks.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If condition is not working and getting error

Hi Team, If condition is not working properly and getting below error # ./score1.sh Enter your score ('q' for quit): 102 Enter your score ('q' for quit): q ./score1.sh: line 9: q: integer expression expected Average is: 102%. Exit. Actual code # Calculate the average of given... (3 Replies)
Discussion started by: Torrid
3 Replies

2. Shell Programming and Scripting

If condition giving syntax error. Need help.

Hi, I have an if condition on executing it is giving syntax error as below: -------------------------------------------------------------------------------------- line 61: syntax error in conditional expression ./play_test.sh: line 61: syntax error near `]' ./play_test.sh: line 61: ` if... (2 Replies)
Discussion started by: ramki067
2 Replies

3. Shell Programming and Scripting

Handling standard error in condition

Testing this with KSH on RHEL The bellow code works but i can't seem to handle the exit status of the unix command when it fails... i wanted to put something like >/dev/null 2>&1 to manage standard output and standard error but it changes my logic and the code doesn't work cause it doesn't... (11 Replies)
Discussion started by: maverick72
11 Replies

4. Shell Programming and Scripting

Error: if condition

please follow below code if ; then its giving me an error as "test1.sh: line 3: r=$(eval echo '$lck_'$TABLE != "") if can anyone tell me how to do inside if condition...with out use any variable.. TIA (6 Replies)
Discussion started by: gnnsprapa
6 Replies

5. Shell Programming and Scripting

while condition error:

I'm trying to run following code: while do echo "Hello World" done but I'm getting error on first line: ./test: line 1: syntax error near unexpected token `(' can anyone please tell me who can i run this loop. Also please tell me what will be syntax of do while loop in case i... (11 Replies)
Discussion started by: kashif.live
11 Replies

6. Shell Programming and Scripting

Condition error!

Hi Guys, Can you please help me to check if my condition is correct. if && then if ; then # active SE_USERID_LUZON_5="A" elif ; then # not active SE_USERID_LUZON_5="D" ${ECHO_CMD}... (3 Replies)
Discussion started by: nikki1200
3 Replies

7. Shell Programming and Scripting

Error in if condition string comparison

Hello all! I need help in debugging following script. I have no idea where I am going wrong. #!/bin/bash for p1 in A1 TM MP do for p2 in A1 TM MP do for mp1 in N1 N2 do for mp2 in N1 N2 do for mp3 in N1 N2 do for mp4 in N1 N2 do for... (7 Replies)
Discussion started by: RLOA
7 Replies

8. Shell Programming and Scripting

If condition error-please debug my script

Hi All, I have a script which is based on calculating current time The script looks like this: whenever i try to run the script it gives me error:Please advice wherther i have made mistake with if condition or with for loop?? The code : set -x #!/usr/bin/ksh set -x... (7 Replies)
Discussion started by: usha rao
7 Replies

9. Shell Programming and Scripting

error in if-else condition

Hi.. Im using the following script to find whether the present day is monday or not. If it is monday it has to do a specific set of things. #!/bin/sh Present_Date=`date` LOAD_DAY=`date -d "$Present_Date" | cut -d " " -f 1` echo $LOAD_DAY if ;then echo "Monday" #statements;... (6 Replies)
Discussion started by: abala
6 Replies

10. Shell Programming and Scripting

When running if condition, getting the following error

Hi All, My input file name is 1.sh the contents of file are cat status2.txt | while read filename do echo "$filename" echo "first content of the file is ${filename}" echo "second content of the file is ${filename}" echo "second content of the file is ${filename}" if } -eq 0 -a... (1 Reply)
Discussion started by: sunitachoudhury
1 Replies

Featured Tech Videos