- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Problem on accepting a WORD in shell script
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
Discussions
Discussions
Forums
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
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
тАО10-17-2002 07:57 AM
тАО10-17-2002 07:57 AM
I'm suppose to allow my script to accept only ONE word but not more than one word. I don't want to use the usage of 'scriptfile [argument]'. I want my shell script to use the 'read' command. I've done something like this:
---------------
#!/bin/sh
echo "Enter a word: \c"
read word
if [ -z "$word" ]
then
echo "nothing is entered"
exit 1
elif [ ??? ] #how to do the checking here?
then
echo "cannot enter more than 1 word"
exit 1
fi
echo "You entered: $word"
---------------
How do I write the proper elif statement that to ensure my word is not more than 2 word?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 08:05 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 08:08 AM
тАО10-17-2002 08:08 AM
Re: Problem on accepting a WORD in shell script
echo "Enter a word: \c"
read word word2 word3
In this case, if more than one word is entered word2 will be a non-null value. If more that two words are entered word3 will be a non-null value. If three or more 'words' are entered ${word3} will contain everything past the first two 'words'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 08:09 AM
тАО10-17-2002 08:09 AM
Re: Problem on accepting a WORD in shell script
read word
set -A wordlist $word
...
elif [ ${#wordlist[*]} > 1 ]
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 08:10 AM
тАО10-17-2002 08:10 AM
Re: Problem on accepting a WORD in shell script
elif [ `echo $word | wc -w` != "1" ]
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 11:09 AM
тАО10-17-2002 11:09 AM
Re: Problem on accepting a WORD in shell script
Yet another version.
cnt=0
read a
for word in $a
do
cnt=$(($cnt + 1))
done
case $cnt in
0)
echo You must enter one word !
;;
1)
echo You entered $a
;;
*)
echo You entered $cnt words
;;
esac
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 11:23 AM
тАО10-17-2002 11:23 AM
Re: Problem on accepting a WORD in shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 12:27 PM
тАО10-17-2002 12:27 PM
Re: Problem on accepting a WORD in shell script
Another way.
You can use $# parameter to look at the number of arguments.
..
elif [ $# -gt 2 ]
then
echo "Cannot enter more than 2 words"
exit 1
..
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-17-2002 10:49 PM
тАО10-17-2002 10:49 PM
Re: Problem on accepting a WORD in shell script
# first clear the screen
clear
# now ask for a word
echo "enter one word \c"
read w
# check if a word was entered
if test "$w" = ""
then echo "nothing entered"
# count the words in the variable
else if test `echo $w|wc -w` -gt 1
then echo "more than one word2"
else echo "one word entered"
fi
fi
# **************************************
# another approach :
clear
echo "enter a word \c"
read w
# simply cut out the first word only and discard the rest
w2=`echo "$w"|cut -d' ' -f1`
if test "$w2" = ""
then echo "nothing entered"
else echo "here's your word : $w2"
fi
have fun
Sandro