Mailinglist Archive: opensuse (1786 mails)

< Previous Next >
Re: [opensuse] question on if clause formatting
Hello,

On Thu, 10 Nov 2011, Ed Greshko wrote:
On 11/10/2011 10:25 AM, George OLson wrote:
I am trying to set up ssh, and I find different command line examples
on the internet to help in my research. As I am learning CLI and
scripting, can someone explain the following to me please?

In a given directory, I typed in the following line.

if [ ! -f authorized_keys ]; then touch authorized_keys ; chmod 600
authorized_keys ; fi

It created the file authorized_keys and gave it the time stamp and
changed the permission. I got that part.

The only thing I don't understand is the very first boolean clause
after the "if".

What exactly does the exclamation mark signify?

What also is the "-f" option?

http://www.linuxtutorialblog.com/post/tutorial-conditions-in-bash-scripting-if-statements

is one of the many places you'll be directed to if you use an
appropriate google search.

That stuff is _SO_ WRONG!

The syntax is:

if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else
COMMANDS; ] fi

Note: COMMANDS. Not(!) EXPRESSION.

'if [ .. ]; then' is just a weird way to write

'if /usr/bin/[ ... ]; then'
or
'if builtin [ ... ]; then'

which both are weird variants of '/bin/test' / 'builtin test' with the
extra quirk to gobble up ']' as the last argument. And it's only an
argument if it's seperated from the rest by a space etc., or else a ]
will be part of the argument before it.

In short: NEVER EVER EVER use [ .. ]. Use 'test'.

The EXPRESSION comes with 'test':

$ help test
test: test [expr]
Evaluate conditional expression.
[..]
-f FILE True if file exists and is a regular file.
[..]
! EXPR True if expr is false.

OTOH: [[ ... ]], (( ... )), $( ... ), $(( ... )), $[ .. ], _are_ bash
syntax and NOT to be confused with [ .. ].

$ help [[
[[ ... ]]: [[ expression ]]
Execute conditional command.
$ help '(('
(( ... )): (( expression ))
Evaluate arithmetic expression.

==== man bash ====
Arithmetic Expansion
$((expression))
====

-dnh, too tired now to elaborate properly.

--
"Cynical" is a term invented by optimists to describe realists.
-- Gregory Benford
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse+owner@xxxxxxxxxxxx

< Previous Next >
Follow Ups