Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-04
13
Medium Priority
?
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 2000 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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 2000 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

661 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