reading in arguments with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading in arguments with spaces
# 1  
Old 04-22-2009
reading in arguments with spaces

I want to be able to read in input which contain spaces and put that into an array. Each field should be delimeted by a space and should be a different array element. However I cant get it to work. any tips?

Code:
read input
//type field1 field2 field3
echo "$input"
array="$input"

Thanks in advance

Calypso
# 2  
Old 04-22-2009
Code:
read input
set -f
IFS=' '
array=( $input )

# 3  
Old 04-23-2009
Thanks very much for that, the only problem is that if a filed is a metacharacter for example "*" it doesnt seem to work. Ive tried looking on the net but I cant find any similiar examples, any other ideas?


Calypso
# 4  
Old 04-23-2009
Try something like this :-

Code:
#!/bin/ksh

input="$@"

set -A array $input

print "Array element 0 = ${array}"
print "Array element 1 = ${array[1]"
print "Array element 4 = ${array[4]"

Call with :

Code:
# ./script zero one two three four

# 5  
Old 04-23-2009
Hi lavascript,

sorry its not script arguments im trying to split up, im reading user input in a while loop e.g

while true
do read input
#split input here into array elements
done

also I am using bash shell and set -A gives me an error

"line 84: set: -A invalid options"
# 6  
Old 04-23-2009
ok apologies set -A is for ksh.

Are you doing a while loop for another task or is it purely for this function?

Assuming you want to do other things in your while loop you can try the below code. Otherwise you don't need the while. Saying that you could use while instead of for but below would need changes.

Code:

array=""
array_count=0

for element in $input
do
     ... do some checking....
     array[$array_count]=$element
     ... do other stuff .....
     array_count=$(( $array_count + 1 ))
done

# 7  
Old 04-23-2009
Yes I am using the while loop for other purpases

Im afraid its still not working, if i type in "5 * 5" and then print out all array elements i get

element 0 - "5"
element 1 - "file 1 in directory"
element 2 - "file 2 in directory"
element n-1 - "file n in directory"
element n - "5"

Calypso
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

2. Shell Programming and Scripting

Reading in data that has spaces in it.

I have a csv file called template.csv which has the following data Name, Age, Height Jessica Jesse, 18, 150 Now what I want to do is use a shell script to read the name age and height which looks like this: #!bin/sh INPUT='template.csv while read Name Age Height do echo... (2 Replies)
Discussion started by: JSNY
2 Replies

3. Programming

Reading structured arguments

I am passing an argument to a C++ program which is going to look like I need to get the integers into arrays a, b, c, d with a= 12,12,34,2,12 b= 34,4,2,1,23 c= 5,5,4,4,13 d= 6,6,6,6,5 (5 Replies)
Discussion started by: kristinu
5 Replies

4. Shell Programming and Scripting

Reading arguments for a shell script from file

I have a shell script that takes 2 arguments. I will have to execute this script multiple times with different values for the arguments. for example, ./shscript env1 value1 ./shscript env1 value2 ./shscript env2 value3 ./shscript env3 value4 ./shscript env1 value5 ./shscript env3... (24 Replies)
Discussion started by: goddevil
24 Replies

5. Programming

Reading command line arguments and setting up values if option not provided

I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value. I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very... (2 Replies)
Discussion started by: kristinu
2 Replies

6. Shell Programming and Scripting

Reading arguments but exclusing some

I have a tcsh script which I pass arguments to. I want to store all the arguments except for the options. Currently I am using the line below, however I want to also exclude and variation of them (e.g. whether any letter is uppercase or lower case) Is there a neat way to do it? set... (0 Replies)
Discussion started by: kristinu
0 Replies

7. Shell Programming and Scripting

Reading complete line after spaces

Hi, I am reading data from a variable which has spaces in it. I want to get the data after first space, i.e. if my data line is "My Name is Ashish...", I want the data returned as "Name is Ashish". I am using #!/bin/sh shell. Please help me with the code to read the complete data after first... (8 Replies)
Discussion started by: gupt_ash
8 Replies

8. Shell Programming and Scripting

How to print arguments along with spaces using awk

Hi All, file_1.txt contains aaa bbbb hhhh vvvvv mmmmm iiiii What i want is to search for the first coloumn of each line using awk.i.e as below: awk '/aaa/ {printf(<>)}' file_1.txt The print part (<>) should contain all the values(or coloumns ) in the particular line(here it... (8 Replies)
Discussion started by: jisha
8 Replies

9. Shell Programming and Scripting

Bash: Reading 2 arguments from a command line

If no arguments are entered I wanna be able to read 2 arguments, i have done like this but it doesnt work: x=0 until #loop starts do if ; then echo No arguments were entered, please enter 2 arguments. read $1 $2 elif || ; then echo $#... (0 Replies)
Discussion started by: Vozx
0 Replies

10. UNIX for Dummies Questions & Answers

Reading runtime arguments from a file

Hi All, I am running a script which wud take a long time to complete execution ... In between, it asks for yes/no sort of confirmation from user for doing some acivities .... Now that I dont want to wait till the script executes upto this point where it needs user confirmation, is there any way... (4 Replies)
Discussion started by: Sabari Nath S
4 Replies
Login or Register to Ask a Question