Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: bash script if then else

  1. #1

    Question bash script if then else

    Hey people!

    I started scripting a few days ago and now i wanna write a script for updating my system with zypper.
    But i'm having some troubles. Loading the repositories and refreshing them works, but now i want that
    zypper shows me the available updates. And then if there are no new updates you can exit by pressing
    ENTER or you press ENTER and update what has to be updated.

    My problem now is how do i get the value of zypper -list-updates for the if-condition?

    it should look something like:

    if[zypper -lu == "no updates"]
    then
    echo "no updates"
    else
    zypper update
    fi

    with the red marked thing i have a problem.
    i'm glad for every kind of help

    cheers

  2. #2
    Join Date
    Jul 2008
    Location
    Seattle, WA
    Posts
    17,317

    Default Re: bash script if then else

    On Fri, 06 Dec 2013 17:26:02 +0000, CosmicAlien wrote:

    > My problem now is how do i get the value of zypper -list-updates for the
    > if-condition?


    For what the return values are, see "Exit Codes" in 'man zypper'.

    You can assign the return value as a variable, or you can use the command
    in the comparison, as I recall.

    Jim



    --
    Jim Henderson
    openSUSE Forums Administrator
    Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

  3. #3
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: bash script if then else

    On 2013-12-06 18:26, CosmicAlien wrote:

    > My problem now is how do i get the value of zypper -list-updates for the
    > if-condition?


    You can try to find out if zypper issues different exitcodes (you get
    them with $?), else you have to parse the output.

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 12.3 x86_64 "Dartmouth" at Telcontar)

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,811

    Default Re: bash script if then else

    Hello CosmicAlien,

    Welcome here.

    First I want to mention a feature here on the forums that is not automaticaly or easy to be found by first posters like you: the CODE tags. When evr you post computer text, please copy/paste it between CODE tags. You get the CODE tags by clicking on the # button in the tool bar of the posteditor. It will preserve the layout as it is on your terminal amongst other things. Something that is specialy important when we want to see the white space in statements.

    Then you should be a bit more talkative about what you want to achieve. Maybe you should keep in mind: do not describe the step, but the goal.

    You talk about using
    Code:
    henk@boven:~> zypper -list-updates
    Unknown option 'l'
    Unknown option 'i'
    henk@boven:~> zypper -lu          
    Unknown option 'l'
    Unknown option 'u'
    henk@boven:~
    As you see both statements are not doing something very usefull. Thus first checking what zypper statement you need for whatever your goal is, using:
    Code:
    man zypper
    as Jim advises would be good.
    I was curious and found:
    list-updates (lu) [options]
    List available updates.
    It seems that the - sign you put at the start is of your own invention

    From the man page of bash:
    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 status of the last command executed, or zero if no condition tested true.
    So after the "if" you need a "list". You used the list
    Code:
    [zypper -lu == "no updates"]
    That looks like a test or [ statement:
    test expr
    [ expr ]
    Return a status of 0 or 1 depending on the evaluation of the conditional expression expr. Each operator and operand must be a separate argument. Expressions are composed of the primaries described above under CONDITIONALEXPRESSIONS. test does not accept any options, nor does it accept and ignore an argument of -- as signifying the end of options.
    And thus there must be a conditional expression between the [ ... ].
    The == lets us assume that you want to compare two strings. But left of the == there are two strings because of the white space between zypper and -lu. And even when you would make the syntax correct by putting e.g. " " around those two words, you are then still comparing two strings. I can tell you the outcome "zypper -lu" is never equal to "no updates".

    You should not use "zypper list-updates" as a string, you must execute it, catch the output and check it for the string "no-updates". Something like:
    zypper lu | grep -q 'no updates'
    which will return 0 as the string i found in the output.
    Then your statements woul be sometyhing like
    Code:
    if zypper lu | grep -q 'no updates'
    then    echo "No updates"
    else    echo "Updates"
    fi
    This is not tested by me. Please look if it helps and come bacl with further questions without forgetting hat man pages ar your friend, specialy when programming.
    Henk van Velden

  5. #5

    Default Re: bash script if then else

    thanks for the help so far!

    @hcvv: i wanted to use the CODE tag but i didn't find it

    and i made some progress

  6. #6
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,811

    Default Re: bash script if then else

    Quote Originally Posted by CosmicAlien View Post
    @hcvv: i wanted to use the CODE tag but i didn't find it
    Yes, it is not obvious to find it. That is why I am explaining this about 5-10 times a day
    Henk van Velden

  7. #7

    Default Re: bash script if then else

    I've finished the script now and it works(like i want it to work). There is just one small bug but i will fix it later when i
    have ran the program or you guys have a solution.

    here the CODE:
    Code:
    #
    # Script for updating installed software
    #
    #
    #
    
    # show me the repos
    zypper lr
    
    # update the repos
    echo -e "\nEnter Root password for refreshing\n"
    sudo zypper ref
    echo -e "\npress ENTER to continue"
    read
    
    # show me available updates
    zypper list-updates
    
    # if no updates exit, else update(but ask me first :-) )
    if  zypper lu | grep -q 'No updates'
    then
    
            echo -e "No updates needed\nPress ENTER to exit"
            read
            exit 0
    
    else
            echo -e "Do you want to update[y/n]?\c"
            read agree
    
            if [ $agree = 'y' ]
            then
                    sudo zypper -n up
            else
    
                    echo "Press ENTER to exit"
                    read
                    exit 0
            fi
    fi
    The bug is that i execute the command zypper lu for showing me the list and for the if condition.

    Code:
    # show me available updates
    zypper list-updates
    
    # if no updates exit, else update(but ask me first :-) )
    if  zypper lu | grep -q 'No updates'
    then
    ...
    so it's executed twice which requires twice the time.
    I will fix that bug later, the script does his job(although there is room for improvement)
    Anyway, how is that script written? It's my first real script and a feedback would be great!

    so, cheers
    CoAl

  8. #8
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,811

    Default Re: bash script if then else

    That is not a bug, it is something you do not like, which I can understand.

    Let me think a bit about this.
    Henk van Velden

  9. #9

    Default Re: bash script if then else

    My personal opinion is that one reason this doesn't work nicely is that
    the script is duplicating what zypper does already in that it determines
    if there is something to do and, if so, prompts to go head and do it, or
    alerts you that there is nothing to do (and then has an extra prompt which
    is really not helpful since the output from the script would be there even
    if the last 'read' was removed.

    For a first script, I think it's good; I personally really appreciate
    indenting blocks of code, recommend single-quotes whenever possible to get
    in the habit of using them correctly, and usually add more metadata at the
    top describing the script, author, version, and of course include a
    "shebang" line so that the system can identify the correct interpreter of
    the script. Most of these fall in the preference/semantic category, but
    they work well for me.

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below...

  10. #10
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,811

    Default Re: bash script if then else

    I am completely with ab. IMHO it is important to have a style of writing programs (the way to indent, etc) and stick to it. And you do this, thus it is pleasant to read and thus easy to understand.

    I also miss the shebang. You first line should be
    Code:
    #!/bin/bash
    (if this script is a Bash script, else the name of the correct scripting languge).

    And about the double usage of zyppr lu, I realy do not know a good solution. We use the -q option of grep to suppress the output and to stop the grep processing when the first match is found. That is a feature build in in grep with this sort of usage in a script in mind. But zypper is not designed like that. It is apparently designed as an interactive program, where the output is to be interpreted by human interaction. As long as zypper has no option to check if at least one update is available and then stop processing and having a return code teling if an update was found or not, there is no better solution imho. And as long as that is the case we use grep for this purpose. Nothing strange thhere.
    Henk van Velden

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •