Solved

What is the converted result of this Bash script to sh for Solaris 10?

Posted on 2013-12-04
13
555 Views
Last Modified: 2013-12-04
What is the converted result of this Bash script to #!/sbin/sh for Solaris 10?

 
set JAVA_HOME=/u00/app/jdk1.7.0_45
set AS_JAVA=/u00/app/jdk1.7.0_45
set JAVA_BINDIR="${JAVA_HOME}/bin"

echo "Set PATH with Java"
echo $PATH | /bin/grep -v "${JAVA_BINDIR}"
if ( $status == 0 ) then
     setenv PATH "${JAVA_BINDIR}:${PATH}"
endif

GLASSFISH=/u00/app/oracle/product/glassfish4
DERBY_BIN=/u00/app/oracle/product/glassfish4/javadb/bin

case "$1" in
‘start’)
  echo "Starting GlassFish from $GLASSFISH" > /tmp/GlassFishStarted.txt
  /bin/su glassfish "$GLASSFISH/bin/asadmin" start-database
  /bin/su glassfish "$GLASSFISH/bin/asadmin" start-domain appsand01
  ;;
stop)
  echo "Stopping GlassFish from $GLASSFISH"
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" stop-domain appsand01
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" stop-database
  ;;
restart)
  $0 stop
  $0 start
  ;;
status)
  echo "# GlassFish at $GLASSFISH:"
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-domains | grep -v Command
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-domains | grep -q "appsand01 running"
  if [ $? -eq 0 ]; then
    sudo -u glassfish -E "$GLASSFISH/bin/asadmin" uptime | grep -v Command
    echo "\n# Deployed applications:"
    sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-applications --long=true --resources | grep -v Command
    echo "\n# JDBC resources:"
    sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-jdbc-resources | grep "jdbc/"
  fi
  echo "\n# Derby:"
  sudo -u glassfish -E "$DERBY_BIN/NetworkServerControl" ping | sed "s/^.* : //"
  ;;
*)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
  ;;
esac

Open in new window

0
Comment
Question by:bcarlis
  • 6
  • 6
13 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 39696794
I'm confused. Didn't you initially write "converted to csh"??

This is for csh:

#!/bin/csh
echo "Set java shell variables"
set JAVA_HOME=/u00/app/jdk1.7.0_45
set AS_JAVA=/u00/app/jdk1.7.0_45
set JAVA_BINDIR="${JAVA_HOME}/bin"

echo "Set PATH with Java"
echo $PATH | /bin/grep -q -v "${JAVA_BINDIR}"
if ( $status == 0 ) then
     setenv PATH "${JAVA_BINDIR}:${PATH}"
endif

set GLASSFISH=/u00/app/oracle/product/glassfish4
set DERBY_BIN=/u00/app/oracle/product/glassfish4/javadb/bin

switch ( $1 )
case start:
  echo "Starting GlassFish from $GLASSFISH" > /tmp/GlassFishStarted.txt
  /bin/su glassfish "$GLASSFISH/bin/asadmin" start-database
  /bin/su glassfish "$GLASSFISH/bin/asadmin" start-domain appsand01
  breaksw
case stop:
  echo "Stopping GlassFish from $GLASSFISH"
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" stop-domain appsand01
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" stop-database
  breaksw
case restart:
  $0 stop
  $0 start
  breaksw
case status:
  echo "GlassFish at ${GLASSFISH}:"
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-domains | grep -v Command
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-domains | grep -q "appsand01 running"
  if ( $status == 0 ) then
   sudo -u glassfish -E "$GLASSFISH/bin/asadmin" uptime | grep -v Command
   echo
   echo "# Deployed applications:"
   sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-applications --long=true --resources | grep -v Command
   echo
   echo "# JDBC resources:"
   sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-jdbc-resources | grep "jdbc/"
  endif
  echo
  echo "# Derby:"
  sudo -u glassfish -E "$DERBY_BIN/NetworkServerControl" ping | sed "s/^.* : //"
  breaksw
