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.

...

A shell is a UNIX system command processor. Basically, it is a command language to tell the system what to do. There are two major shell (command interpreter) families: 
    Bourne, Korn, Bash Shell
    C Shell
   
The syntax of shell commands can vary from one shell to another.
At UQAM we use the Bourne Again Shell (bash). Therefore all commands below are bash commands.

Syntax of a shell command

        command-name [ -option(s) filename(s) or arguments ]

...

      ls -l -t -r
is the same as
      ls -lrt

In case you have a file name or an argument starting with a '-' you can use '--'. The command will NOT interpret anything following a '--' as an option/key.
For example if you are using 'grep' to look for '-a' in a file you could type:
    $  grep -iw -- -a filename

For more detailed explanations about Program Argument Syntax Conventions, please click on the here.

Documentation of specific commands

      whatis command    - displays a one-line summary about command
      man command         - displays on-line manual pages about command

'man' can be use on almost all of the shell commands listed below to get the complete description of the command.

Shell commands are case sensitive

Type commands exactly as shown; most shell commands are lower case. File and directory names can be lower, upper, or mixed case but must be typed exactly as listed.

...

<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

...

Conditional structures

if-then-else

Volet
if    expression_1; then
   Statement(s) to be executed if expression 1 is true
[elif expression_2; then
   Statement(s) to be executed if expression 2 is true
elif expression_3; then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true]
fi

...

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

Loops

Loops can be used to execute (parts of) code repeatedly.

for-loop

Loop over different fixed elements

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


Examples:

Loop over numbers:

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

Loop over files:

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

while-loop

Repeat statement(s) while 'condition' is true.

Volet
while

...

condition ; do
   Statement(s) to be executed while expression is true
done

Example:

Volet
a=1
while (( a <= 10 )) ; do
    echo $a
    a=$(( a + 1 ))     # increase the loop parameter
done


You One can also use a while-loop to read lines from an ASCII file.
Example:

Volet
   

...

cat > text_file << EOF
    > Hello
    > How are you?
   

...

EOF

   

...

while read line ; do echo $line; done < text_file
    Hello
    How are you?

break

...

& continue

To interrupt a loop you can use 'break' and 'continue'.
      break       interrupts the loop
      continue interrupts the current cycle of the loop

Manipulating Variables

Example string:
     $       string="ABC_abc_123"String

Get string length:

     $       echo ${#string}

Remove patterns from string

#

...

removes minimal matching prefixes
##

...

removes maximal matching prefixes
%

...

removes minimal matching suffixes
%%

...

removes maximal matching suffixes
:s:

...

nget 'n'

...

characters starting from position 's' (first position is 0)

Look at these examples and you will understand the meaning of the above:
    $      string="ABC_abc_123"
    $      echo ${string#*_}     
             abc_123                                             123                                           => removes everything before first occurance of '_'
    $      echo ${string##*_}
             123                                                    => removes everything before last occurance of '_'
    $      echo ${string%_*}
             ABC_abc                                            abc                                          => removes everything after last occurance of '_'
    $      echo ${string%%_*}
             ABC                                                   => removes everything after first occurance of '_'
    $      echo ${string:s:n}
             abc                                                    => gets n characters, starting from position s

cut

Cut out/print part of the content of a variable:
cut prints selected parts of a string
Options:

...

string.

Most useful options:

-cselect only these characterscharacters according to their position in the string
-ddelimiter=DELIM; use DELIM instead of TAB for field delimiter
-fselect only these fields/columns


Examples:
    $      abc="I am hungry"
    $      echo $abc | cut -c 6-9
             hung                                                => returns characters 6-9
    $      echo $abc | cut -c 6-
             hungry                                            => returns characters 6 to end
    $      echo $abc | cut -d' ' -f 3
             hungry                                            => returns 3rd element with ' ' used as seperator
    $    abc="I am hungry. Are you?"
    $      echo $abc | cut -d. -f 2
             
Are you?                                        => returns 2nd element with '.' used as seperator

set

When you call calling set without options but with one or more arguments, it sets the values of the command line argument variables ($1-$9$n) to its arguments.
Examples:
    $      abc="I am hungry"
    $      set $abc
    $      echo $3
             hungry

When options are specified, they set or unset shell attributes.
Options:

    -xexpands each simple command
This is very useful for debugging scripts!!!
+xno expanding of commands anymore

For more options check man page: man set
Examples:
    $      abc="I am hungry"
    $      set -x
    $      abc="I am hungry"
      + abc='I am hungry'

& -> Send job in background

You can send a job in the background by adding a '&' at the end of the command line.
This is useful to get the prompt back - beeing being able to continue using the window - when calling a program whic which opens a nother another window, like for example Matlab, emacs, xrec.
Example:
    $ matlab      emacs &

In case you forgot to add the '&' you can still send an already launched job into the background with 'Ctrl-z' followed by 'bg' (for background):
Example:
    $      emacs
         Ctrl-z
            [1]+  Stopped                 emacs
    $      bg
            [1]+ emacs &

Check and kill running processes

ps reports a snapshot of the current processesOptions

Most useful options:

...

-eselect all processes
-fdo full-format listing
-u userlistthis selects the processes whose effective user name or ID is in userlist


Try for example:
    $      ps -fu $USER

If ever you need to kill one of these jobs you can use 'kill' followed by the 'PID'.
If a normal 'kill' does not work try 'kill -9' followed by the 'PID'.