PDA

View Full Version : Shell Programming Question



Tek-E
January 26th, 2009, 06:46 AM
How....can I read input from a user character by character. Then place each character from the string the user entered into their own variables?

Any help with this will be greatly appreciated!

Thank you!

avikrc
January 26th, 2009, 08:15 AM
Hi,
you could use the cut utility to achieve this..

[assuming of course you already know how many characters the user will enter]

echo "Enter your name: "
read name
echo "You entered: $name"
a=`echo $name|cut -c 1-1`
b=`echo $name|cut -c 2-2`
c=`echo $name|cut -c 3-3`
d=`echo $name|cut -c 4-4`

echo "$a $b $c $d"
echo ""

geirha
January 26th, 2009, 11:38 AM
You can have the read command only read 1 character at a time with -n 1.


#!/bin/bash
read -n1 a
read -n1 b
echo $a $b


On the other hand, if you just want to process a string character by character, you can do:



#!/bin/bash
read word
for ((i=0; i < ${#word}; i++)); do
echo "index $i: ${word:i:1}";
done

Tek-E
January 26th, 2009, 05:59 PM
Thank you both Very much!