LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-08-2012, 12:43 PM   #1
carlr
LQ Newbie
 
Registered: Mar 2012
Posts: 11

Rep: Reputation: Disabled
Why does cygwin and linux give different array outputs?


Hi,

I have written a script which works in cygwin (CYGWIN_NT-6.1-WOW64 1.7.11(0.260/5/3)) but i know want to run it on lunix (Linux 2.6.18-53.1.21.el5 #1 SMP Tue May 20 09:35:07 EDT 2008).

The script which works in cygwin does not work in linux. After some investigation I have found it to be due to array issues, so i wrote a simpel test script:

#!/bin/bash

PART_NAME=Wheel.inp

eval ARRAY_2D_ELSET=( $(awk -F, '{print "["$1"]=\""$0"\" "}' "$PART_NAME"_2D_ELSET.tmp) )

for ((i=1; i<=10; i++)) do
echo ${ARRAY_2D_ELSET[i]} >> ARRAYout.txt
done


The output files vary when run on linux and cygwin. This is causing my script not to function.

Linux output:
1]= 1, 183, 3, 4
[
2]= 2, 59, 357, 46
[
3]= 3, 91, 356, 170
[
4]= 4, 356, 39, 61
[
5]= 5, 94, 355, 163
[


Cygwin output:
1, 183, 3, 4
2, 59, 357, 46
3, 91, 356, 170
4, 356, 39, 61
5, 94, 355, 163
6, 355, 161, 206
7, 123, 354, 122
8, 354, 127, 144
9, 353, 129, 212
10, 104, 353, 24


Can any one explain to me why this script does not give the same output in both? Also, and more importantly, is there any way to make the linux output the same as the cygwin one? or do i have to go and make some significant edits to my origional cygwin script.


A sample of the file "$PART_NAME"_2D_ELSET.tmp is below:

1, 183, 3, 4
2, 59, 357, 46
3, 91, 356, 170
4, 356, 39, 61
5, 94, 355, 163
6, 355, 161, 206
7, 123, 354, 122
8, 354, 127, 144
9, 353, 129, 212
10, 104, 353, 24
 
Old 08-08-2012, 01:50 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I'm not a scripting expert, however I did take the script and data that you posted to see if I could recreate the problem you have described. The output generated under both Ubuntu 10.04 and Kubuntu 12.04 were the same as you described for cygwin.

Last edited by dwhitney67; 08-08-2012 at 01:52 PM.
 
Old 08-08-2012, 02:23 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Is it possible some or all of your input files have DOS style (\r\n) line endings? What bash version are you using on Cygwin and Linux?
 
Old 08-08-2012, 03:15 PM   #4
carlr
LQ Newbie
 
Registered: Mar 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Linux:
GNU bash, version 3.1.17(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

cygwin:
GNU bash, version 4.1.10(4)-release (i686-pc-cygwin)
Copyright (C) 2009 Free Software Foundation, Inc.

I've just checked and i have \n line endings on all files.
 
Old 08-08-2012, 04:38 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
I can reproduce the problem in Linux by putting a space at the beginning of each line of data.
Without the spaces I get the Cygwin output.
Code:
echo ' 1, 183, 3, 4
 2, 59, 357, 46
 3, 91, 356, 170
 4, 356, 39, 61
 5, 94, 355, 163
 6, 355, 161, 206
 7, 123, 354, 122
 8, 354, 127, 144
 9, 353, 129, 212
 10, 104, 353, 24' > file.tmp
Code:
eval ARRAY_2D_ELSET=( $(awk -F, '{print "["$1"]=\""$0"\" "}' file.tmp) )

for ((i=1; i<=10; i++)); do
  echo ${ARRAY_2D_ELSET[i]}
done

1]= 1, 183, 3, 4
[
2]= 2, 59, 357, 46
[
3]= 3, 91, 356, 170
[
4]= 4, 356, 39, 61
[
5]= 5, 94, 355, 163
[
 
Old 08-08-2012, 04:47 PM   #6
carlr
LQ Newbie
 
Registered: Mar 2012
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thank you for your help every one.

As Kenhelm says, it seems to be that cygwin can deal with a space in front to the first character, where as linux can not. i'm not sure why. It may just be the versions of bash i'm using.
 
Old 08-08-2012, 05:31 PM   #7
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
A bit off topic but any reason why you're loading into an array and then looping through to print rather than just redirecting the output from awk?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] mdadm: only give one device per ARRAY line: /dev/md/:raid and array laughing_man77 Linux - Hardware 4 03-23-2012 04:05 PM
converting a for loop to an array so zenity can give progress wonderfullyrich Programming 2 03-01-2012 08:58 AM
Array in awk outputs multiple values kristo5747 Linux - Newbie 1 11-09-2011 10:27 PM
how big can be an array to give segmentation fault!!! mlaich Programming 7 01-21-2006 07:58 AM
ODD! Cygwin - cat tr outputs email message from ealier in the day?!?!?!?! chingasman Linux - General 0 02-27-2003 05:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration