LinuxQuestions.org
Help answer threads with 0 replies.
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 02-13-2011, 07:23 PM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
bash: cant reach internal var from outside!


Hi,

any idees how I get mystr: unix to become mystr: linux?



#!/bin/bash

mystr="unix"
echo "linux" | while read myline
do
mystr="$myline"
echo "str: "$mystr
done
echo "mystr: "$mystr


# $ bash test
# str: linux
# mystr: unix
 
Old 02-13-2011, 08:28 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Because you are piping the information into the while loop it is creating its own shell and hence anything set within the loop is lost once
the shell is closed (ie when the loop ends).
You can check this out for the reasons and solutions.
 
Old 02-13-2011, 10:22 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Hey, grail, your BashFAQ/024 is great, but lacks one useful method: using a temporary descriptor to pass the result values. This works in all POSIX shells, like bash and dash, it might even work in Bourne shell.
Code:
exec 3>&1
result=`some-command some-parameters ... >&3 ; echo new-result`
exec 3>&-
For longer stuff and multiple variables, this can be extended to
Code:
exec 3>&1
result=(`exec 4>&1 1>&3

         # do some bash stuff, stdout and stderr work normally

         echo newval1 newval2 newval3 >&4
       `)
exec 3>&-
val1="${result[0]}"
val2="${result[1]}"
val3="${result[2]}"
No descriptors are left open; this is quite clean. Although $(...) would nest much better, older shells may only support `...` so I used the latter just in case here.
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 07:58 AM.
 
Old 02-14-2011, 12:24 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
for bash, perhaps simply:

Code:
mystr="unix"

while read myline; do
    mystr="$myline"
    echo "str: $mystr"
done < <(echo "linux")

echo "mystr: $mystr"
noting:
Code:
command1 | while ...
puts 'while' into another subshell
 
Old 02-14-2011, 03:24 AM   #5
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
thanks all to all of you. really helpful.

i normally program in c and do not know bash that well.

This is for automatic mounting of samba(with root since the S option do not work any more) and nfs command. The plan is to also make thunar plugins.


Where do i publicize these commands later? Any idees?
 
  


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
xml_grep from bash script giving errors on var. brizey Linux - Software 1 08-16-2010 04:55 AM
[SOLVED] in bash, how could I check if $var is not start with 0 junust Programming 14 05-25-2010 01:19 PM
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
bash shell env var script ejbest Linux - Newbie 4 09-29-2004 10:23 AM
bash script+ftp+ls->var g00ral Programming 1 05-05-2004 06:58 AM

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

All times are GMT -5. The time now is 12:37 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