Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bash Script Help

 
Phil Powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am brand-spankin' new to Bash script from a web programming background, so my questions are going to, perhaps, sound "ignert" at best, but understand, I come from a TCL/PHP/Java/ASP background so I'm accustomed to All Things Web

Having said that, I need help. I wrote a bash script function that will return a string (or whatever bash comes closest to doing "return" as) and needs to return it into a variable that I will use later.

Here is the function:



Problem is that I have no clue as to how to capture the return of this function.

does not work
also does not work
also does not work
also does not work
[CODE excludedList=`echo \\`tarExcludedList\\''[/CODE].. you guessed it.

What do I do? I want to return the last line of tarExcludedList into a variable and have no clue how.

Help appreciated

Thanx
Phil
 
Phil Powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW I figured out I was using the wrong function name, nonetheless, nothing still works, I cannot output the function results no matter what I try.

I know in TCL it's this



In PHP it's this



and in Java it would be a string return that I just do



Ok, so how is it done in Bash?

Phil
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps only your test was wrong?
how did you test?

did you capture in the same script?
In another script?
From commandline?

in the script, you have to call the function.
But then you could assign a global variable to the result, and use the variable.

from shell:

should work, or better:


but then the script needs to call the function.

Else you could source the script, and call it on your own:


From commandline
 
Phil Powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps what I don't understand is the fact that bash script seems to have no concept of scope that I can fathom. Assigning a global variable failed upon doing so.

What I wound up doing was throwing out a weird code snippet that worked:



Where $excludedList was a variable set in the function that, for some wacko reason, becomes instantly global. This is nowhere near what I'm used to in any programming language so I never would have guessed that one.

Phil
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't work much with functions in scripts.

A very good tutorial with much more answers than I can give might be found here:http://freshmeat.net/projects/advancedbashscriptingguide/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic