shell - bash - script - extract words from a string and put it in an array

echo "Enter a string "
read s
declare -a arr
l=` echo $s| wc -c `
s="$s "
i=1
j=0
s3=""
S2=""
    while [ $i -le $l ]
    do
    s2=`echo $s |cut -c $i `
   
        if [ ! $s2 = " " ]
        then
        s3=$s3$s2
        else
        arr[j]=$s3
        s3=""
        j=` expr $j \+ 1 `
        fi

    i=` expr $i \+ 1 `
    done
echo "The words are:"
len=${#arr[@]}
u=0
while [ $u -lt $len ]
do
 echo "${arr[u]}"
u=` expr $u \+ 1 `
done
Post a Comment