PDA

View Full Version : [SOLVED] Shell Scripting Shift Command



DGortze380
March 14th, 2008, 04:53 AM
Hey All,

Again, I've been searching for two hours and can't find an answer here or on google. I know how to use a shift for command line arguments, is there a way to use it for variables that are read in? Or is there a similar command?

EX:




#!/bin/ksh

while read name var0 var1 var2 var3 var4 ;do
for (( x=0;x<4;x++ )) ;do
sampleArray[x]=$var0
shift 1
done
done<fileName



The above obviously doesn't work or I wouldn't be asking. This is a simple example, but I need to read 50+ variables into an array :???:

Thanks

WW
March 14th, 2008, 05:05 AM
Can you read directly into the array? For example,


$ read a[0] a[1] a[2]
first second third
$ echo ${a[2]} ${a[1]} ${a[0]}
third second first
$

DGortze380
March 14th, 2008, 05:11 AM
Can you read directly into the array? For example,

Not sure how I'd do that, or if it would work. I'm reading from a text file, that has 15 to 20 lines and 50+ entries per line. I need an array for each line.

ghostdog74
March 14th, 2008, 05:14 AM
you can put into array


while IFS=" " read -a array;
do
echo ${array[0]};
# use a for loop to iterate the elements.
done < file

WW
March 14th, 2008, 05:17 AM
I don't know if ksh is different, but in bash, the read command has the -a option:


$ read -a myarray
zero one two three four five six seven eight nine ten eleven
$ echo ${myarray[3]}
three
$ echo ${myarray[11]}
eleven
$

nanotube
March 14th, 2008, 05:19 AM
you can read the line directly into an array, as long as items are separated by space. the following bash session should give you some clues:



$ myarr=( bla shmu gnu )
$ echo ${#myarr }
3
$ echo ${myarr[0]}
bla
$ echo ${myarr[1]}
shmu
$ echo ${myarr[2]}
gnu
$ myarr=( $(ls -1) )
$ echo ${#myarr }
9
$ ls -1
bin
Desktop
dev
documents
Examples
seamonkey-1.1.7.en-US.linux-i686.tar.gz
ssh
tmp
vmware
$ echo ${myarr[5]}
seamonkey-1.1.7.en-US.linux-i686.tar.gz



so, once you have a "line" in one variable, (say, $line), to make an array out of it, you can simply do

myarr=( $line )

DGortze380
March 14th, 2008, 05:56 AM
ok, I've pretty much got it working, I have a few other things to work out, I'll post back if I get stuck, otherwise I'll mark it solved.

DGortze380
April 9th, 2008, 01:18 AM
I know, old thread. but I finally got around to finishing this problem.
Ended up using a compound variable. Reading directly into and array worked in bash but not ksh, and left me with too many indirect variable references and control breaks. Unfortunately you do have to read it all in, but it ended up being shorter in the end.



#!/bin/ksh

itemCount=0
items=
(
typeset=" "
integer bal[52]
)

while read items[$itemCount].name items[$itemCount].bal[0] ... items[$itemCount].bal[51] ;do

echo " "
(( intemCount++ ))

done<filename



Thanks for the help!