Monday, 28 July 2014

Shell programming (Posix)

I had learn some shell programming basis. I summarized the things to here. I suggest you always try to follow the posix (easier to understand and you can avoid specific shell issues).

Rules (I continuously will update it):

  1. Do not use pushd/popd! Use 'cd -'.
  2. Do not use double equal to compare. This is not c++. Use just one ('=').
  3. Do not use double bracket [[ $foo ]]. Use single bracket ('[ $foo ]').
Difference '$foo' and "$foo":
$export foo=cica
$echo "$foo"
cica
$echo "$foo"
$foo
There is substitution at first case while the other one there wasn't.

Shell script usually started with shebang (or hashbang):
#!/bin/sh

Debug features in shell:
set -euxv

e : stop first error
u : check uninitialized variables / prevent undefined behavior
x : print line the executed command
v : print line as read from input

In posix the return values always a positive numbers (0-255).

No comments:

Post a Comment