Friday, May 3, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 639  / 2 Years ago, tue, january 11, 2022, 9:48:11

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.


More From » command-line

 Answers
6

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.


[#25646] Thursday, January 13, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fatuatexen

Total Points: 451
Total Questions: 107
Total Answers: 110

Location: Trinidad and Tobago
Member since Thu, Apr 27, 2023
1 Year ago
fatuatexen questions
;