User input for shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting User input for shell
# 1  
Old 07-05-2013
User input for shell

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.

Code:
 
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

# 2  
Old 07-05-2013
What shell do you use?
# 3  
Old 07-05-2013
I am using ksh, sorry forgot to mention
# 4  
Old 07-05-2013
Not sure if this is what you want so......

From the command line:-

Code:
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:-
Code:
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~> _


Last edited by wisecracker; 07-05-2013 at 04:34 PM..
# 5  
Old 07-05-2013
Quote:
Originally Posted by bigbenn
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. . . .
What about reading the man ksh page?
Quote:
read [ -ACprsv ] [ -d delim] [ -n n] [ [ -N n] [ [ -t timeout] [ -u unit] [ vname?prompt ] [ vname ... ]
. . .
If the -v is specified, then the value of the first vname will be used as a default value when reading from a terminal device.
. . .
If the first argument contains a ?, the remainder of this word is used as a prompt on standard error when the shell is interactive.
The -v option might not be what you want it to be...
# 6  
Old 07-05-2013
Quote:
Originally Posted by bigbenn
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.

Code:
 
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

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...
Code:
#!/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


Last edited by Don Cragun; 07-05-2013 at 06:54 PM.. Reason: Update getopts notes...
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 07-09-2013
had to change a little

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


Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Recording user input from interactive shell script

Hello, I want to start out by saying that I am fairly new to scripting and am looking for someone that can point me in the right direction. Basically what I need is a way to run a interactive script that will prompt users with questions weather that be yes/no or a specific answer.. I want to be... (3 Replies)
Discussion started by: shoutcast
3 Replies

2. Shell Programming and Scripting

Shell read command is not waiting for user input

Hi, i am working on one automation , for that i have writing one shell program that take user input in "while read line" block. but read command is taking value that is readed by While block. while read line; do command 1; command 2 echo -n "Do you want to continute > " read rsp... (2 Replies)
Discussion started by: ranvijaidba
2 Replies

3. Shell Programming and Scripting

Shell script to accept user input on the fly

I want a shell script that accepts user input simultaneously when performing other tasks. Example: A shell script should echo some messages on the console and when the user presses some keys it should respond to that action. say, when user presses the key A - more information should be printed... (2 Replies)
Discussion started by: Arun_Linux
2 Replies

4. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

5. Shell Programming and Scripting

create an array which can store the strings from the user input in shell script

I want to create an array which can store the strings from the user input in shell script . example :- I want to store the 5 fruits name in a single array which the user provides . (1 Reply)
Discussion started by: Pkast
1 Replies

6. Shell Programming and Scripting

User Input Shell Script

Hello I am trying to create a user input shell scipt. The objective is user should enter the circuit number and the input is saved in a log file. If the user does not enter anything then the question should prompt it until the circuit no. is entered. Can any one please correct the code below.... (3 Replies)
Discussion started by: sureshcisco
3 Replies

7. Shell Programming and Scripting

SQL PLUS Command 'ACCEPT' is not waiting for user input with sh shell script

Dear All, The sqlplus 'Accept' command is not waiting for user input when I include the command within a shell script. Note: The 'Accept' command is working fine if I execute it in a SQLPLUS Prompt. Please fins the below sample script which i tried. SCRIPT: -------- #!... (4 Replies)
Discussion started by: little_wonder
4 Replies

8. Shell Programming and Scripting

sh shell user input and stote it to a array

Dear Friends, I am doing a sh shell script , But I dont have any idea how to read value from user Keyboard and store them to an array .. Is it possible or not I am not also sure in sh shell script ? EX:- #! /bin/sh read DATA echo "DATA -" $DATA echo "DATA -" $DATA echo "DATA... (7 Replies)
Discussion started by: user_prady
7 Replies

9. Shell Programming and Scripting

Accepting user input in c shell

i need to accept the user input in my c shell script before executing next command. i have the following code which ask for user input, but does not store this value. set req echo " Enter your input(Y/N)?" read req if (req = Y) echo " print $req" else echo " print $req" ... (3 Replies)
Discussion started by: skumar11
3 Replies

10. Shell Programming and Scripting

Accepting user input in Bourne shell and using sed

He guys. Basically I want to make a script that can add, delete and view stuff in a external file called config.txt. I can open it up in Joe but im not sure how to read in the user input or using commands automatically in joe to edit, save then quit. Problem area below: 1) echo "Add... (1 Reply)
Discussion started by: Pits
1 Replies
Login or Register to Ask a Question