The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Simple loop in Bash BrewDudeBob Shell Programming and Scripting 1 06-17-2008 02:08 PM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM
Basic bash 'for loop' usage Orange Stripes Shell Programming and Scripting 2 12-18-2007 08:58 PM
Bash while loop problem Kweekwom Shell Programming and Scripting 5 07-23-2007 12:49 AM
loop does not execute in bash script? fedora Shell Programming and Scripting 2 01-16-2007 12:38 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-25-2008
estienne estienne is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
bash and ksh: variable lost in loop in bash?

Hi,

I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh).

The goal of my script if to read a "config file" (like "ini" file), and make various report. I really simplified my script and reproduce the problem. It seems that variable inside "while loop" are lost in bash, but not in ksh?

Here's the script:

#!/bin/bash

function read_configfile
{
typeset configfile="$1"
typeset Hostname="$2"

grep '=' $configfile | sed "s/'//g" | while read ligne; do
variable="$(echo "$ligne" | awk -F= '{print $1}')"
value="$(echo "$ligne" awk -F= '{print $2}')"
eval "$variable='$value'"
done

echo "Affectation=$Affectation"
}

read_configfile bogus.in $(hostname)


And the bogus.in file:

Affectation=Yes life is good

When run, the output is

Affectation=

So... First of all I want to understand WHY the behavior is different. Is it because the way the "pipe" are processed?

Second, I found a way to bypass this. I just put every "variable=value" in a file (echo equation >>file), and outside the loop I'm doing an eval, and it works. But I would prefer NOT to use temporary file, so if you have any suggestions...

Thanks.

Last edited by estienne; 08-25-2008 at 12:55 PM..
  #2 (permalink)  
Old 08-25-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
See: Help in dealing with arra
  #3 (permalink)  
Old 08-25-2008
estienne estienne is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Thanks a lot. I was afraid of that. I should have tested it more before posting. Grrrrrrrrr... I don't like this behavior of bash!

Thx.
Sponsored Links
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 11:30 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