ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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.
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:
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> ]]
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.
[[ ]] 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 [ ]".
"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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.