I think its a sinple query but somehow i m stucked up here...
I am trying to enter n number of inputs from the user and write them in
an input file ie row wise...
I tried standard commands like
$echo "enter the inputs for the file"
$read var1 var2 var3 var4
test1 test2 test3 test4
$echo "$var1\n$var2\n$var3\n$var4" > input_file
$more input_file
test1
test2
test3
test4
However, the problem lies that i dont know how many inputs the user wants to enter
like
here we have 4 inputs but it may be 5,6 .... and so on...
so if we write till var4 it will not take var5 and var6....
Pls suggest where i am wrong or the possible solution...
#!/bin/bash
read var
declare -a vars
vars=($var)
echo Number of elements: ${#vars[*]}
echo Elements:
j=0
while [ ${j} -lt ${#vars[*]} ]
do
echo ${vars[${j}]}
(( j += 1 ))
done
#!/usr/bin/ksh
read var
set -A vars $(echo $var)
echo Number of elements: ${#vars[*]}
echo Elements:
j=0
while [ ${j} -lt ${#vars[*]} ]
do
echo ${vars[${j}]}
(( j += 1 ))
done
There is another method but in this method we need to answer y or n if we want to give more output.
echo "enter the inputs for the file"
answer=y
while [ "$answer" = "y" ]
do
read fname
echo "$fname">>filename
echo "Enter anymore y or n\c"
read anymore
case $anymore in
y*|Y*) answer=y ;;
n*|N*) answer=n ;;
*)answer=y ;;
esac;done