Re: Need bash script Help

Not really. I was demonstrating that setting variables within the function meant that they were then available from the command line outside the function (ie the function innards don't get performed within a sub-shell).


I used OFS (output field separator) rather than IFS (input field separator) as you were interested in creating a value that would be available outside the function.

I understood you to say that you were having difficulty in creating the appended string and seeing its value, rather than its name. Did I misunderstand?

 Martin



----- Original Message ----- From: "Donald Marang" <donald.marang@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, July 25, 2010 9:08 PM
Subject: Re: Need bash script Help


I do not exactly follow this example. Are you saying another way to pass the value from the function is to just use echo statements and to have the main, calling script catch it from standard output? Otherwise, I am missing something. Is OFS a special variable, like IFS (Input Field Separator)? I have not come across it before.

Don Marang

--------------------------------------------------
From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
Sent: Sunday, July 25, 2010 3:50 AM
To: <programmingblind@xxxxxxxxxxxxx>
Subject: Re: Need bash script Help

Hi Don,

I may not be understanding what you want to achieve, but ignoring the logic, I can get, using the function below without the use of eval or multiple quoting:

$ function show
{
echo $1
_var="$2"
OFS="--"
_total="$1"$OFS"$2"
}

$ show "one two three" four
one two three

$ echo $_var
four

$ echo $_total
one two three--four


 Martin


----- Original Message ----- From: "Donald Marang" <donald.marang@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, July 25, 2010 12:31 AM
Subject: Need bash script Help


I hope there is someone here with a little experience with functions in a bash script. I wrote a function that will append a filename to a list of filenames. The first argument passed is the name of the variable for a list of filenames. I fashoned this method based on a bash tutorial on how to return values from a function. that use this method and the 'eval' statements within the if statement. Since bash functions can not returna string, passing just the name seems to be a poor man's way to call by reference. The second argument is a filename that will be appended to the lisst.

I think everything is in order except the line below:
eval _ResultString="'$1'"

I need _ResultString to contain the actual filenames in the variable name in the variable whose name is the first argument. For some reason I can not get this statement correct.

Can anybody know how this assignment statement should look?


Here's the function:
AddFilenameToString() {
# $1 is the variable name of the sttring of filenames
# $2 is the new filename to Add
local _ResultName="$1"
local _ResultString=""
eval _ResultString="'$1'"
echo "variable name = $_ResultName"
echo "value = $_ResultString"
if [[ -z "$_ResultString" ]]; then
 echo "create new string"
 eval $_ResultName="'$2'"
else
 echo "append to string"
 eval $_ResultName="'${_ResultString}${IFS}$2'"
fi
}

Don Marang
__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind

Other related posts: