Shell Script for Java version switch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script for Java version switch
# 1  
Old 07-03-2013
Shell Script for Java version switch

Hello all I am writing a shell script for unix, and I am trying to get it to ask me if instances are stopped, and then if the version of java running is correct, if not I want it to ask me to switch to the other bit version. I am newish to shell and not clear on how to to input prompts for yes/no etc, I cant get it to run properly,could someone please help?

Code:
SCRIPT=$(basename "$0")
VERSION='$Revision: 6327 $'
VERBOSE=0
NUMBITS=0
YES=0
# , support version specification
function usage {
  cat << EOM
usage: $SCRIPT [-h] [-v] [-V] [-b] [-y]
  where:
    -b bits java bit level 32 or 64
    -h show this message and exit
    -v add verbosity
    -V show version and exit
    -y answer prompts with yes
EOM
        exit 1
}
while getopts ":b:hvVy" opt
do
  case "$opt" in
    b ) NUMBITS=$OPTARG ;;
    h )
      usage
      ;;
    v ) ((VERBOSE+=1)) ;;
 V )    
      echo "$SCRIPT VERSION: $(echo $VERSION | awk '{ print $2 }')"
      exit 0
      ;;
    y ) YES=1 ;;
    * )
      echo "Unexpected option \"$opt\""
      usage
      ;;
  esac
done
shift $(($OPTIND - 1))
# Dependencies:
for DEPENDENCIES in symlink-resolve
do
  if ( which $DEPENDENCIES 2>&1 | grep -e "^which: no " -e "^no ")
  then
    echo "Aborting - unable to resolve dependency: $DEPENDENCIES"
    exit 1
  fi
done
unset DEPENDENCIES
case "$NUMBITS" in
  32|64)
  ;;
  *)
    echo "Invalid bits specified: $NUMBITS"
    usage
  ;;
esac
SHARE=/app/share/java/bin
JAVASHARE=${SHARE}/java
if [ -L "$JAVASHARE" ]
then
# if YES is 0, prompt user asking if instances are stopped
#              if answer is no exit
read -y " Did you stop all instances before this? [y/n] answer
if [ $answer != [-y] ]
then
echo "Cannot proceed becuases instances not stopped" exit
else
echo "Current Java Version running on machine is: $(java -version)
fi
# Is current version what we want?
# If yes say so and exit
read -y "Is this version of Java the current version you want? [y/n]" answer
if [ $answer =! [-y] ]
then exit
else
read -b "Please choose which bit version you want Java at? [32/64]" NUMBITS
if [ $NUMBITS =! [-b] ]
then setServerJava -b $NUMBITS
fi
fi
JAVACUR=$(symlink-resolve $JAVASHARE)
# test and make sure it is directory
# extract the bits from the current
#OLDBITS
  JAVAP=$(basename $JAVACUR)
  JAVAN=$(echo "$JAVAP" | sed -e "s/$OLDBITS/$NUMBITS/")
  JAVANEW=${SHARE}/${JAVANEW}
  # test is dir JAVANEW error exit if not
  cd $SHARE
  rm $JAVASHARE
  ln -s $JAVANEW $JAVASHARE
  # darn I am good

else
  echo "Symlink not found: $JAVASHARE"
fi

# 2  
Old 07-03-2013
This could be bash or ksh or something else similar. Which shell are you running in? When you know that, try man <your shell> and look for the section about the read built-in command. There are variations, but a common one allows:-
Code:
read var1?"Prompting text : "

... such that the message is displayed (trailing space to make it look nice when the user types) and put the response into variable var1



I hope that this helps.


Robin
Liverpool/Blackburn
UK
# 3  
Old 07-03-2013
its ksh, I just can't get it when i run it to prompt my questions for yes no and all, it runs through and displays all else, but just wont do that for me
# 4  
Old 07-04-2013
you need to fix some of the errors in the script ...
1. ((VERBOSE+=1)) does not work in bash or ksh in CentOS and Solaris

2. read -y and read -b -- did not find any reference to -y and -b options to read in CentOS and Solaris

