How to accept multiple input in a same variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to accept multiple input in a same variable
# 1  
Old 01-25-2010
Error How to accept multiple input in a same variable

Hi All,



I am trying to write a script in which I need to accept multiple value in the same variable.

The case Is that I have put a FOR loop and inside the FOR loop I am accepting a variable value. And I require all the values which gets inputted in the variable.


To be more clear,
Suppose:

for (( i=1 ; i<=5 ; i++ ))
do
read rahul
done

Now I want that for each value of "i" I want the corresponding value of "rahul"

Please suggest some method using which I can implement this.


Regards,
rahul
# 2  
Old 01-25-2010
1) code tags for code. [ code ] stuff [ /code ] without the extra spaces in the tags.

2) I'm not sure what you want that you're not getting already. You should already get the entire line in the rahul variable. What value are you getting in rahul, what value do you WANT in rahul, and what is the input?
# 3  
Old 01-25-2010
I am writing a script in which i want to take multiple PATHS values in a FOR loop.
The problem is that value for "rahul" will get reset to latest entered value on each "i" variable traverse.
what i want is i have value for "rahul" for each "i"

For example:

when i=1 , user enters rahul as "/opt"
i=2, user enters rahul as "/tmp"

so here the value of the variable "rahul" is getting reset. What i want is to save the value in some kind of 2D array so that all the value corresponding to i are saved. so that later i can use all the values of "rahul"
# 4  
Old 01-25-2010
Depends on your shell. Does your shell even support arrays?

Code:
#!/bin/bash

ARR=()

for ((N=0; N<5; N++))
do
        read ARR[$N]
done

echo ${ARR[0]}
echo ${ARR[1]}
echo ${ARR[2]}
echo ${ARR[3]}
echo ${ARR[4]}

# 5  
Old 01-25-2010
Hey thanks Man Smilie
U Solved My Problem!

---------- Post updated at 04:18 PM ---------- Previous update was at 03:49 PM ----------

Hey, i am stuck now with something else.
lemme show a part of my code
Code:
echo "Enter the Number of Languages"
read number
ORG=()
NEW=()
for ((N=1; N<=$number; N++))
do
echo -e "Enter The Source-Path For the Language $N"
read ORG[$N]
echo -e "Enter The Destination-Path For The Language $N"
read NEW[$N]
done
echo "Enter the number of prompts which u want to rename"
read pnumber
for ((i=1; i<=$pnumber; i++))
do
	clear
	echo -e "Original name for Prompt $i ::::   \c"
	read ORIGINAL
	echo -e "New name for Prompt $ORIGINAL   ::::   \c"
	read NEW
	for ((N=1; N<=$number; N++))
	do
		cp -p /$ORG[N]/$ORIGINAL /$NEW[N]/$NEW
	done
done
}


This is what i am implementing. Now the problem is when i am using

cp -p /$ORG[N]/$ORIGINAL /$NEW[N]/$NEW

i am not able to use the Array values in the FOR loop.

Please suggest the correct method of usage.....
# 6  
Old 01-25-2010
[CODE]cp -p /${ORG[$N]}/$ORIGINAL /${NEW[$N]}/$NEW[CODE]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to accept user input on the fly

I want a shell script that accepts user input simultaneously when performing other tasks. Example: A shell script should echo some messages on the console and when the user presses some keys it should respond to that action. say, when user presses the key A - more information should be printed... (2 Replies)
Discussion started by: Arun_Linux
2 Replies

2. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

3. Shell Programming and Scripting

accept input on an echo output

echo (some info) read? <&1(not working for me) (1 Reply)
Discussion started by: robin_simple
1 Replies

4. Programming

How to accept multiple lines input from User in C?

Hi I want to accept multiple lines input with spaces from User and i have a working code like this. char sRes; char sReq; printf("Please enter request:"); scanf("%",sReq); /* Accept the input from user */ printf("\nPlease enter response:"); scanf("%",sRes); but the... (4 Replies)
Discussion started by: AAKhan
4 Replies

5. UNIX for Advanced & Expert Users

Gedit semi-frozen/hangs; does not accept keyboard/mouse input

I have a very weird problem with the gedit 2.30.3 text editor on GNOME 2.30.2 (Ubuntu Linux 10.04 LTS): Any and all mouse clicks and key-presses into the gedit window are summarily ignored. When I say the gedit window, I mean the gedit window proper, the window contents, not the decoration of... (1 Reply)
Discussion started by: ropers
1 Replies

6. UNIX for Dummies Questions & Answers

accept user input?

how would i accept user input from the keyboard? (2 Replies)
Discussion started by: JamieMurry
2 Replies

7. UNIX for Dummies Questions & Answers

Command to accept input

I am trying to write a one line command for Oracle grid control. I am using a semi-colon to separate the commands. But, I am having a problem with a shell script accepting input. Wondering if you can help. Here is what I am trying to do: (PGPRD5432)@prd01:/> cd /export/home/postgres ##... (3 Replies)
Discussion started by: rexmabry
3 Replies

8. Shell Programming and Scripting

How to prompt for input & accept input in ONE line

hi, am a new learner to shell programming. i have a script which will prompt for user to key in their name & display their name afterwards. script ===== echo "Pls enter your name:" read name echo "Your name is $name." output ===== Pls enter your name: Bob Your name is Bob. what... (2 Replies)
Discussion started by: newbie168
2 Replies

9. Shell Programming and Scripting

Accept input parameters

Dear All, I got a table creation file in a standard format. I need to accept parameters from the user & then based on the input change the data in the file. For. eg. i will accept the database name, dbspace name & user name from the user and accordingly change the same in the table creation... (2 Replies)
Discussion started by: lloydnwo
2 Replies

10. Shell Programming and Scripting

Accept user input - only numbers

I have a situation where I want the user to enter only numbers in response to a READ command. I have some validation to restrict the number to be between 1 and 12, but if the user type in some characters the script echoes some error message and goes to the next command. Below is a snippet of the... (1 Reply)
Discussion started by: pvar
1 Replies
Login or Register to Ask a Question