read n number of inputs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read n number of inputs
# 1  
Old 07-25-2006
read n number of inputs

Hello,

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...

Thakns In advance
Aparna
# 2  
Old 07-25-2006
Code:
man 1 getopt

# 3  
Old 07-25-2006
Code:
i=1
while true
do
   read var
   [ "${var}." = "." ] && break; #NULL VAR->STOP INPUT
   VAR[${i}]=$var
   (( i += 1 ))
done

# to show

j=1
while [ ${j} -lt ${i} ]
do
   echo ${VAR[${j}]}
   (( j += 1 ))
done

# 4  
Old 07-25-2006
What about this?
Code:
#!/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

Regards.
# 5  
Old 07-25-2006
getopt is useful when there is a need to parse command line arguments and in this case it would not work.
# 6  
Old 07-26-2006
thanks

Thanks a lot for the solution . However, can there be some possibility , that by using ksh !!!

I dont want to use bash shell....

Thanks a lot for help!!
# 7  
Old 07-27-2006
Try this:
Code:
#!/usr/bin/ksh

echo "Enter the inputs for the file: \b"
read input
echo $input |tr ' *' '\n' > input_file

You just read one variable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Noob question: How to check the total number of inputs entered by user?

Say I have this line: read -p "Enter 3 numbers: " num1 num2 num3; I want to write a while loop that repeatedly asks for input if the number of inputs entered is not equal to 3. I don't know the correct command to find the number of inputs entered. Help, please? (4 Replies)
Discussion started by: jejemonx
4 Replies

2. Shell Programming and Scripting

Print Unknown Number of User Inputs in awk

Hello, I am new to awk and I am trying to figure out how to print an output based on user input. For example: ubuntu:~/scripts$ steps="step1, step2, step3" ubuntu:~/scripts$ echo $steps step1, step2, step3 I am playing around and I got this pattern that I want: ... (3 Replies)
Discussion started by: tattoostreet
3 Replies

3. Shell Programming and Scripting

Read variable inputs

I am trying to make an interactive script. Only problem my inputs are not read and correctly channeled. Please help: Here is my code #!/bin/sh PATHSCRIPT=/home/pp/tmp #if ; then # echo "Syntax : $0 input off lat sample" # exit 1 # fi echo "Choice of Graph" echo "1 -- Type... (5 Replies)
Discussion started by: newkid.7955
5 Replies

4. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

5. Programming

Some error with number of keyboard inputs occured with this code for reversing a string..

i used a two-way linked list "node" for the code:: #include<stdio.h> #include<malloc.h> void insert(); void reverse(); struct node { char c; struct node *next; struct node *back; }*start=NULL; int main() { int n,i; (4 Replies)
Discussion started by: mscoder
4 Replies

6. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

7. UNIX for Dummies Questions & Answers

read a number

- A=90-100 - B=80-89 - C=70-79 - D=60-69 - F=0-59. echo -n "What test score did you get? (0-100)" read score1 echo “you got a $score1” if . then echo “you got a F” else echo --------------------- ) echo you got... (2 Replies)
Discussion started by: JudoMan
2 Replies

8. Programming

Using ioctl to read mice inputs

I was wondering if its possible to read mouse inputs using ioctl functions somehow ? If it is not too much of trouble can anyone write or even direct me to sample code of ioctl reading someother HID. (2 Replies)
Discussion started by: maverick_
2 Replies

9. Shell Programming and Scripting

How to read inputs from a file

Hello; Please I need to read inputs from a file change 1 or 2 things the output to another file. (1 Reply)
Discussion started by: jimoney
1 Replies

10. Shell Programming and Scripting

read several inputs and if none input set to 9999

need a script that goes something like this #!/usr/bin/bash echo "input up to TEN values, separated by spaces" read vari1 vari2 vari3 vari4 vari5 vari6 vari7 vari8 vari9 vari10 #set null variables to 9999 (somehow?) #now echo all variables echo $vari1 $vari2 $vari3 $vari4 $vari5 $vari6... (1 Reply)
Discussion started by: ajp7701
1 Replies
Login or Register to Ask a Question