If I generate a "Package Download Script" in Snyaptic can it be rn from a terminal window instead of Synaptic?

Avi Greenbury lists at avi.co
Mon Oct 22 13:31:59 UTC 2012


Nils Kassube wrote:
> jimmckenzie at earthlink.net wrote:
> > Is the && at the end of each line also required to run a script?
> 
> It isn't required at all. The "&&" means that the shell should run one 
> command at a time and continue with the next command only if the 
> previous command was run successfully. 

It works in the way you describe if you do this:

 command1 && command2 && command3 

where it will 'exit' that list of commands as soon as a command fails.
But, if you did this in a script:

 command1 && command2 && command3 
 command4 && command5

And command2 failed, it would not run command3 but would go on to run
command4 (and then try command5 if it succeeds).Basically, an && only
has that effect on a single line. 

When you give bash a construct such as:

  if [condition1 && condition2]; then do something(); done

You are saying that before doing something() it must check that both
condition1 and condition2 return true. If condition1 returns false
there is no need to test condition2, since there is now now way that
both will return true, so the 'condition2' is never evaluated. This is
occasionally known as 'short-circuit' evaluation[0]. 

Bash allows you to have these evaluations in arbitrary places, without
needing to have a thing to do in a loop, which is where that sort of
construct comes from. But it must be in a single statement, so without
any (non-escaped) newlines.

If you want a script to exit if a command fails, you need to do this
sort of thing:

command1 || exit
command2 || exit
command3 || exit

Making use of a 'short-circuit or', which is roughly the same as the
and - if the left-hand side returns false, the right hand side needs
to be evaluated to ensure that at least one of them returns true. If
the left-hand side returns true there is no need to test the
right-hand side.

-- 
Avi

[0] http://en.wikipedia.org/wiki/Short-circuit_evaluation




More information about the ubuntu-users mailing list