comparing 2 string variables in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers comparing 2 string variables in unix
# 1  
Old 07-23-2008
Error comparing 2 string variables in unix

search=Jul 22
date=Jul 22

if [ "$search" = "$date" ]
then
echo They match

>>this piece of code does not detect that search and date are equivalent strings.. what's wrong with it?
# 2  
Old 07-23-2008
You can't assign values with spaces in them unless you quote them.

Code:
search="Jul 22"
date="Jul 22"

case $search in "$date") echo they match;; esac

The use of case is just to demonstrate a different approach; for simple equivalence (and with proper quoting) if [ ... ] is fine.
# 3  
Old 07-23-2008
Please use [ code ] and [ /code ] tags, ty Smilie

Code:
search="Jul 22"
date="Jul 22"

if [[ "$search" = "$date" ]]
then
echo They match

You should enclose the value of the variables with double quotes too, like you did inside the test. It is always good to check the current value of variables with a simple "echo".
The double squared brackets are optional but recommended.
# 4  
Old 07-23-2008
Oh, god morgon to Sweden btw! Smilie
# 5  
Old 07-23-2008
what if the string for the variable search will come from the output of a command line? placing-double-quotes solution will not be applicable then...
# 6  
Old 07-23-2008
How do you mean, as a parameter? Usually double quotes should still be ok... Best explain a bit further please, ty.
# 7  
Old 07-23-2008
Code:
search="$@"


date_files="`date -r "$i""

substring="${date_files:3:7}"

if [[ "$substring" = "$search" ]]

here's some parts of the code i'm working on..
this is how i initialized the string variables that i'm having problems with..
i already placed double quotes on the equations that will be initialized to the variables.. it still doesn't work.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing 2 variables in UNIX

Hi, I have 2 variables as given below. How can i compare them and say its matching ? Appreciate your help VAR1=describe/read/write VAR2=read/write/describeThanks, Please use CODE tags as required by forum rules! (4 Replies)
Discussion started by: prince1987
4 Replies

2. Shell Programming and Scripting

Comparing two variables

I have a script like this. Just couldn't get the comparison part work. Any thought? thanks, #!/usr/bin/ksh -x STEP=`echo $(basename $0 .ksh) | tr "" ""` log=/skip.log while read LINE do if then echo `date`: STEP $STEP skipped by user >> $log exit 0 fi done < $1 echo... (0 Replies)
Discussion started by: ghostmic
0 Replies

3. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

4. Shell Programming and Scripting

comparing variables to date as string

I have a file $ cat myfile A 02/16/2012 B 02/19/2012 C 02/20/2012 D 02/17/2012 E 02/16/2012 My simple script > cat myscript.sh mydate="02/16/2012" awk ' ($2~/$mydate/) {print $1}' < myfile but I got no output! and when I try $2~/'$mydate'/ I got: The error context is (2 Replies)
Discussion started by: Sara_84
2 Replies

5. UNIX for Dummies Questions & Answers

Comparing the characters of 2 variables

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario eg ... (13 Replies)
Discussion started by: lsecer
13 Replies

6. UNIX for Dummies Questions & Answers

comparing variables

im trying to compare ipaddresses. i loop through an array to see if the ip is already is in the array and if it is it should set a flag and then i wont add it to the array. but its just adding all the ipaddresses to the array if ] then ... (3 Replies)
Discussion started by: magnia
3 Replies

7. Shell Programming and Scripting

Comparing String with Number variables

I have two variables and want to perform some functions after comparison $cat file1 14.abcde a=`cut -f 1 -d "." file1 b=15 if then .... fi but i got an error message says that unary operator expected and i think its because of $a is a string and trying to compare with... (3 Replies)
Discussion started by: bonosungho
3 Replies

8. Shell Programming and Scripting

Comparing Variables in Perl

Hi. I have three arrays. @a=('AB','CD','EF'); @b=('AB,'DG',HK'); @c=('DD','TT','MM'); I want to compare the elements of the first two array and if they match then so some substition. I tried using the if statement using the scalar value of the array but its not giving me any output. ... (7 Replies)
Discussion started by: kamitsin
7 Replies

9. Shell Programming and Scripting

Comparing two variables

Script #!/bin/sh hardware=PC os=WindowsNET for i in `cat newservers` do x=`sudo /opt/openv/netbackup/bin/admincmd/bpplclients |grep $i |head -40 |grep $i|awk '{print $3;exit}'` if then echo "$i is already added" else echo "Need to add" fi done O/p in debug mode bash-2.05$... (3 Replies)
Discussion started by: rajip23
3 Replies

10. UNIX for Dummies Questions & Answers

comparing variables

I have searched and found a few threads that have dealt with this, but the examples I've tried haven't seemed to help. I am monitoring our database log for high checkpoints. I can parse out the checkpoint value which can be anywhere from zero into a 3 digit number. I set a variable to be the... (3 Replies)
Discussion started by: MizzGail
3 Replies
Login or Register to Ask a Question