Solved

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

Posted on 2013-12-04
13
548 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now