Comparaison des versions

Légende

  • Ces lignes ont été ajoutées. Ce mot a été ajouté.
  • Ces lignes ont été supprimées. Ce mot a été supprimé.
  • La mise en forme a été modifiée.

...

Cursor movements in command line

emacs mode:

<Esc> .repeat last word of previous command
Ctrl-ajump to beginning of line
Ctrl-ejump to end of line
Ctrl-rsearch backwards in command history 
Ctrl-cinterrupt current command/process

Control structures

if then else

...

Arithmetic expression examples ( a=2 ): 
    (( a == 3 )) 
    (( b != 3 ))
    (( $a >= 3 ))
In Arithmetic expressions '(( ... ))' it is not necessary to put the '$' in front of variables since strings are not allowed so all strings are assumed to be variables.

String expressions examples ( abc="Bonjour" ):
equal:
    [[ "$abc" == "Hello" ]]
not equal:
    [[ "$abc" != "Hello" ]]

The more common but less recommended way is:
    [ $a -le 3 ]
resp.
    [ "$abc" -nq "Hello" ]

Note: It is mandatory to keep a 'space' just before and after the double and single square brackets!!!

and: &&
or: ||

If you want to know more about the comparison operators try:
     man test
or
     man bash            -> CONDITIONAL EXPRESSIONS

case-statement

A case statement is a conditional control structure that allows a selection to be made between several sets of program statements. It is a popular alternative to the if-then-else statement when you need to evaluate multiple different choices.

Volet
case test_string in
    pattern_1 | pattern_2 )  statement(s) ;;
    pattern_23                       )  statement(s) ;;
    *                                        ) default_statement(s) ;;
esac

...

The pipe character '|' serves here as an "or".
The last line of each statement needs to be finished with ';;' at the end.

Examples:

Volet

#  Determine number of days per month
case"$month"in
    01|03|05|07|08|10|12)days=31;;
    04|06|09|11)          days=30;;

...

    02)

...

                  if [$(( ${year} % 4

...

))

...

-eq0];then

...

                             days=29

...

                          else

...

                             days=28

...

                          fi;;

...

esac

for loop

for var in word1 word2 ... wordN ; do
   Statement(s) to be executed for every word.
done

Examples:

for hour in 00 06 12 18 ; do
  echo $hour
done

touch dm_01 dm_02
for fine in dm* ; do
  echo $file
done

...