read n number of inputs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read n number of inputs
# 8  
Old 07-27-2006
Code:
#!/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

# 9  
Old 07-27-2006
This is ksh:
Quote:
Originally Posted by Klashxx
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

# 10  
Old 07-27-2006
Klashxx,

every where { } are used is there any specific reason .. i tried the script with no {} i got error

line 1: var: command not found

can u explain me

--Chanakya
# 11  
Old 07-27-2006
I simulated your issue ,but i didn´t get the same result.
What shell are you using? Could you post what you tried?

Thx
# 12  
Old 07-27-2006
I think the reason for the error is that "{}" are mandatory for arrays but not for "normal" variables.
In assignments its OK:
Code:
VAR[${i}]=$var

But, here, you need the "{}":
Code:
# Right
echo ${VAR[${j}]}

# Wrong
echo $VAR[${j}]

Regards.
# 13  
Old 07-27-2006
Thanks Grial .. now i got it thanks a lot..

-Chanakya
# 14  
Old 07-27-2006
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
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