Operating System - HP-UX
1753830 Members
8947 Online
108806 Solutions
New Discussion юеВ

Problem on accepting a WORD in shell script

 
SOLVED
Go to solution
cybermilky
Occasional Contributor

Problem on accepting a WORD in shell script

Problem on accepting a word in shell script
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?
8 REPLIES 8
Ted Ellis_2
Honored Contributor
Solution

Re: Problem on accepting a WORD in shell script

use the wc command with echo:

`echo $word | wc -w` you will get the number of words submitted... just check it against 1 and if it is not, exit with your error

Ted
A. Clay Stephenson
Acclaimed Contributor

Re: Problem on accepting a WORD in shell script

While I would prefer to do this with regular expressions you can do it by attempting to read more than 1 var.

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'.
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Problem on accepting a WORD in shell script

If you used "ksh" instead, you could do-

read word
set -A wordlist $word
...
elif [ ${#wordlist[*]} > 1 ]

HTH

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Problem on accepting a WORD in shell script


elif [ `echo $word | wc -w` != "1" ]


live free or die
harry
Live Free or Die
Leif Halvarsson_2
Honored Contributor

Re: Problem on accepting a WORD in shell script

Hi

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

H.Merijn Brand (procura
Honored Contributor

Re: Problem on accepting a WORD in shell script

set word=`perl -le'while(print"Enter ONE word > "){$w=;$w=~/^\w+$/&&last}print$w`;
Enjoy, Have FUN! H.Merijn
Sridhar Bhaskarla
Honored Contributor

Re: Problem on accepting a WORD in shell script

Hi,

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
You may be disappointed if you fail, but you are doomed if you don't try
Sandro Schaer_1
Advisor

Re: Problem on accepting a WORD in shell script

different solutions on that one. here are two of mine. The first on really checks if more than one word was entered. The second one simply cuts out the first word only.




# 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