if then else; no variable match but statement executes anyway.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if then else; no variable match but statement executes anyway.
# 1  
Old 09-27-2012
if then else; no variable match but statement executes anyway.

My first then statement is executing even though there is no match between the variables. each subsequent if then statement is also executing.

Why do they execute when there is no match in the dates?



Code:
yr=`date +%y`
date1=12-31-$yr
date=`date +%m-%d-%y`



set -vx
if  [ "$date" == "$date1" ] ;
        then
                #rm /root/sar_data/hist_sar*-*-2012.tar.gz
                printf "2012\n"
fi
        if [ "$date"=="$date1" ]
                then
                rm /root/sar_data/hist_sar*-*-2013.tar.gz
                printf "2013\n"
fi
        if  [ $date==$date1 ]
                then
                rm /root/sar_data/hist_sar*-*-2014.tar.gz
                printf "2014\n"
fi
if  [ $date=$date1 ]
                then
                rm /root/sar_data/hist_sar*-*-2015.tar.gz
                printf "2015\n"
fi


Last edited by Scrutinizer; 09-27-2012 at 09:57 PM.. Reason: code tags
# 2  
Old 09-27-2012
Quote:
Originally Posted by bash_in_my_head
My first then statement is executing even though there is no match between the variables. each subsequent if then statement is also executing.

Why do they execute when there is no match in the dates?



yr=`date +%y`
date1=12-31-$yr
date=`date +%m-%d-%y`



set -vx
if [ "$date" == "$date1" ] ;
then
#rm /root/sar_data/hist_sar*-*-2012.tar.gz
printf "2012\n"
fi
if [ "$date"=="$date1" ]
then
rm /root/sar_data/hist_sar*-*-2013.tar.gz
printf "2013\n"
fi
if [ $date==$date1 ]
then
rm /root/sar_data/hist_sar*-*-2014.tar.gz
printf "2014\n"
fi
if [ $date=$date1 ]
then
rm /root/sar_data/hist_sar*-*-2015.tar.gz
printf "2015\n"
fi
The arguments to test need to be separate operands. Your first test:
Code:
if  [ "$date" == "$date1" ] ;

is working as you expect because there are spaces surrounding the "==". The rest all evaluate to true because without the spaces around "==" and "=", you just have one operand to test and it evaluates to true because it is not an empty string.
# 3  
Old 09-28-2012
I am trying to match a date when the date on the right side which will be 12-21-year. I want a match. the date on the left will

so I want 12-30-12 = 12-30-12 to match and when they do I would like to have the command in the statement execute. If there is not a data match I would like nothing to happen.

What is the best way to accomplish this in my example.

Thanks
# 4  
Old 09-28-2012
The first if command in your example does exactly what you want when I try it. (I use ksh; you haven't said what shell you're using.) In other words, it does not print 2012 because "12-31-12" is not "09-28-12".) It should work with any shell that supports the basic tenets of the shell command language defined by the the original Bourne shell. (This includes at least bash, ksh, and sh.)

The test statements in the rest of your if commands all evaluate to true because the strings "09-28-12==12-31-12" and "09-28-12=12-31-12" are not empty strings. In these cases == and = are just parts of a string; they are not comparison operators.

Assuming you are using bash, ksh, sh or a similar shell (NOT csh nor tcsh), you should get what you seem to want if you change: "$date"=="$date1" to "$date" == "$date1", $date==$date1 to $date == $date1, and $date=$date1 to $date = $date1, respectively.

You may note that the standards specify = as the operator to compare strings for equality, but I don't know of any POSIX-conforming shell that doesn't also recognize == (and most shell maintainers prefer == and consider = obsolete).
# 5  
Old 09-29-2012
Code:
# test command need 3 arguments when like to compare strings
# value1 operator value2
# operator:  !=  or  = 
# => need spaces between args
if [  "$date" = "$date1" ] ; then
    echo same
fi

# or
[ "$date" = "$date1" ]  && echo same
# or
[[ "$date" == "$date1" ]]  && echo same

[
and
[[
are not same.
[[ is newer and include more functionality as old test command (sortly [ )

Last edited by kshji; 09-29-2012 at 10:58 AM..
# 6  
Old 10-02-2012
Code:
#!/bin/bash
Now I would like to iterate through two arrays.
array3=(12 13 14 15 16 17 18 19 20 21 22 23 24 25)
array4=(13 14 15 16 17 18 19 20 21 22 23 24 25)

for i in ${array3[@]}  && for q in ${array4[@]};
do
[ "$date" = "12-31-"$q"" ] && rm /root/sar_data/hist_sar*-*-20"$i".tar.gz ; printf "20"$i"\n"
done

ERROR:
for i in ${array3[@]} && for q in ${array4[@]};
./sarbackup.sh: line 73: syntax error near unexpected token `&&'
./sarbackup.sh: line 73: `for i in ${array3[@]} && for q in ${array4[@]};'

I did not think this would work but I tried in anyway for fun. This does however work to display what I am trying to accomplish.

Please show me a way that works with proper syntax.

thanks
# 7  
Old 10-03-2012
for need do and done.
Code:
# this format works
for ...
do
     for ....
     do
     done
done

"$date" = "12-31-"$q""
=> no need to join strings and variable values, in the shells in the double quoting variable values has taken. Between " " almost every special characters lost the special meaning except \ and $.
"$date" = "12-31-$q"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable IF statement in awk

I have multiple requirements to run data extracts (with the same basic requriement / resulsts) however the parameters of the extracts change with each request. To help make life easier I am writing a script where the parameters for the specific request are set at the start and then used as needed.... (6 Replies)
Discussion started by: meike
6 Replies

2. Shell Programming and Scripting

Variable on If Statement

Hi, I am tasked to modify soem script and I come accross a line which I dont fully understand. I tried searching online but I couldnt get a good explanation on it. Here it the part of the code: PAY_RT=`cat $TEMPFILE | cut -f2 -d","` if ; then PAY_RT=R fi What is... (3 Replies)
Discussion started by: The One
3 Replies

3. Shell Programming and Scripting

script to match statement from a file

All, I am trying to write a script which will match the statement from one log file and then proceed for other commands mention in the script. for my application, backup is schedule to run every day at particular time and creats a log file "/var/adm/backuplog". if backup completes then... (2 Replies)
Discussion started by: anshu ranjan
2 Replies

4. Shell Programming and Scripting

trying to find match from multiple string if statement

I'm trying to create what (should be) a simple bash script that will pull computer name and use that info to bind to one of three servers. Is there any way to do this without having a text file with the names of the servers and associated computer names? (5 Replies)
Discussion started by: jacobsbigbro1
5 Replies

5. Shell Programming and Scripting

awk pattern match and search in single statement

Hi All, I am trying to alter all lines between EXEC SQL and END-EXEC that have an INCLUDE in them. The following code search="INCLUDE " cp -f ${WORK}/$file.in ${WORK}/$file.wrk2 for item in `echo $search `; do > ${WORK}/$file.wrk1 awk -vITEM="$item" '{ if ( $0... (3 Replies)
Discussion started by: Bruble
3 Replies

6. Shell Programming and Scripting

Select variable within a if statement

i want to select a variable created and use it in a if statement, but not getting the desired results LINE='device for 0101a01: lpd://172.25.41.111:515' prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7` echo $prt My if statement to select just what i want.. IFS=$":" while read prt... (11 Replies)
Discussion started by: ggoliath
11 Replies

7. Shell Programming and Scripting

Using variable in case statement

I want to do this: Ex 1: case $answer in 1|2|3|4|5) echo $answer;; x) break;; *) echo "Invalid selection. Try again.";; esac But I need the part "1|2|3|4|5" to be fetched from a variable, like so: Ex 2: case $answer in $cases) echo $answer;; x) break;; *) echo "Invalid... (2 Replies)
Discussion started by: fialia
2 Replies

8. Shell Programming and Scripting

How to match the last XML extension by using Case statement

Hi All, I have a status.txt file which contains following three files. 1.xml 2.xml 3.xml Now i have written a shell script 1.sh which contains the following cat status.txt | while read filename do echo $filename case "$filename" in xml) echo "running 1.xml" ;; ... (3 Replies)
Discussion started by: sunitachoudhury
3 Replies

9. UNIX for Dummies Questions & Answers

Regex in if-then-else statement to match strings

hello I want to do a pattern match for string in the if statement, but I am not sure how to use regex inside the if statement. I am looking for something like this: if {2,3} ]; then ..... .... ... fi (7 Replies)
Discussion started by: rakeshou
7 Replies

10. UNIX for Dummies Questions & Answers

if statement to match *

Hi I have a script like read i if then echo bingo fi I want to enter the if block if i is equal to * The same doesn't happen , when I input * the script fails with error "[: too many arguments" I know it is very basic , but still Could any one help me out with the issue ... (3 Replies)
Discussion started by: sivasenthil_k
3 Replies
Login or Register to Ask a Question