default:
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
endsw

Open in new window

0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 39696834
"sh" should need just a few modifications:

#!/sbin/sh
JAVA_HOME=/u00/app/jdk1.7.0_45
AS_JAVA=/u00/app/jdk1.7.0_45
JAVA_BINDIR="${JAVA_HOME}/bin"

echo "Set PATH with Java"
echo $PATH | /bin/grep -v "${JAVA_BINDIR}"
if [ $? -eq 0 ]; then
     PATH="${JAVA_BINDIR}:${PATH}"
     export PATH
fi

GLASSFISH=/u00/app/oracle/product/glassfish4
DERBY_BIN=/u00/app/oracle/product/glassfish4/javadb/bin

case $1 in
start)
  echo "Starting GlassFish from $GLASSFISH" > /tmp/GlassFishStarted.txt
  /bin/su glassfish "$GLASSFISH/bin/asadmin" start-database
  /bin/su glassfish "$GLASSFISH/bin/asadmin" start-domain appsand01
  ;;
stop)
  echo "Stopping GlassFish from $GLASSFISH"
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" stop-domain appsand01
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" stop-database
  ;;
restart)
  $0 stop
  $0 start
  ;;
status)
  echo "# GlassFish at $GLASSFISH:"
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-domains | grep -v Command
  sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-domains | grep -q "appsand01 running"
  if [ $? -eq 0 ]; then
    sudo -u glassfish -E "$GLASSFISH/bin/asadmin" uptime | grep -v Command
    echo "\n# Deployed applications:"
    sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-applications --long=true --resources | grep -v Command
    echo "\n# JDBC resources:"
    sudo -u glassfish -E "$GLASSFISH/bin/asadmin" list-jdbc-resources | grep "jdbc/"
  fi
  echo "\n# Derby:"
  sudo -u glassfish -E "$DERBY_BIN/NetworkServerControl" ping | sed "s/^.* : //"
  ;;
*)
  echo "Usage: $0 {start|stop|restart|status}"
  exit 1
  ;;
esac

Open in new window

0
 
LVL 2

Author Comment

by:bcarlis
ID: 39697050
Sorry, I looked at the other scripts in the init.d dir and say the #!/sbin/sh

this is what I am getting from the output
/etc/init.d/glassfish status
Set PATH with Java
/usr/sfw/bin:/usr/sfw/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/ucb:/etc:/usr/openwin/bin:/usr/local/samba/bin:/usr/ccs/bin:/usr/local/bin:.
# GlassFish at /u00/app/oracle/product/glassfish4:
/etc/init.d/glassfish: sudo: not found
/etc/init.d/glassfish: sudo: not found
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .

# Derby:
/etc/init.d/glassfish: sudo: not found

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:bcarlis
ID: 39697053
could you edit the answers and remove my headed information - name, company etc
thank you..
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39697065
Scripts in /etc/init.d are usually run by root. So you don't have to use sudo, a simple "su" should suffice, e.g.:

su - glassfish -c "$GLASSFISH/bin/asadmin list-domains | /usr/xpg4/bin/grep -q -v Command"

Open in new window

In your above lines 17 and 18 you're already using "su". Please note that you must use "su -" to get the target user's environment and that you must prefix the command to be executed with "-c" and surround the command itself with double quotes, so the mentioned line 17 should read:

/bin/su - glassfish -c "$GLASSFISH/bin/asadmin start-database"

To make the "quiet" switch ("-q") of "grep" work under Solaris you must use
"/usr/xpg4/bin/grep" as shown in the example above.
0
 
LVL 2

Author Comment

by:bcarlis
ID: 39697200
Great it compiles... but something is not working

Can't I just su - glassfsh once somehow ?