3. see treatment of variable test using [-y] compared to "-y" ...
Code:
[otto@centosgeek ~]$ a="-y";if [ $a = [-y] ]
> then
>     echo $a
> else
>     echo boo
> fi
boo
[otto@centosgeek ~]$ a="-y";if [ $a = "-y" ]
> then 
>     echo $a
> else 
>     echo boo
> fi
-y
[otto@centosgeek ~]$

4. [ $answer =! [-y] ] -- string not equal sign should be != ...

5. improper use of read command ...
Code:
[otto@centosgeek ~]$ read -b "Please choose which bit version you want Java at? [32/64]" NUMBITS
bash: read: -b: invalid option
read: usage: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]
[otto@centosgeek ~]$

also, you need to make your coding style consistent so it is easier to debug ... indentations would also help you a lot ...
from ...
Code:
if [ $NUMBITS =! [-b] ]
then setServerJava -b $NUMBITS
fi

to ...
Code:
if [ $NUMBITS =! [-b] ]
then 
      setServerJava -b $NUMBITS
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to switch user in shell script?

HI in a server we can't login with root user directly but i can login with different user and then i can switch to root user by su command Requirement is there anyway where i can write a script without mentioning password in file as mentioning the root password is not the... (3 Replies)
Discussion started by: scriptor
3 Replies

2. UNIX for Dummies Questions & Answers

How to switch the user before executing a shell script from web page??

hi, i want to execute a shell script as a different user. the flow is like this. there is a html web page from which i have to call a shell script. web server is apache. to call the shell script from html page, a perl script is required. so the html page calls the perl script and the perl... (2 Replies)
Discussion started by: Little
2 Replies

3. UNIX for Dummies Questions & Answers

How to switch to a certain version of glibc?

Hello, I currently have glibc 2.13. I was wondering if you could teach me how to switch to : glibc2.3.2 I would really appreciate it if you tell me how to do it step by step since I am a beginner . thanks. (1 Reply)
Discussion started by: alireza6485
1 Replies

4. UNIX for Advanced & Expert Users

Help with server switch shell script

I need a script to change server automatically after performing some operations. The command for changing server is ssh username@servername . Then a prompt comes to enter a password. Then i need to perform some opertaions on the other server. How can i do this in a script? (1 Reply)
Discussion started by: pratikm23
1 Replies

5. Shell Programming and Scripting

How to switch user using shell script ?

Hi, script1.sh script2.sh script3.sh From above, script1.sh is the main script which is executed from root user, creates installation directory, changing ownership and execution rights etc..etc.. and finally calls scripot2.sh and script3.sh to create the database as well as for post... (1 Reply)
Discussion started by: milink
1 Replies

6. Shell Programming and Scripting

Switch user inside shell script

Hi, I am trying to create one script where I have to login as another user inside the script to exeute some commands How can i achieve this? Many thanks in advance. (4 Replies)
Discussion started by: prarat
4 Replies

7. UNIX for Advanced & Expert Users

Advanced Search * View * Edit JAVA version to WORK in GLASSFISH Forum topic JAVA version

Would like to confirm the ff. I got confused actually with the version I needed to download that will work on glassfish 3.0.1 a. Debian Squeeze (HP DL360). Need to use java version6 On Debian, I did apt-get install sun-java6-jdk. So when I check it's java version "1.6.0_22" Java(TM) SE... (1 Reply)
Discussion started by: lhareigh890
1 Replies

8. Shell Programming and Scripting

Switch User in within a Shell Script

Hi Experts, I'm trying to write a shell script to stop few things where i have to use another user to execute a command. Otherwise it will not work. Your help is really appreciated Thanks, (16 Replies)
Discussion started by: Afi_Linux
16 Replies

9. Shell Programming and Scripting

Switch from one database to other using shell script

Hi all, This is my first ever post to any forum so, dont let this go in vain...........:) Here is the scenario........ I have logged into the unix where oracle_sid is initialized for some X database in the .profile. I have a unix script where some sql query which fetches data from X... (3 Replies)
Discussion started by: sachinkl
3 Replies
Login or Register to Ask a Question