LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 05-02-2013, 10:12 AM   #1
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Rep: Reputation: Disabled
Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet?


Ok, i'm trying to port bash script from my Linux system to Solaris. The end user "doesn't like linux" or apparently upgrading utilities. He has a Solaris 9 system with bash 2.05.

I'm also having a weird issue where the commands are working on the command line but not in the scripts even though the #! line is pointing to the same bash returned by $0 (i don't trust echo $SHELL). I'm sure this is probably a head slap issue that i'm just missing something easy but any help would be appreciated.

I have a large script (kind of grew from five or six individual scripts) in bash. I have many commands i'm used to using that are now failing on the solaris test box. Does anyone have or know of a conversion type cheat sheet to help me change things over.

So far these are the command giving me fits and making me go through a lot of revisions to find the one that works. If you know any better ways to do this please let me know.

Code:
count=`grep -o ":" <<< $testLine| wc -l`
now 
count=`echo $testLine|sed 's/a-zA-Z0-9\.\-\//g' |tr -d '\n'|wc -m`

if [[ ]]  ## reduced to [ ] seems to work
if [ x == y ] ## now =

(( mycounter++ )) ## failed at (
mycounter=`expr $mycounter + 1`

for ((i=0;i<10;i++));do echo;done  ## fails at (, can't get any for one liner to work.
                                    ## this is also a command that works on the cmdline 
                                    ## but not in script

for i in {1..8};   ## this fails takes the {1..8} as static value, now writing out 1 2 3 4 5 6 7 8

grep "^[[:alpha:]]"     ## this fails unexpected [, now using [a-zA-Z]

NevVal=$(($myval/$myDivisor))  ## debating between
NewVal=`echo "$myval/$myDivisor" |bc`
NewVal=`expr $myval / $myDivisor`   ## leaning towards this one.

sed -i "/$staticIP/d" $Full_Listing
now
sed "/$staticIP/d" < $Full_listing > tmp_Full_listing
mv tmp_Full_listing $Full_listing
Thanks.
 
Old 05-02-2013, 11:48 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Code:
for ((i=0;i<10;i++));do echo;done  ## fails at (, can't get any for one liner to work.
This should work:

Code:
for i in $(awk 'BEGIN {for(i=0;i<10;i++) print i}');do echo $i;done
 
Old 05-02-2013, 12:53 PM   #3
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jlliagre View Post
This should work:

Code:
for i in $(awk 'BEGIN {for(i=0;i<10;i++) print i}');do echo $i;done
Just like other ways i've tried it doesn't work. I checked my typing multiple times but it just sits there when i hit enter
 
Old 05-03-2013, 12:32 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
For Solaris that old, you're probably better off using ksh. I used it for years on Solaris (& HP-UX & Dynix); pretty good.

The ksh built-ins are usually the same, but the version of ksh there is probably more capable (than that version of bash) eg probably handle [[ ]], maybe even (( ))
See http://www.kornshell.com/

External cmds like grep and sed are a completely separate qn. The grymoire page may help; it tends to be a bit old school http://www.grymoire.com/Unix/

Have you got or can you get/upgrade the sfw (sun freeware) pkg for that? Has all the GNU version utils.

Why don't you try any version of bash you can find on that system ie both $0 and $SHELL; you might be surprised.
 
Old 05-03-2013, 01:02 AM   #5
cfajohnson
LQ Newbie
 
Registered: Aug 2012
Distribution: Linux Mint 17
Posts: 22

Rep: Reputation: Disabled
Quote:
Originally Posted by oly_r View Post
Code:
count=`grep -o ":" <<< $testLine| wc -l`
Code:
count=`grep -o ":"` <<.
 $testLine| wc -l
.
Or:

Code:
count=`echo "$testline" | grep -o ':' | wc -l`
Or (without any external commands):

