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

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

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)

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

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:

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 :wink:

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

[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

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.

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

Yes, it is not obvious to find it. That is why I am explaining this about 5-10 times a day >:(

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:


#
# Script for updating installed software
#
#
#

# show me the repos
zypper lr

# update the repos
echo -e "
Enter Root password for refreshing
"
sudo zypper ref
echo -e "
press 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
Press 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.


# 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

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

Let me think a bit about this.

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…

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

#!/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.

thanks for the answeres, were very helpful! :slight_smile:

especially:

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.

thanks :wink:

and i’ve already seen the “shebang”, how you called it, in other scripts but i don’t really know
what it’s good for so i have not used it and the script also worked.
For what is the shebang good and what does it do? And should i use it in ever script?

greetings ca

On 12/19/2013 09:56 AM, CosmicAlien wrote:
>
> and i’ve already seen the “shebang”, how you called it, in other scripts
> but i don’t really know
> what it’s good for so i have not used it and the script also worked.
> For what is the shebang good and what does it do? And should i use it in
> ever script?

It’s a special comment-like thing that only works at the top of the script
which tells whatever opens it (‘bash’ probably in your case) how the file
should be executed. This means that gasp the file extension doesn’t
matter as much as what’s inside the script, in other words the book is not
judged by its cover. You can have scripts for all types of languages a
long as the system can interpret them (it has the interpreter, duh) and
the top line tells the system which interpreter to use (perl, python,
bash, ksh, csh, zsh, tcsh, your-own-sh, etc.). In this case, it says, use
/bin/bash though you’ll see other variants like ‘#!/usr/bin/env python’
which basically says ‘find the python executable in the PATH and use it’
instead of specifying the path explicitly. It’s called ‘shebang’ because
it is started by the combination of a sharp (#) and bang (!) symbols.
Yes, you may call them hash/pound and exclamation point, but in *nix land
a ‘!’ is a bang. Old ‘vi’ folks will recognize the phrase ‘q bang’ or ‘w
q bang’ to quit or write/quite forcefully from vi, skipping all errors
about things you’ve done incorrectly like not saving the file first.
Anyway… history.

http://en.wikipedia.org/wiki/Shebang_(Unix)


Good luck.

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

Shift-ZZ, now what? won’t write? :w q bang, that’ll teach you. Thanks for today’s little trip on Memory Lane :slight_smile: