sh shell user input and stote it to a array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sh shell user input and stote it to a array
# 1  
Old 11-14-2007
Bug sh shell user input and stote it to a array

Dear Friends,

I am doing a sh shell script , But I dont have any idea how to read value from user Keyboard and store them to an array .. Is it possible or not I am not also sure in sh shell script ?
EX:-
#! /bin/sh

read DATA
echo "DATA[1] -" $DATA[1]
echo "DATA[3] -" $DATA[2]
echo "DATA[3] -" $DATA[3]


if my input line is - 300 200 100. Then it should display

DATA[1] -300
DATA[2] -200
DATA[3] -100

If it can be store in array then is $# will give me total no of array or anything else ?
or The only way to store is
read DATA[1] DATA[2] DATA[3] in sh script ?

I need a drop of Knowledge from you friends..

Thanks in advance.
user_prady

Last edited by user_prady; 11-14-2007 at 02:18 AM..
# 2  
Old 11-14-2007
something like this,

Code:
echo "300 200 100" | awk '{ split($0, arr, " "); for ( i in arr ) { print arr[i] } }'

# 3  
Old 11-14-2007
Quote:
Originally Posted by matrixmadhan
something like this,

Code:
echo "300 200 100" | awk '{ split($0, arr, " "); for ( i in arr ) { print arr[i] } }'

Oh oh..
But I think you did not get my problem..

Anyway Thanks for the reply

Like in csh shell script

#!/bin/csh -f

echo 'enter a line'
set userline = $<
echo $userline
set uline = ($userline)
echo $uline[1]
echo $uline[2]
echo $uline[3]
echo $uline[4]

If We enter 20 10 30 70
and I guess $# will give me total no index in that array..
Then it ll display
20
10
30
70
like this I want in sh shell script not in awk or nawk . I am not sure it is possible or not ?

Hope this time my explanation is better than before

Thanks again
user_pradyu
# 4  
Old 11-14-2007
Quote:
Originally Posted by user_prady
I am doing a sh shell script , But I dont have any idea how to read value from user Keyboard and store them to an array .. Is it possible or not I am not also sure in sh shell script ?
sh does not have support for arrays but ksh does.
# 5  
Old 11-14-2007
Quote:
Originally Posted by porter
sh does not have support for arrays but ksh does.
Thanks for your reply...
can I have a ksh code for the above problem ?
# 6  
Old 11-14-2007
OK, here is an example.

#!/bin/ksh

set -A parms $*
print "You supplied ${#parms[@]} parameters:"

echo 'Enter a line'
read x
echo 'You entered this line: ' $x
set -A rarray $x
print "You entered ${#rarray[@]} items"
print ${rarray[0]}
print ${rarray[1]}
print ${rarray[2]}
# 7  
Old 11-14-2007
Quote:
Originally Posted by fpmurphy
OK, here is an example.

#!/bin/ksh

set -A parms $*
print "You supplied ${#parms[@]} parameters:"

echo 'Enter a line'
read x
echo 'You entered this line: ' $x
set -A rarray $x
print "You entered ${#rarray[@]} items"
print ${rarray[0]}
print ${rarray[1]}
print ${rarray[2]}

Thaaaaaaaaaaak You ..............Love the people out here.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to embed data instead of reading user input from an array?

Hello, I am running under ubuntu1 14.04 and I have a script which is sending given process names to vanish so that I'd see less output when I run most popular tools like top etc in terminal window. In usual method it works. Whenever I restart the system, I have to enter the same data from... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Shell read command is not waiting for user input

Hi, i am working on one automation , for that i have writing one shell program that take user input in "while read line" block. but read command is taking value that is readed by While block. while read line; do command 1; command 2 echo -n "Do you want to continute > " read rsp... (2 Replies)
Discussion started by: ranvijaidba
2 Replies

3. Shell Programming and Scripting

User input for shell

I still cannot figure out how to get this read command to work. I want the script to ask questions when prompted, and the user to enter a response. Based on response it will continue or exit. I have not worked with this type of script before so I am almost clueless on what to do, and so far google... (6 Replies)
Discussion started by: bigbenn
6 Replies

4. Shell Programming and Scripting

create an array which can store the strings from the user input in shell script

I want to create an array which can store the strings from the user input in shell script . example :- I want to store the 5 fruits name in a single array which the user provides . (1 Reply)
Discussion started by: Pkast
1 Replies

5. Shell Programming and Scripting

every time user input create array perl

Hi, How to create array every time user input and store user input and display all array print " Enter input " my @input = split(' ', $input) chmop($input = <STDIN>; foreach ($input) { @array= @input; } print @array"\n"; (1 Reply)
Discussion started by: guidely
1 Replies

6. Shell Programming and Scripting

Assign user input to already declared array

What I am doing is creating a top menu, which a user will select a choice with a number entry. That number corresponds to a string in an array. I then want to assign that response to another array I've already declared. For example: #!/bin/bash colors=(red blue yellow) red=(cherry fire)... (2 Replies)
Discussion started by: Akilleez
2 Replies

7. Shell Programming and Scripting

User Input Shell Script

Hello I am trying to create a user input shell scipt. The objective is user should enter the circuit number and the input is saved in a log file. If the user does not enter anything then the question should prompt it until the circuit no. is entered. Can any one please correct the code below.... (3 Replies)
Discussion started by: sureshcisco
3 Replies

8. Programming

How to pass C array as input to Shell script

Hi, In the below C code , i want to pass the array to a unix shel script. my script should called as ex myscript 1,2,3 #include <stdio.h> int main() { int a={1,2,3}; } Thanks, Arun (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

9. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies

10. Shell Programming and Scripting

Accepting user input in c shell

i need to accept the user input in my c shell script before executing next command. i have the following code which ask for user input, but does not store this value. set req echo " Enter your input(Y/N)?" read req if (req = Y) echo " print $req" else echo " print $req" ... (3 Replies)
Discussion started by: skumar11
3 Replies
Login or Register to Ask a Question