Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm pretty new to Ubuntu linux, let's say all of linux because I installed it a few days ago. I'm sorry for my mistakes in my use of English, because I live in the Netherlands.

Well I tried to do some programming, writing a script, but I encountered a problem which I haven't been able to solve yet. Well I was writing a script, and I want this script to automatically install a few things for me. Well I was doing and tried to make as less nests as I could, and I finished my script, but I thought. Why wouldn't I try to put a few lines in it which check if the programs are correctly installed. So I wondered how I could do it and I came up with the following:

Because I'm new to programming I wanted it to be so advanced as I could by myself. So I decided to try to work with shell functions. These functions check if the correct file is there, and the outcome of these functions has to be a value which I can use later. Here an example:

function isitthere
{
if [ -f <thefile> ]; then
true
else
false
fi
}

if [ $(isitthere) = "true" ]; then
echo "Your program is properly installed"
else
echo "Your program isn't properly installed.
fi

In this example are true and false my values, and as you see, I try to re-use them in my second command. I know this can be done much more easily, but I want to know if it is possible, and if it is possible how. Because in one part of my script doing it like this makes it much easier. Is there any way you can give a value to your if command and use that value later in your next if command? Thanks.

share|improve this question

3 Answers 3

up vote 0 down vote accepted

There are many ways of doing this.

  1. Use the inbuilt features of the shell. Commands (and functions) have exit values. Usually, an exit value of 0 means the command was successful and a value > 0 means it failed. This is checked by the shell when you use the || or && operators. So, to run a command only if the previous one was successful, you would use &&, ro run it unless the previous was successful, you use ||. For example:

    $ foo
    The last command was succesful
    echo 'foo' && echo "The last command was succesful"
    

    If you use a command that does not exist, it will obviously fail:

     $ echooo 'foo' && echo "The last command was succesful"
     echooo: command not found
    

    Note that the second message is not printed. If you use || instead:

    $ echooo 'foo' || echo "The last command failed"
    echooo: command not found
    The last command failed
    

    You can combine these into your shell function:

    function isitthere
    {
        [ -f thefile ]
    }
    
    isitthere && echo "The file exists" || echo "the file does not exist"
    

    The test operator (also known as [ ]) returns an exit value of 0 on success and 1 on failure. This means that your function can be simplified to the above.

  2. Use the return command. This is actually exactly the same as the example above, I am simply stating it more explicitly. It also allows you to return more complex values for different fail cases.

    function isitthere
    {
        if [ -f <thefile> ]; then
            return 0
        else
            return 1
        fi
    }
    
    if [ $(isitthere) ]; then
        echo "Your program is properly installed"
    else
        echo "Your program isn't properly installed.
    fi
    

    I used the same syntax as you to illustrate, but this could be shortened to

    isitthere && echo "Your program is properly installed" || echo "Your program isn't properly installed."
    
  3. Have your function print something to standard output and then act accordingly.

    function isitthere
    {
        [ -f thefile ] && echo yes || echo no
    }
    
    if [ $(isitthere) = "yes" ];
    then
        echo "Your program is properly installed"
    else
        echo "Your program isn't properly installed."
    fi
    

    In this example, we're using the standard output of the function and not its return value. To capture the output of a command (or function) you need to use command substitution. This allows you to run a command and save the output as a variable. For example

    $ foo=$(echo bar)
    $ echo $foo
    bar
    

    So, the test if [ $(isitthere) = "yes" ] checks whether the output of the functin was yes and acts accordingly.


Note that I have used both [ -f thefile ] && echo yes || echo no and the more complex

if [ -f <thefile> ]; then
    return 0
else
    return 1
fi

The two are basically equivalent, I used both only to illustrate that. You can use whichever you prefer.

share|improve this answer

Keep it simple; this works alright (tested in Ubuntu 12.04):

#!/bin/bash

function isitthere
{
    [ -f TheFile ]
}

if isitthere; then
    echo "Your program is properly installed."
else
    echo "Your program isn't properly installed."
fi
share|improve this answer

Welcome Dutch fellow!

You could use something like this:

result=isitthere

Here you assign the return value of your function isitthere to the variable result. This variable now contains, in this case, true or false. You can always retrieve the value of result using $result.

However, if you want to catch the output (STDOUT) of your function (for example if you use echo inside your function) you should use:

result=$(isitthere)
share|improve this answer
    
This code does not work; the first two lines are syntactically incorrect and both give an error. –  Ruud Apr 26 at 18:35
    
Feel free to correct it, I was just trying to explain the idea of variables. –  Louis Matthijssen Apr 26 at 18:41
1  
Variable $result does not solve OP's problem; you just introduced one more issue (variable assignment must be without $) on top of the three bugs already present in OP's code. A correction from my side would change the code beyond recognition, which is inappropriate for an answer that got both upvoted and accepted (though it's a mystery to me why). So instead, I posted a separate answer. –  Ruud Apr 26 at 19:30
    
Well, he asked if he can store the return value of his function so he can use it later, so I explained it. I've removed my typo and the unnecesary code. –  Louis Matthijssen Apr 26 at 19:57
    
Good point; sorry, I was a bit too focused on code, skipped through most of the smalltalk. Anyway, you may want to add that it should be isitthere instead of $(isitthere). The latter captures stdout, and there's not much of that coming out of function isitthere. –  Ruud Apr 26 at 20:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.