status)
  echo '# GlassFish at "$GLASSFISH:"'
  /bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" list-domains | grep -v Command
  /bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" list-domains | /usr/xpg4/bin/grep -q "appsand01 running"
  if [ $? -eq 0 ]; then
    /bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" uptime | grep -v Command
    echo '\n# Deployed applications:'
    /bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" list-applications --long=true --resources | /usr/xpg4/bin/grep -v Command
    echo '\n# JDBC resources:'
    /bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" list-jdbc-resources | grep "jdbc/"
  fi
  echo '\n# Derby:'
  /bin/su - glassfsh -c "$DERBY_BIN/NetworkServerControl" ping | sed "s/^.* : //"
  ;;
*)
  echo 'Usage: $0 {start|stop|restart|status}'
  exit 1
  ;;

Open in new window


OUTPUT from above:
# GlassFish at /u00/app/oracle/product/glassfish4
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
Set path shell variable
Set PATH with Java
/usr/bin:
Set GlassFish Environment
/u00/app/jdk1.7.0_45/bin:/usr/bin:
Set Environment
Shortcut aliases
Use "exit" to exit and "help" for online help.

Open in new window


All this is from one su - glassfsh
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
Set path shell variable
Set PATH with Java
/usr/bin:
Set GlassFish Environment
/u00/app/jdk1.7.0_45/bin:/usr/bin:
Set Environment
Shortcut aliases

Open in new window

0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 39697211
Yes, that's what glassfish's profile writes to the terminal.

The question is, do you need anything set by that profile, particularly any "glassfish" specific environment variables?

If you don't, you can use just "su" without the following dash.

If something is needed from the profile - could you set this in the script itself to avoid having to run "su -"?

And you can't "su -" just once, because "su -" without a following command starts its own shell and thus requires a manually typed "exit" to return to the calling script.
0
 
LVL 2

Author Comment

by:bcarlis
ID: 39697215
What is happening is
  /bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" list-domains | grep -v Command

it is getting hung on  if [ $? -eq 0 ]; then ??




Actually how whould I write:

status)
  echo '# GlassFish at "$GLASSFISH:"'
  /bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" 

< list-domains | grep -v Command
< list-domains | /usr/xpg4/bin/grep -q "appsand01 running"
  
if [ $? -eq 0 ]; then
    < uptime | grep -v Command
    echo '\n# Deployed applications:'
    < list-applications --long=true --resources | /usr/xpg4/bin/grep -v Command
    echo '\n# JDBC resources:'
    < list-jdbc-resources | grep "jdbc/"
  fi
  echo '\n# Derby:'
  /bin/su - glassfsh -c "$DERBY_BIN/NetworkServerControl" ping | sed "s/^.* : //"
  ;;
*)
  echo 'Usage: $0 {start|stop|restart|status}'
  exit 1
  ;;

Open in new window


Where asadmin is a command prompt and just want to pipe the commands into the existing prompt? :)

This
/bin/su - glassfsh -c "$GLASSFISH/bin/asadmin"
gives this
asadmin>
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 500 total points
ID: 39697218
You can't pipe commands into a prompt, only into a command/program which reads stdin.
0
 
LVL 2

Author Comment

by:bcarlis
ID: 39697221
Thank you..

/bin/su - glassfsh -c "$GLASSFISH/bin/asadmin" list-domains | grep -v Command

I placed list-domains | grep -v Command in the quotes to make all one command

/bin/su - glassfsh -c "$GLASSFISH/bin/asadmin list-domains | grep -v Command"

otherwise it was stopping at
Oracle Corporation      SunOS 5.10      Generic Patch   January 2005
Set path shell variable
Set PATH with Java
/usr/bin:
Set GlassFish Environment
/u00/app/jdk1.7.0_45/bin:/usr/bin:
Set Environment
Shortcut aliases
Use "exit" to exit and "help" for online help.
asadmin>
0
 
LVL 2

Author Comment

by:bcarlis
ID: 39697223
Thank you very much for your help, I will tweak it tomorrow.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39697224
>> I placed list-domains | grep -v Command in the quotes to make all one command <<

That's indeed what I suggested - see ID: 39697065 above!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question