Assigning multiple variables in ksh script

Is it possible to have a user input multiple words in one line and have the script assign each word a variable? I'm stuck please assist.

Example using "BILL JOHN SARA JILL" as what the user could type:

printf "Enter account names: "      BILL JOHN SARA JILL  
read input

Hi You could try the following,

IFS=' '
echo -e "Input Variables:-\c"
while read W X Y Z
do
echo ${W};echo ${X};echo ${Y};echo ${Z}
done

Let me know how you get on.

Regards

Dave

That worked! Thanks Dave :slight_smile:

Is there also a way to set a limit to the number of variables the user can enter?

Hi,

The script will append the fourth and any further space separated entries as a single variable, you could dispense with excess data simply by incorporating a simple line like.

IFS=' '
echo -e "Input Variables:-\c"
while read W X Y Z
Z=`echo ${Z} | awk '{ print $1 }'`
do
echo ${W};echo ${X};echo ${Y};echo ${Z}
done

Not elegant, but it should do the job.

Regards

Dave

Hi,
Another solution with array:

set -A TAB $(read && echo $REPLY)
i=0
echo "Nb arguments = ${#TAB[*]}"
while (( $i < ${#TAB[*]} ))
do
echo ${TAB[$i]}
let i="i + 1"
done

Regards.

Thanks again! One last question...I'm trying to understand the read command a little better. Is there an option where it reads only what the user inputs? Let me give an example of what i mean. Lets say the user inputs DOOR. I understand the read command will assign the value to the varable to "DOOR". But when I use that variable to search other files named "DOOR" it will return "DOOR" and "DOOR2ND". How can I make it to only return "DOOR"? Any insight will be greatly appreciated.

Hi,

Here the problem is some what changed and is more dependent on how you search the file, so it is difficult to take a stab at this without knowing that information.

Post what you have already tried.

Regards

Dave

Here is part of the script that is giving me issues...

printf "Enter Account name: "
read Answer

        #Print blanket cancel working directory and process id
        echo BLANKET CANCEL:
        echo $(date +'%c')
        ps -e -o pid,args | grep "phantom EORD.BLANKET.CANCEL" | grep -v grep | while read pid command
        do
                ln=`procwdx $pid`
                echo "$ln" | grep -q "${Answer}" && echo "$ln" && ps -ef | grep $pid | grep -v grep | sed -e 's/^[ \t]*//'
        done
	echo

Script results:

Enter Account name: NAIB

BLANKET CANCEL:
Wed Sep 24 08:26:59 EDT 2014
8585430:        /home/HKUNAIB/
root  8585430        1   0   Sep 04      -  0:04 phantom EORD.BLANKET.CANCEL
8978610:        /home/NAIB/
root  8978610        1   0   Sep 16      -  0:19 phantom EORD.BLANKET.CANCEL

In this script I dont want it to display "HKUNAIB". Please advise

You can use:

grep -w

---------- Post updated at 07:53 AM ---------- Previous update was at 07:38 AM ----------

There's stuff in your script which can be changed such as redundant grep's etc.

Just looking at the pertinent points though, I would imagine that you would want something like this in replace of your first two lines (the printf and read):

max=5; set --
while [[ ($# -eq 0) || ($# -gt $max) ]] ; do
        print "Please enter no more than $max account names: "
        read -r input; set -- ${input} 
        IFS='|'; RE="$*"
done

Then instead of:

grep -q "${Answer}"

You can use:

egrep -qw "${RE}"

Looks good now. Thank you all!!!! :slight_smile: