LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-02-2009, 09:55 AM   #1
IPAddress
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Rep: Reputation: 0
[Scripting] Strings


Hi.
Im running a CentOS release 5.3 (Final) and im triyng to make a script to compare two lines of two files...

My Script:
Code:
#! /bin/bash

# CREATE ONE OF THE FILES: LIST OF ALL DOMAINS IN MY DNS SERVER.
cd /var/named/data
declare -a dominios=( * )

# CLEAN THE OTHER FILE FOR THE LAST CHARACTER.
#rev /var/www/html/ipaddress/lista | cut -b 2- | rev >> /var/www/html/ipaddress/dominios.txt

CantDominios=`sed -n '$=' /var/www/html/ipaddress/dominios.txt`
existe=false


for (( j = 1 ; j < $CantDominios ; j++ )) do
    Dominio=`head -n $j /var/www/html/ipaddress/dominios.txt | tail -n 1`
    for (( i = 0 ; i < ${#dominios[@]} ; i++ )) do
         [ ${dominios[$i]} == $Dominio ];${existe:=true}
         echo $existe "[${dominios[$i]}/$Dominio]"
    done
    if ($existe == false ) then
        echo ''  >> /etc/named.conf.BK
        echo 'zone "'$Dominio'" {' >> /etc/named.conf.BK
        echo '       type slave;' >> /etc/named.conf.BK
        echo '           file "/var/named/data/'$Dominio'";' >> /etc/named.conf.BK
        echo ''          >> /etc/named.conf.BK
        echo ''          >> /etc/named.conf.BK
        echo '            masters {xxx.yyy.www.zzz;' >> /etc/named.conf.BK
        echo '            };' >> /etc/named.conf.BK
        echo '};' >> /etc/named.conf.BK
    fi
        ${existe:=false};
done

# REMOVE THE FILE
#rm -f /var/www/html/ipaddress/dominios.txt

However theres a problem, cause "Existe" is always "false", even when the lines are exactly the same!
Maybe its because the way i'm comparing the string i take from ${dominios[$i]} and the string i take from $Dominio.
Any ideas?

Thanks in advance.

Bye!
 
Old 09-02-2009, 12:50 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
This line is strange
Code:
[ ${dominios[$i]} == $Dominio ];${existe:=true}
Should it be
Code:
[ ${dominios[$i]} == $Dominio ] && existe=true
If so, it would be more robustly coded as
Code:
[[ "${dominios[$i]}" == "$Dominio" ]] && existe=true
 
Old 09-02-2009, 01:12 PM   #3
IPAddress
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
This line is strange
Code:
[ ${dominios[$i]} == $Dominio ];${existe:=true}
Should it be
Code:
[ ${dominios[$i]} == $Dominio ] && existe=true
If so, it would be more robustly coded as
Code:
[[ "${dominios[$i]}" == "$Dominio" ]] && existe=true
Hi.
I think you meant...
Code:
[[ "${dominios[$i]}" == "$Dominio" ]] && $existe=true
But with that i get the output...
line 15: false=true: command not found
Is that the only change i have to do?

The idea of that line is to check...
if dominios[i] = Dominio then, i want existe to be true. An then, every time the i check " if ($existe == false )" to put $existe==true

Any ideas?

Bye!
 
Old 09-02-2009, 01:23 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Eh.. you want to assign a value to the existe variable, right? Then don't expand ($) it. i.e.:
Code:
existe="true"
That's another thing -- you should probably be quoting string values. That helps when you're doing logical comparisons, so if you bump into an unset / no-value variable you won't get a syntactical error.
 
Old 09-03-2009, 12:11 PM   #5
IPAddress
LQ Newbie
 
Registered: Apr 2009
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by anomie View Post
Eh.. you want to assign a value to the existe variable, right? Then don't expand ($) it. i.e.:
Code:
existe="true"
That's another thing -- you should probably be quoting string values. That helps when you're doing logical comparisons, so if you bump into an unset / no-value variable you won't get a syntactical error.
Well, i'm finishing the script, but theres still one problem. Now the script is this:
Code:
# cat /root/ReplicarDns.sh
#! /bin/bash

grep -v ".in-addr.arpa" list.txt >> lista
chmod 777 /var/www/html/ipaddress/lista
rm -f list.txt
cd /var/named/data
declare -a dominios=( * )
rev /var/www/html/ipaddress/lista | cut -b 2- | rev >> /var/www/html/ipaddress/dominios.txt
rm -f /var/www/html/ipaddress/lista
CantDominios=`sed -n '$=' /var/www/html/ipaddress/dominios.txt`
existe="false"
Eliminar="logging {category lame-servers { null; };};"
(echo "g/${Eliminar}/d"; echo 'wq') | ex -s /etc/named.conf.BK
for (( j = 1 ; j <= $CantDominios ; j++ )) do
    Dominio=`head -n $j /var/www/html/ipaddress/dominios.txt | tail -n 1`
    for (( i = 0 ; i < ${#dominios[@]} ; i++ )) do
        [ ${dominios[$i]} == $Dominio ] && existe="true"
    done
    if ( $existe=="false" ) then
    echo 'The Domain ' "$Dominio" ' exist? ' $existe
        echo ''  >> /etc/named.conf.BK
        echo 'zone "'$Dominio'" {' >> /etc/named.conf.BK
        echo '       type slave;' >> /etc/named.conf.BK
        echo '           file "/var/named/data/'$Dominio'";' >> /etc/named.conf.BK
        echo ''          >> /etc/named.conf.BK
        echo ''          >> /etc/named.conf.BK
        echo '            masters {xxx.yyy.www.zzz;' >> /etc/named.conf.BK
        echo '            };' >> /etc/named.conf.BK
        echo '};' >> /etc/named.conf.BK
    fi
    existe="false";
done
echo '' >> /etc/named.conf.BK
echo 'logging {category lame-servers { null; };};' >> /etc/named.conf.BK
 rm -f /var/www/html/ipaddress/dominios.txt
#/etc/init.d/named restart
But for some reason its allways entering in the if:
Code:
if ( $existe=="false" ) then
Why?

Thanks.

Bye!
 
Old 09-03-2009, 12:23 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Because ( <test expression> ) simply runs the test expression in a sub-shell where it is interpreted as a command (so should generate "bash: true==false: command not found"; did you not see that?).

Test expressions need to appear in one of the three test formats, the best of which is [[ <test expression> ]]
 
Old 09-03-2009, 01:52 PM   #7
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by IPAddress
Code:
if ( $existe=="false" ) then
And to add to the previous comment, you're missing a semicolon as well.

Code:
if [ "${existe}" == "false" ] ; then
 
Old 09-03-2009, 01:53 PM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
This:
if ( $existe=="false" ) then
should be this:
if [[ "$existe" = "false" ]] ; then
 
Old 09-03-2009, 02:30 PM   #9
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
@catkin & @gnashley: I'm curious why you recommend the extended test command - [[ ... ]] - in this case instead.

Code:
$ _foo='barbazboo'
$ if [ "${_foo}" == "barbazboo" ] ; then echo 'OK!' ; fi
OK!
$ unset _foo
$ if [ "${_foo}" == "barbazboo" ] ; then echo 'OK!' ; fi

Last edited by anomie; 09-03-2009 at 02:32 PM.
 
Old 09-03-2009, 02:35 PM   #10
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Also, not to nitpick too much (and pollute poor OP's thread), but I dislike using -
Code:
=
- as a comparison operator. I know it's syntactically (and logically) legit with bash, but it sends off alarm bells in my mind because it looks like strictly an assignment operator in many other languages.
 
Old 09-03-2009, 03:24 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
[[ ]] was introduced to the shell after [ ] to compensate for some of its deficiencies. See http://tldp.org/LDP/abs/html/testconstructs.html and search for "The [[ ]] construct is the more versatile Bash version of [ ]".
 
Old 09-03-2009, 03:26 PM   #12
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
That's what I linked to in my post. I was curious why you recommended it in this particular case.
 
Old 09-04-2009, 12:46 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS is just generally more robust

"Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example, the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.
"

plus I read somewhere it copes better/correctly with unassigned/empty args.
Basically, I was told on my (ksh) course many yrs ago the whys & wherefores of the various options to 'check' something, and told to stick to it ie [[ ]].
Personally I always do. Its basically the 'best' (see prev remarks) and it's easier than trying to remember the different limitations of 'test' vs bare 'if' vs [ ] vs [[ ]].
YMMV
 
Old 09-04-2009, 01:19 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by anomie View Post
That's what I linked to in my post. I was curious why you recommended it in this particular case.
I am sorry I didn't notice the link. For the reasons mentioned by chrism01, I recommend it in all cases.
 
Old 09-04-2009, 11:17 AM   #15
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Thanks for the musings, guys. I'll think this through some more. On a normal week I whip together a half dozen bash scripts to automate things; I try to stay with a set of best practices to make life easer.

It does bother me that [[ ... ]] is getting away from Bourne shell compatibility. But the reality is I'm working on 90% Linux hosts these days anyway, where bash is default.

-------

@IPAddress: sorry to digress on your thread. I hope your question was answered.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
compare strings in shell scripting culin Linux - General 10 01-24-2013 01:33 AM
Simple Scripting Problem [Comparison of several strings] thomasknowles Programming 7 07-22-2008 06:53 AM
shell (bash) scripting - strings vs. integers bullfrog1870 Linux - Newbie 3 11-02-2006 08:51 PM
Bash scripting: column-ize file of varying length strings Quantum0726 Programming 4 08-13-2005 06:19 PM
Shell scripting: exclamation marks in strings Dark_Helmet Programming 9 06-16-2003 06:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration