Original script written on CentOS 6.3 with GNU bash 4.1.2
Destination system is Solaris 9 with GNU bash 2.05 (not changeable by me)
I have a script written on the linux side but now we need to provide a version to another site that "doesn't like linux". I've been going through changing the [[ ]] or (( )) and the == but have really hit a wall with the sed -i.
The script is very quick with the sed -i being used but man is it slow to write to a temp file and them mv back to the primary.
Basically
while read StaticIP
do
grep -w $StaticIP $Full_file > /dev/null
if [ $? = 0 ]; then
cat $Full_file |sed "/$StaticIP/d" > tmpfile
mv tmpfile $Full_file
fi
do
This all went very smoothly when i had the if construct like this
sed -i $StaticIP $Full_file
But now this whole script has gone from 15 - 20 minutes to 2+ hours to run.
I cant use any of the following that i'm used to.
for i {1..8}; ## this fails takes the {1..8} as static value
grep "^[[:alpha]]" ## this fails unexpected [
NevVal=$(($myval/$myDivisor))
for ((i=0;i<=10;i++));do somthing;done ## works cmdline fails in script
# Some version of sed have an option, -i, that edits the file in situ
# A trick that allows redirection to the same file is:
{ rm FILE; sed -e '...' > FILE; } < FILE
also, make sure that you have #!/bin/bash in your script - to make sure you're using the desired shell interpreter....
I don't believe that. It does not make sense. sed -i also creates a temp file, behind the scenes.
If you do a test that shows sed -i is faster, could you post?
Using cat here is totally not needed.
cat $Full_file |sed "/$StaticIP/d" > tmpfile
Instead:
sed "/$StaticIP/d" $Full_file > tmpfile
More fundamentally, instead of testing with grep, then running sed, try just running grep -v:
while read StaticIP; do
grep -v -w $StaticIP $Full_file > tmpfile
mv tmpfile $Full_file
done
---------- Post updated at 02:01 PM ---------- Previous update was at 02:00 PM ----------
--
( As a side-note: the -w option may not be sufficient, because the dots in the ip address mean "any character" so that means that unintended adresses may be matched. Look into the -F and -x options, but that goes for both the Linux and the Solaris version )
---------- Post updated at 09:48 AM ---------- Previous update was at 06:46 AM ----------
One of the worst parts of this is that some commands are working on the cmdline but not in the script. I've verified i'm using the same shell in the #! line as i'm working the command line. This is terribly aggravating.
How is this script being run when not from terminal? Cron or the like? Make sure its PATH is up to par with yours, things run by cron and other such automatic tools get quite minimal PATHs.
That way, you are invoking /bin/sh , an entirely different shell, and you are bypassing a shebang if it is present in your script. You should run it as bash script.sh ( the same holds true for Linux by the way ) or make the script executable, use a shebang and execute it as /path/to/script.sh .
CRAP, i just tested using the same test files with the executable now set and several of these now work. I thought the shebang overrode however it was called. I've used sh anyscript.sh for years on my bash scripts. Of course it has been a while since i've been on solaris with any frequency.
Funny thing is my case still doesn't work.
#!/usr/bin/bash
echo $SHELL
while read myDomain
do
myCounter=`cat domain_file.txt |awk '{print $3}'|grep "^$myDomain"|wc -l`
echo $myCounter
case $myCounter in
0) echo "found a 0";;
1) echo "found a 1";;
2) echo "found a 2";;
*) echo "everything else";;
esac
done < domains_only.txt
Gets the counts right but everything falls through to the all other entry.