Converting from Linux bash (GNU) to Solaris script syntax errors


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting from Linux bash (GNU) to Solaris script syntax errors
# 1  
Old 05-01-2013
Converting from Linux bash (GNU) to Solaris script syntax errors

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
Code:
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
Code:
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.
Code:
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

# 2  
Old 05-01-2013
Code:
# 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....
# 3  
Old 05-01-2013
Quote:
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.
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.
Code:
cat $Full_file |sed "/$StaticIP/d" > tmpfile

Instead:
Code:
sed "/$StaticIP/d" $Full_file > tmpfile

More fundamentally, instead of testing with grep, then running sed, try just running grep -v:
Code:
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 ----------

Quote:
grep "^[[:alpha]]"
Should be grep "^[[:alpha:]]"
This User Gave Thanks to hanson44 For This Post:
# 4  
Old 05-01-2013
Quote:
Originally Posted by hanson44
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.
Code:
cat $Full_file |sed "/$StaticIP/d" > tmpfile

Instead:
Code:
sed "/$StaticIP/d" $Full_file > tmpfile

^^^ doesn't work, i tried that first
Quote:
Originally Posted by hanson44

More fundamentally, instead of testing with grep, then running sed, try just running grep -v:
Code:
while read StaticIP; do
   grep -v -w $StaticIP $Full_file > tmpfile
   mv tmpfile $Full_file
done

^^^ thanks, using this ( bad thing is i've done this before Smilie)
Quote:
Originally Posted by hanson44

---------- Post updated at 02:01 PM ---------- Previous update was at 02:00 PM ----------



Should be grep "^[[:alpha:]]"
^^^ This is correct in the actual script.
# 5  
Old 05-01-2013
Quote:
cat $Full_file |sed "/$StaticIP/d" > tmpfile

Instead:

Code:
sed "/$StaticIP/d" $Full_file > tmpfile

^^^ doesn't work, i tried that first
Well, there must have been a typo when you tried it, or some other error. The cat $Full_file | is not needed, period.
# 6  
Old 05-01-2013
On Solaris 9 you should use/usr/xpg4/bin/grep instead of grep,

Instead of the first loop, try:
Code:
/usr/xpg4/bin/grep -vwf "$static_ip_file" "$full_file" > newfile




--
( 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 )

Last edited by Scrutinizer; 05-01-2013 at 04:30 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 05-02-2013
Quote:
Originally Posted by Scrutinizer
On Solaris 9 you should use/usr/xpg4/bin/grep instead of grep,

Instead of the first loop, try:
Code:
/usr/xpg4/bin/grep -vwf "$static_ip_file" "$full_file" > newfile




--
( 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 )

THANKS, Big difference

---------- 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Need help to upgrade GNU bash in Solaris 10

Hi All, I want upgrade GNU bash in solaris 10. present bash is -bash-3.00$ and I downloaded new bash that is bash 4.4 (correct me if that is wrong). and my server is local zone, hosted on global zone. please help how can i proceed what is the process. I heard that need install first then it... (2 Replies)
Discussion started by: Harish4032
2 Replies

2. Shell Programming and Scripting

Converting macro to bash script

Gents, Please can you help me with this. When column 49 == 2 Before X 4714 14710 69445.00 19257.001 1218 12271 69596.00 19460.00 19478.001 X 4714 14710 69445.00 19257.001 1228 12292 69596.00 19480.00 19480.001 After X 4714 14710 69445.00 19257.001 1218... (1 Reply)
Discussion started by: jiam912
1 Replies

3. Shell Programming and Scripting

Converting awk script from bash to csh

I have the following script set up and working properly in bash. It basically copies a set of lines which match "AS1100002" from one file and replaces the same lines in another file. awk -vN=AS1100002* 'NR==FNR { if($1 ~ N)K=$0; next } { if($1 in K) $0=K; print }' $datadir/file1... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

4. Shell Programming and Scripting

Sed, bash problems migrating from Cray to GNU/Linux

So, I have a series of ASCII files, all named something like mrkxxxxz.tmp (say, mrk1001z.tmp, mrk1002z.tmp, mrk1003z.tmp,...) -- these are .tmp files created by a large simulation program, and each different .tmp file represents a different parameter space used in the simulation). The simulations... (2 Replies)
Discussion started by: johnny_canucl
2 Replies

5. Emergency UNIX and Linux Support

Seek help on shell script syntax errors

I want to delete archivelog files that has been archived and applied from primary database to standby database. This piece of script is working in Linux server. However, I copy it to Unix server with tiny modification. It won't work and generate the error message. I have checked code carefullt... (8 Replies)
Discussion started by: duke0001
8 Replies

6. Programming

Project ideas for GNU/Linux C and BASH development

Hello everyone... I'm trying to find an interesting project to work on for my master thesis. I like GNU/Linux C development and BASH scripting. Please give me any idea that flashes in your mind. I thank you in advance... (3 Replies)
Discussion started by: jonx
3 Replies

7. Shell Programming and Scripting

Solaris bash syntax different from Linux?

I have a script that's meant to check the disk usage on a particular volume and delete the oldest logfile if it's over a certain percentage. It runs fine on a Linux machine, but on a Solaris one, I get this error: diskspace_check.sh: syntax error at line 3: `diskspace=$' unexpected I assume... (2 Replies)
Discussion started by: cara_k
2 Replies

8. Shell Programming and Scripting

linux bash script to sun solaris

Hi guys, I seek a solution for this action for Sun solaris. find /sapmnt/${up}/global -prune -printf "%m %M %u %g %p\n" > $DAT1 The Application/Utilities in Sun Solaris are to old and cant understand "-printf". An update for Application/Utilities is exist, but not possible to implement... (6 Replies)
Discussion started by: ixibits
6 Replies

9. Shell Programming and Scripting

Subtracting time with awk - BASH/Debian GNU Linux

I'm sure this is simple and I've been looking at examples for days on end but can't seem to come to grips with awk. What I have: mplayer -v dvd:// -identify -vo null -ao null -nolirc -nojoystick -frames 0 2>/dev/null >> /tmp/MplayerOut ChapterStart=($(grep CHAPTERS: /tmp/MplayerOut |sed... (3 Replies)
Discussion started by: rickenbacherus
3 Replies

10. Shell Programming and Scripting

Converting bash script to csh

Hi, I'm a beginner in scripting and I recently wrote a bash script that would've worked fine until I realized it needed to be written in csh. Could someone please show me how to correctly change the syntax from bash to csh in this script? Any help will be greatly appreciated. I can provide more... (4 Replies)
Discussion started by: Kweekwom
4 Replies
Login or Register to Ask a Question