LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? (https://www.linuxquestions.org/questions/solaris-opensolaris-20/converting-script-from-linux-gnu-bash-4-to-solaris-bash-2-05-any-cheat-sheet-4175460446/)

oly_r 05-02-2013 10:12 AM

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.

jlliagre 05-02-2013 11:48 AM

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

oly_r 05-02-2013 12:53 PM

Quote:

Originally Posted by jlliagre (Post 4943495)
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

chrism01 05-03-2013 12:32 AM

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.

cfajohnson 05-03-2013 01:02 AM

Quote:

Originally Posted by oly_r (Post 4943431)
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?

oly_r 05-03-2013 07:28 AM

First let me say THANKS for your help. i'm at the head to the wall stage :banghead:

Ok, in order as they came

Code:

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

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 :doh:

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


oly_r 05-03-2013 08:25 AM

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.


All times are GMT -5. The time now is 11:22 PM.