The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2009
Calypso Calypso is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 29
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 (permalink)  
Old 04-22-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365

Code:
read input
set -f
IFS=' '
array=( $input )

  #3 (permalink)  
Old 04-23-2009
Calypso Calypso is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 29
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 (permalink)  
Old 04-23-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
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 (permalink)  
Old 04-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365
Quote:
Originally Posted by Calypso View Post
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?

You must quote your input if it contains wildcard characters.

(And please post the code you are currently using.)

Last edited by cfajohnson; 04-23-2009 at 01:34 PM..
  #6 (permalink)  
Old 04-23-2009
Calypso Calypso is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 29
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"
  #7 (permalink)  
Old 04-23-2009
lavascript lavascript is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 47
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

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0