![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Reading a file using sh with spaces in filename | jekl5 | Shell Programming and Scripting | 4 | 02-29-2008 10:18 AM |
| How to print arguments along with spaces using awk | jisha | Shell Programming and Scripting | 8 | 01-17-2008 01:43 AM |
| Reading a line including spaces | aksarben | UNIX for Dummies Questions & Answers | 1 | 09-19-2007 08:39 PM |
| Bash: Reading 2 arguments from a command line | Vozx | Shell Programming and Scripting | 0 | 12-08-2005 05:23 PM |
| Reading runtime arguments from a file | Sabari Nath S | UNIX for Dummies Questions & Answers | 4 | 08-24-2005 10:38 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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" |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|