I still cannot figure out how to get this read command to work. I want the script to ask questions when prompted, and the user to enter a response. Based on response it will continue or exit. I have not worked with this type of script before so I am almost clueless on what to do, and so far google has not proved to be a valid friend on this endeavor.
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
read -v "Did you stop all instances before this? [y/n]" answer
if [ $answer != [-y] ]
then
echo -h "Cannot proceed because instances not stopped"
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 -v "Is this version of Java the current version you want? [y/n]" answer
if [ $answer = [-y] ]
then
exit
else
read -v "Please choose which bit version you want Java at? [32/64]" NUMBITS
if [ $NUMBITS = [-b] ]
then
setServerJava -b $NUMBITS
fi
Last login: Fri Jul 5 20:14:24 on ttys000
AMIGA:barrywalker~> ksh
AMIGA:uw> printf "Type in your name: "; read name
Type in your name: Barry
AMIGA:uw> echo "$name"
Barry
AMIGA:uw> exit
AMIGA:barrywalker~> _
Alternatively:-
Last login: Fri Jul 5 20:31:50 on ttys000
AMIGA:barrywalker~> ksh
AMIGA:uw> echo -n "Type in your name: "; read name
Type in your name: Barry
AMIGA:uw> echo "$name"
Barry
AMIGA:uw> exit
AMIGA:barrywalker~> _
Code marked in red above is not valid for ksh (some of it isn't valid for any shell I've seen) or is using variables that have not been defined. Code marked in orange above doesn't work that way on my system; and you didn't say what system you're using. (The code I have below using the what utility should work on any Linux or UNIX system I've seen.) You look for dependencies on a utility that isn't used??? You don't look for dependencies on java and setServerJava which are used in your script. (Note that my system doesn't have setServerJava, so I had to comment out that code while I tested the following script.)
The way you were printing diagnostics for unknown options doesn't match standard getopts behavior (and doesn't work on my system). The code below seems to do what you want in a portable manner. (On some systems, what you were trying would work if you used $optopt instead of $opt . And, if you want to print a custom diagnostic, you should be able to use $OPTARG instead of $opt to get the unknown option letter.)
You have a -y option, but you don't have any code to implement it. (And if you do what it implies, you will never change the java in use because it will exit when you say y in response to the question "Is this version of Java the current version you want? [y/n]". (The code below prints a diagnostic and exits if -y is given on the command line.)
The way you test NUMBITS requires a -b option to be specified on the command line and then requires that the value be entered again later???
The following script seems to do what I was guessing you were trying to do. Hopefully it will give you some useful ideas on how to do what you really want to do...
#!/bin/ksh
SCRIPT=${0##*/}
VERSION=0.2
function usage {
cat << EOM
usage: $SCRIPT [-b bits] [-h] [-v] [-V] [-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: $VERSION"
exit 0 ;;
(y) YES=1
echo "$SCRIPT: -y option not yet implemented."
exit 1 ;;
(*) usage ;;
esac
done
shift $(($OPTIND - 1))
printf "NUMBITS=%s, VERBOSE=%d, YES=%d\n" "$NUMBITS" "$VERBOSE" "$YES"
# Dependencies:
for DEPENDENCIES in java setServerJava # symlink-resolve
do
if ! which $DEPENDENCIES >/dev/null 2>&1
then
echo "Aborting - unable to resolve dependency: $DEPENDENCIES"
exit 1
fi
done
unset DEPENDENCIES
read answer?"Did you stop all instances before this? [y/n] "
if [ "$answer" != y ]
then
echo "Cannot proceed because instances not stopped"
exit 1
fi
echo "Current Java Version running on machine is:"
java -version
# Is current version what we want?
# If yes say so and exit
read answer?"Is this version of Java the current version you want? [y/n] "
if [ "$answer" = y ]
then
exit
fi
if [ -z "$NUMBITS" ]
then
read NUMBITS?"Please choose which bit version you want Java at? [32/64] "
fi
if [ "$NUMBITS" != 32 ] && [ "$NUMBITS" != 64 ]
then
echo "$SCRIPT: bit version must be 32 or 64; not $NUMBITS."
usage
fi
echo "Setting bit version to $NUMBITS."
setServerJava -b $NUMBITS
Reviewed with my superior, said I had to many prompts, he only wants the two. It needs to ask for instances(DONE) then ask if you want to set the java bit to current version(DONE
command line to run should be like
setServerDefaultJava -b32
or
setServerDefaultJava -b64
I need to add the setServerJava or something to functions, because it isn't switching between the 2 versions of Java, I am very confused on this. It will be run in a shell script, but in a bash environment
SCRIPT=$(basename "$0")
VERSION='$Revision: 6327 $'
VERBOSE=0
NUMBITS=0
YES=0
function usage {
cat << EOM
usage: $SCRIPT [-h] [-v] [-V] [-b bits] [-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))
printf "NUMBITS=%s, VERBOSE=%d, YES=%d\n" "$NUMBITS" "$VERBOSE"
# 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=${SCRDIR}/app/share
JAVASHARE=${SHARE}/java/bin/java
# if YES is 0, prompt user asking if instances are stopped
# if answer is no exit
if [ -L $JAVESHARE ]
then
read ANSWER?"Did you stop all instances before this? [yes/no] "
if [[ $ANSWER != @([yes])* ]]
then
echo "Cannot proceed because instances not stopped." & exit 1
fi
fi
# Is current version what we want?
# If yes say so and exit
read ANSWER?"Is this version of Java the current version you want? [yes/no]"
if [[ $ANSWER = @([yes])* ]]
then
echo "Setting Java to: $NUMBITS"
usage
fi
setServerJava -b $NUMBITS
if [ $VERBOSE -ne 0 ]
then
echo "NUMBITS: $NUMBITS"
fi
JAVACUR=$(symlink-resolve $JAVASHARE)
# test and make sure it is directory
if [ $JAVACUR = $JAVASHARE ]
then
exit 1
fi
# extract the bits from the current
$(basename (symlink-resolve /app/share/java | sep -e "/.*([36][42])bit$//"))
# OLDBITS
JAVAP=$(basename $JAVACUR)
JAVAN=$(echo "$JAVAP" | sed -e "s/$OLDBITS/$NUMBITS/")
JAVANEW=${SHARE}/java
# test is dir JAVANEW error exit if not
if [ $JAVAP = $JAVANEW ]
then exit 1
else
echo "Symlink not found: $JAVAN "
fi