SYNTAX:
while read l
do
export filename=$l
if [[ `echo $filename | sed -n 's:TEST.*/TEST:&:p'` -ne 0 ]];
then
echo "both are same : $filename"
else
echo "diff: $filename"
fi
done < scripts2.txt*
Problem: Normally its working fine. But for the below data it throws syntax error...
<TEST><![BIGDATA[$EXECUTE SCRIPT ON ALL SERVER $WELCOME#]]></TEST>
Pls let me know what was the exact problem is????
As a general rule, don't try to parse XML/HTML with regular expressions. XML is not a regular language, and you will run into an endless stream of problems. What is it that you are ultimately trying to do? There's probably an easier way to approach the problem.
It is hard to understand what you wish to perform. The obvious mistake in the code above is that you are trying to make an arithmetic comparison (-ne) with something that is not a number.
Your echo ... | sed ... expression for the input above, should produce the string "<&>" and you are testing if this is not equal to zero.
Again, clarifying your intentions would enable other to help you.
Related
I think I'm going crazy, I have a different script that's working fine with the usual way of passing the output of bc into a variable but this one I just can't get working.
The relevant bits of code are this:
PERCENT_MARKUP=".5"
$percentonumber=$(bc -l <<<"$PERCENT_MARKUP/100")
echo "Percent to number = $percentonumber"
$numbertomultiply=$(bc -l <<<"$percentonumber + 1")
echo "Number to multiply = $numbertomultiply"
$MARKUP=$(bc -l <<<"$buyVal * $numbertomultiply")
#$MARKUP=$(bc -l <<<"$buyVal * (1+($PERCENT_MARKUP/100))")
echo "Markup = $MARKUP"
Originally, I only had the second to last line that's currently commented out but I broke it down to try and troubleshoot.
The error I'm getting when I'm trying to run it is:
./sell_zombie_brains: line 65: =.00500000000000000000: command not found
where the .0050000000000000 is replaced by the output of bc. I've even tried the following at multiple points in the file including right after #!/bin/bash with and without -l
$test=`bc -l <<<"1"`
echo "$test"
echo
$test=$(bc -l <<<"1")
echo "$test"
echo
$test=$(echo "1"|bc -l)
echo "$test"
echo
And each one outputs ./sell_zombie_brains: line 68: =1: command not found
I'm really at my wits end. I don't know what I'm missing here. Any insight as to why it's behaving this way is appreciated.
You can't assign variables with the sigil $ :
instead of
$percentonumber=$()
it's
percentonumber=$()
If you are a bash beginner, some good pointers to start learning :
https://stackoverflow.com/tags/bash/info
FAQ
Guide
Ref
bash hackers
quotes
Check your script
And avoid people recommendations saying to learn with tldp.org web site, the tldp bash guide -ABS in particular) is outdated, and in some cases just plain wrong. The BashGuide and the bash-hackers' wiki are far more reliable.
I'm trying to write a short script that checks for verizon fios availability by zip code from a list of 5 digit us zip codes.
The basis of this I have working but comparing the recived output from curl to the expected output in the if statements isn't working.
I know there is a better & cleaner way to do this however I'm really interested in what is wrong with this method. I think it's something to do with the quotes getting jumbled up.
Let me know what you guys think. I originally thought this would be a quick little script. ha. Thanks for the help
Here is what I have so far:
#!/bin/bash
Avail='<link rel="canonical" href="http://fios.verizon.com/fios-plans.html#availability-msg" />'
NotAvail='<link rel="canonical" href="http://fios.verizon.com/order-now.html#availability-msg" />'
while read zip; do
chk=`curl -s -d "ref=GIa6uiuwP81j047HjKMHOwEyW4QJTYjG&PageID=page9765&AvailabilityZipCode=$zip" http://fios.verizon.com/availability_post4.php --Location | grep "availability-msg"`
#echo $chk
if [ "$chk" = "$Avail" ]
then
fios=1
elif [ "$chk" = "$NotAvail" ]
then
fios=0
else
fios=err
fi
echo "$zip | $fios"
done < zipcodes.txt
Most likely, the line read from curl ends in CR/LF. grep will take the LF as a line-end, and leave the CR in the line, where it will not match either of your patterns. (Other whitespace issues could also cause a similarly invisible mismatch, but stray CR's are very common since HTTP insists on them.)
The easiest solution is to use a less specific match, like a glob or regex; these are both available with bash's [[ (rather than [) command.
Eg.:
if [[ $chk =~ /fios-plans\.html ]]; then
will do a substring comparison
Ok I know my question seems confusing. So i'll explain it here. So i'm making a mmo with bash script (i am bored don't say do it with java or c++ or something like that please) which i won't really explain other than that when registering I want it so I can have a if statement see if they have anything provocative in their username and then tell them this and then make them make a different username. I'm just trying to make it more appropriate and all. So to make it so I can have the word seen by my if statement I need to have it like this pretty much
if [ var == provocative word ]; then
echo "You have a provocative word in your username. Please change"
fi
But to do this I'll need it to look if word is in the statement.
I know that in java it is just by doing 'word*' the star making it so if it sees the word it will so whatever the if said even though the thing might of been 'wordghdksjgh'. Thanks in advance for whoever answers.
To check if a variable contains a substring, you can use:
if [[ $var == *"foo"* ]]
then
echo "The variable contains the substring 'foo'"
fi
In addition to pattern-matching using
if [[ $var = *foo* ]]; then
you can also use regular expressions:
if [[ $var =~ foo ]]; then # Successfully match anything with "foo" as a substring
The nocasematch option applies to regular expression matches as well.
I've coded the following script to add users from a text file. It works, but I'm getting an error that says "too many arguments"; what is the problem?
#!/bin/bash
file=users.csv
while IFS="," read USRNM DOB SCH PRG PST ENROLSTAT ; do
if [ $ENROLSTAT == Complete ] ;
then
useradd $USRNM -p $DOB
else
echo "User $USRNM is not fully enrolled"
fi
done < $file
#cat users.csv | head -n 2 | tail -n 1
Use quotes. Liberally.
if [ "$ENROLSTAT" = Complete ]
(It's a single equal sign, too.) My greatest problem in shell programming is always hidden spaces. It's one of the reasons I write so much in Perl, and why, in Perl, I tell everyone on my team to avoid the shell whenever running external programs. There is just so much power in the shell, with so many little things that can trip you up, that I avoid it where possible. (And not where not possible.)
I was reading though this other question which has some really good regex's for the job but as far as I can see non of them work with BASH commands as BASH commands don't support such complex rexeg's.
if echo "http://www.google.com/test/link.php" | grep -q '(https?|ftp|file)://[-A-Z0-9\+&##/%?=~_|!:,.;]*[-A-Z0-9\+&##/%=~_|]'; then
echo "Link valid"
else
echo "Link not valid"
fi
But this doesn't work as grep -q doesn't work ...
Edit, ok I just realised that grep had an "extended-regex" (-E) option which seems to make it work. But if anyone has a better/faster way I would still love to here about it.
The following works in Bash >= version 3.2 without using grep:
regex='(https?|ftp|file)://[-[:alnum:]\+&##/%?=~_|!:,.;]*[-[:alnum:]\+&##/%=~_|]'
string='http://www.google.com/test/link.php'
if [[ $string =~ $regex ]]
then
echo "Link valid"
else
echo "Link not valid"
fi
I simplified your regex by using [:alnum:] which also matches any alphanumeric character (e.g. Э or ß), but support varies by the underlying regex library. This is another potential simplification which uses + instead of * and a repeated sequence (although your second sequence is different from the first).
regex='(https?|ftp|file)://[-[:alnum:]\+&##/%?=~_|!:,.;]+'
Since I don't have enough rep to comment above, I am going to amend the answer given by Dennis above with this one.
I incorporated Christopher's update to the regex and then added more to it so that the URL has to at least be in this format:
http://w.w (has to have a period in it).
And tweaked output a bit :)
regex='^(https?|ftp|file)://[-A-Za-z0-9\+&##/%?=~_|!:,.;]*[-A-Za-z0-9\+&##/%=~_|]\.[-A-Za-z0-9\+&##/%?=~_|!:,.;]*[-A-Za-z0-9\+&##/%=~_|]$'
url='http://www.google.com/test/link.php'
if [[ $url =~ $regex ]]
then
echo "$url IS valid"
else
echo "$url IS NOT valid"
fi
Probably because the regular expression is written in PCRE syntax. See if you have (or can install) the program pcregrep on your system - it has the same syntax as grep but accepts Perl-compatible regexes - and you should be able to make that work.
Another option is to try the -P option to grep, but the man page says that's "highly experimental" so it may or may not actually work.
I will say that you should think carefully about whether it's really appropriate to be using this or any regex to validate a URL. If you want to have a correct validation, you'd probably be better off finding or writing a small script in, say, Perl, to use the URL validation facilities of the language.
EDIT: In response to your edit in the question, I didn't notice that that regex is also valid in "extended" syntax. I don't think you can get better/faster than that.