Problem with arrays on cygwin.

tranicus
tranicus used Ask the Experts™
on
Hello all,

this is an attempt to setup an arrays in cygwin on Windows shell scripts:

set -a field1 `cat dir_dir.txt | awk '{print $3}'`

i=0
for field2 in  `cat dir_s.txt | awk '{print $4}'`
  do
    if [ "$field2" != bytes ]
     then
       echo "${field1[$i]} $field2" >> join_file.txt
    else
       i=i+1
     fi
done

I kept getting an error:

for field2 in  `cat dir_s.txt | awk '{print $4}'`
  do
    if [ "$field2" != bytes ]
     then
       echo "${field1[$i]} $field2" >> join_file.txt
test.sh: 35: Syntax error: Bad substitution
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Have you tried an alternate syntax for creating the array? Try this:

field1=( `cat dir_dir.txt | awk '{print $3}'` )

If you try that and it still doesn't work, I would make sure that the command:

cat dir_dir.txt | awk '{print $3}'

is actually returning output.

Author

Commented:
the log indicated "cat dir_dir.txt | awk '{print $3}'" it does return data.  it seem the array having problem of storing data.

set -a field1 `cat dir_dir.txt | awk '{print $3}'`
echo ${field[*]} > join_file.txt

Commented:
Did you try the alternate syntax that I showed you?

Also, in that example you just barely posted, you have a typo. Try one of the following:

field1=( `cat dir_dir.txt | awk '{print $3}'` )
echo ${field1[*]}

and

set -a field1 `cat dir_dir.txt | awk '{print $3}'`
echo ${field1[*]}

Do you get output in either case?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
This work:

set field1=`cat dir_dir.txt | awk '{print $3}'`
echo $field1

but once I placed arrays to it.

field1=`cat dir_dir.txt | awk '{print $3}'`    ###minor modification to work in my env.
echo ${field1[*]}

error: echo ${field1[*]}
test.sh: 14: Syntax error: Bad substitution

same error for the next statement.

set -a field1 `cat dir_dir.txt | awk '{print $3}'`
echo ${field1[*]}


I am beginning to think this version of Cygwin cannot do arrays.  The syntax are basic.

Commented:
I just tried these exact commands in Cygwin and it worked fine:

value=( `ls` )
echo ${value[*]}

Copy and paste these exact commands and let me know what you see.

When you say "###minor modification to work in my env", what do you mean? You need to include the parenteses and spaces in order for this to work.

Doing only...

value=`ls`

...will not create an array, just a parameter with a single value.

Author

Commented:
nope, it didn't work.  I am going to download a new version of cygwin and try it with that.

value=( `ls` )
echo ${value[*]}

Author

Commented:
I just downloaded the latest cygwin.  Yup, its the old version I had that did not support arrays.   Tested your command and it work:

value=( `ls` )
echo ${value[*]}.

thank you for your help.

Commented:
Great! Glad it's working.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial