- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Newbie script help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2007 04:33 AM
07-11-2007 04:33 AM
I'm looking to put together a script that'll prompt you for actions to aid in a tape initialization process used for Veritas. I have the commands already, and there is only one variable (Tape Number) that should be prompted for, and used throught the script. Seems like this is entirely possible using case statements... Anyone have any ideas?
Here's a quick example of how I'd like the script to work.
Prompt for tape number.
Is the tape frozen? Y/N
If yes, execute xxxxxxxxxxxxxxxxxxxx
if no, execute yyyyyyyyyyyyyyyy
then,
would you like to expire the image on the tape? Y/N
if yes, execute ZZZZZZZZZZZZZZZZZZZZ
if no, execute vvvvvvvvvvvvvvvvvvvv
then,
So on and so forth. Any help would be greatly appreciated!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2007 04:57 AM
07-11-2007 04:57 AM
SolutionThere are several ways to approach this. Here's one:
#!/usr/bin/sh
typeset -l REPLY
while true
do
echo "\n Is the tape frozen? [y|n|q] \c"
read REPLY
case ${REPLY} in
"q" ) clear
exit 0
;;
"y" ) echo "OK, I can do that"
break
;;
"n" ) echo "No, I cannot do that!"
break
;;
* ) continue
;;
esac
done
echo "now continuing..."
The 'typeset -l' means that the REPLY variable is automatically converted to lowercase. This simplifies testing. The 'case' statement is executed "forever", that is unitl you supply a proper response. The 'break' statement stops the loop.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2007 04:59 AM
07-11-2007 04:59 AM
Re: Newbie script help
You can use the case construct. e.g.
Here is an example with the case statement.
#----------------------------------------
#!/bin/sh
# An example with the case statement
# Reads a command from the user and processes it
echo "Is the tape frozen? (y/n)"
read command
case "$command" in
y|Y)
echo "You entered yes..."
xxxxxxxxxxxxx;
;;
n|N)
echo "You entered no..."
yyyyyyyyyyyyy;
;;
*)
echo "Bad command, your choices are: y or n"
;;
esac
exit 0
#----------------------------------
You can repeat the above coding for each question accordingly.
hope this helps!
kind regards
yogeeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2007 05:24 AM
07-11-2007 05:24 AM
Re: Newbie script help
This would probably be overkill for a simple YES/NO question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2007 05:48 AM
07-11-2007 05:48 AM
Re: Newbie script help
if you have many y/n question, I suggest to use a shell function for this. Some stuff of the previous posters is included as well.
ask_m() returns 1, when an action is requested.
ask_m () { str="$1"
typeset -i ret=-1
typeset -l ask
while [ ret -lt 0 ]
do
print $str '(y/n)'
read ans
case "$ans" in
y) ret=1 ;;
n) ret=0 ;;
q) exit 0 ;;
*) print illegal input ;;
esac
done
return $ret ;}
if ask_m "what ever 1"
then print do_1
else print ignore_1
fi
if ask_m "something different to do"
then ...
fi
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2007 10:18 AM
07-11-2007 10:18 AM
Re: Newbie script help
Here's one more question, though. When the script fires off, I want it to prompt for a tape number, and assign that tape number to a variable I can insert at later times within the same script.. Any ideas there?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2007 10:34 AM
07-11-2007 10:34 AM
Re: Newbie script help
You could require that number to be on the script command line. If so, it would be $1.
And you would probably want to assign it to a more descriptive variable: TAPE=$1
And use $TAPE in your script.
But if you want to prompt, you can use something like you had before:
print -n "Which tape number? "
read TAPE
if [ -z "$TAPE" ]; then
: code to reprompt for name?? Or use while loop as above
fi
The print -n, doesn't add a newline to the end of the message. The -z check is for an empty string.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2007 05:41 AM
07-18-2007 05:41 AM
Re: Newbie script help
#------------------------------------------------
#!/bin/ksh
#Tape Initialization Script
#------------------------------------------------
#------------------------------------------------
#----IGNORE CASE for Y/N RESPONSE -----------
#------------------------------------------------
typeset -u unfreeze_request
typeset -u expire_request
typeset -u deassign_request
typeset -u relabel_request
typeset -u info_request
echo "This process will initialize a tape after removing all images. These steps should only be
performed when a tape is no longer readable as in a case where the header has been overwritten."
echo "Enter Tape Number"
read media_id
echo $media_id|wc -c|read valid
if [ "$valid" -ne 7 ]
then
echo "Tape Number Not Valid"
exit 1
fi
echo "Is the Tape Frozen?(Y/N)"
read unfreeze_request
if [ "$unfreeze_request" = Y ]
then
echo "Issuing Unfreeze Command"
#/usr/openv/netbackup/bin/admincmd/bpmedia \
# -unfreeze -ev "$media_id" -h server;
fi
echo "Would you like to expire the tape image?(Y/N)"
and so on... Thanks everyone for your help! This has been a great resource!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2007 05:44 AM
07-18-2007 05:44 AM
Re: Newbie script help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2007 09:13 AM
07-18-2007 09:13 AM
Re: Newbie script help
read media_id
echo $media_id|wc -c|read valid
if [ "$valid" -ne 7 ]
Here's a slight improvement on this check:
for media_id in AAA bbbbbbb; do
echo "Testing: $media_id"
if [[ $media_id = ??????? ]]; then
echo "Matches length"
else
echo "No match"
fi
done
(You would of course only use the "if [[ ]]", the for loop is to demo the test.)
This uses pattern matching to check the length.
You could also check for specific chars or numeric using "[0-9]".