Code:
colons=${testline//[!:]/}
count=${#colons}
Quote:
Code:
if [[ ]]  ## reduced to [ ] seems to work
if [ x == y ] ## now =

(( mycounter++ )) ## failed at (
mycounter=`expr $mycounter + 1`

The portable method is:
Code:
mycounter=$(( mycounter + 1 ))
Or:

Code:
: $(( mycounter += 1 ))
Quote:
Code:
for ((i=0;i<10;i++));do echo;done  ## fails at (, can't get any for one liner to work.
                                    ## this is also a command that works on the cmdline 
                                    ## but not in script

for i in {1..8};   ## this fails takes the {1..8} as static value, now writing out 1 2 3 4 5 6 7 8

Brace expansion wasn't introduced until bash 3.0, but all your other above code works in my copy of 2.05.

Instead, you can use seq:
Code:
for i in `seq 8`
Or:
Code:
i=0
while [ $(( i += 1 )) -le 8 ]
Quote:
Code:
grep "^[[:alpha:]]"     ## this fails unexpected [, now using [a-zA-Z]
Code:
That's a problem with grep, not bash; it's probably an old version.
Quote:
Code:
NevVal=$(($myval/$myDivisor))  ## debating between

That works as far back as bash-1.14

Are you sure the script is being executed with bash?
 
1 members found this post helpful.
Old 05-03-2013, 07:28 AM   #6
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
First let me say THANKS for your help. i'm at the head to the wall stage

Ok, in order as they came

Code:
grep -o
will not work -o not recognized so yes i should have separated from bash issues

Code:
colons=${testline//[!:]/}          does not work - bad substitution error
count=${#colons}
Code:
mycounter=$(( mycounter + 1 ))
errors with - 'mycounter=$' not expected

Code:
: $(( mycounter += 1 ))
errors with - syntax error a line 6: '(' unexpected

Code:
for i in `seq 8`
errors seq not found

Code:
i=0
while [ $(( i += 1 )) -le 8 ]
errors with (you guessed it) '(' unexpected

nothing with double (()) is accepted.

Code:
grep "^[[:alpha:]]"
Sorry, this does actually work

Code:
NevVal=$(($myval/$myDivisor))
Quote:
That works as far back as bash-1.14

Are you sure the script is being executed with bash?
To test i write a minimal script with just the questionable command in it. This is what i used to test
Code:
NevVal=$(($myval/$myDivisor))
Code:
#!/usr/bin/bash   ## also run /bin/bash 
myval=10
myDivisor=5

NevVal=$(($myval/$myDivisor))
My response it
Code:
test7.sh: syntax error at line 6: 'NewVal=$' unexpected
And no, i'm not sure anything is running in bash at any time To try and check i added a
Code:
echo $SHELL
to a couple of the mini-scripts and it reports back /bin/bash whether i have /bin/bash or /usr/bin/bash. the two files are the same (not links)


AND A NEW ONE

case isn't working as expected, my mini test is as follows
Code:
#!/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
i get as a response the count then everything falls through the catchall.
Code:
/bin/bash
7
everything else
3
everything else
4
everything else
1
everything else
....

Last edited by oly_r; 05-03-2013 at 07:43 AM.
 
Old 05-03-2013, 08:25 AM   #7
oly_r
Member
 
Registered: Dec 2011
Posts: 31

Original Poster
Rep: Reputation: Disabled
I HAVE FOUND one big mistake i've been doing. I've been calling the script from the command line with sh script.sh like i've always done (i guess not). if i just make it executable and rely on the "shebang" a lot of these irritating issues go away. NOT all. the case problem still exists.

Actually the case error was caused by using wc -l (it pads the answer) so switching to a grep -c fixed that. Thanks everyone.

Last edited by oly_r; 05-03-2013 at 08:49 AM.
 
  


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
LXer: Cheat Sheet collection for Linux users LXer Syndicated Linux News 0 05-17-2011 07:10 PM
converting a py script to bash saawan Linux - Software 2 05-06-2009 11:56 AM
Linux Cheat Sheets (awk, ed, sed, bash, screen, perl) pkrumins Linux - Newbie 1 01-31-2008 03:51 PM
converting bash script to csh DJOtaku Programming 8 02-13-2006 05:35 AM
Linux command cheat sheet freddieB Linux - General 1 02-22-2002 10:50 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 08:47 PM.

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