• Forum
  • Lounge
  • can someone talk me through understandin

 
can someone talk me through understanding this basic bash tutorial

Hey im trying to work out how this returns different things, im not familliar with the brackets in brackets in the if statment (if what exactly?) the ';' and 'then' seem odd too.

http://www.linuxjournal.com/content/return-values-bash-functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function myfunc()
{
    local  __resultvar=$1
    local  myresult='some value'
    if [[ "$__resultvar" ]]; then
        eval $__resultvar="'$myresult'"
    else
        echo "$myresult"
    fi
}

myfunc result
echo $result
result2=$(myfunc)
echo $result2


what does fi mean??

and how does eval get called twice here

1
2
3
4
5
6
7
8
9
function myfunc()
{
    local  result=$1
    local  myresult='some value'
    eval $result="'$myresult'"
}

myfunc result
echo $result
Last edited on
*coff*
devonrevenge wrote:
im not familliar with the brackets in brackets
man bash wrote:
     [[ expression ]]
              Return  a  status  of  0 or 1 depending on the evaluation of the
              conditional expression expression.  Expressions are composed  of
              the  primaries  described  below  under CONDITIONAL EXPRESSIONS.


devonrevenge wrote:
the ';' and 'then' seem odd too. [...] what does fi mean??


man bash wrote:
       if list; then list; [ elif list; then list; ] ... [ else list; ] fi
              The if list is executed.  If its exit status is zero,  the  then
              list  is  executed.   Otherwise,  each  elif list is executed in
              turn, and if its exit status is  zero,  the  corresponding  then
              list is executed and the command completes.  Otherwise, the else
              list is executed, if present.  The exit status is the exit  sta-
              tus of the last command executed, or zero if no condition tested
              true.


Perhaps you need an even more basic bash tutorial first?
yeah your right its pretty different to other languages, can you recomend the ultimate beginner thing?? thanks for your help though
closed account (Dy7SLyTq)
ill help you. so in bash the if doesnt have {} instead the { is "then" and the } is "fi". if you type a command in the terminal, lets say "ls", and put a ; at the end, it will execute the same. you have it to show the end of a command like in c++, allowing for multiple commands on one line. so the if[[expression]]; then is like saying if(expression) { in c++. unfortunately i can't remember the signifigance of [[]]
ahh thanks thats pretty good, I got the impression that [[]] is something like if statment is not null sort of thing but then the code isnt all that clear in my brain box, my brain box needs sleep actually, thanks dts, i can't usually sleep till the little annoyance thing is out my head.

I will have sweet dreams without if statments in now :)
For some reason, the person who wrote the original Bourne shell decided that conditional statements should end with their own name in reverse, so if ends with fi and case ends with esac. It doesn't work for loops, though; for and while both end with done.
ah kay, all this just to get a return statement:/ would it make more sense making loads of small programs and piping them all together?
closed account (Dy7SLyTq)
also, idk about for but while starts with do
Both of them use do.
I quite liked csh (C shell) or tcsh (Technical C shell) because they look like the C language - no more fi's or esac's.

However bash & sh seem to be the standard these days. csh seemed to have a lot of the same functionality as sh - I am not sure whether bash has more - I wouldn't be surprised if it did.

I guess if one was going to fool around with their own stuff, it doesn't matter what one uses. However if you do shell scripting for others then you are probably better sticking with bash. Of course one is probably expected to know bash for work.

Edit:

@devonrevenge

Have you read the bash man page?
Last edited on
yeah, its a little cryptic
Topic archived. No new replies allowed.