Bash Course

download Bash Course

of 23

Transcript of Bash Course

  • 8/2/2019 Bash Course

    1/23

    BashAn integrated introduction to: Linux/Unix terminal bash programming.

    Fbio Csar CanesinD i r e c t o r o f t e c h n o l o g y C o n v e r g e E n g i n e e r i n g

  • 8/2/2019 Bash Course

    2/23

    BE AWARE !If you are reading this presentation from our OpenSource

    initiative be aware that are much more capabilities verballytalked in the course.For more details about the commands try:

    $man commandExample

    $man echo

    This course is not an comprehensive C++ and bash training. This isthe preparatory course to our ConvergeBranch training course.

  • 8/2/2019 Bash Course

    3/23

    Bash:Linux terminal

    Basic concepts and usage of bash language inthe Linux terminal for system management.

  • 8/2/2019 Bash Course

    4/23

    Bash: Bourn again shell

    Drop-in replacement for Bourn shell. One of the first top priority projects ofthe Free Software Foundation. Bash is a POSIX shell!

    POSIX "Portable Operating System Interface for Unix is the name of afamily of standards specified by the IEEE to define theapplication programming interface (API), along with shell and utilitiesinterfaces.

    Initial release in June 1989, last release February 2011, talk about up todate!

    Bash gives acess to the fundamental unix programs, here we will just teachwhat we feel that are the best tricks and most useful stuff

  • 8/2/2019 Bash Course

    5/23

    !$ Repeating a command

    The argument !$ gives you the last used argument. In the example, we create the folder Programming with the

    command mkdirthen we use the command cd to enter the folderProgramming and then use the double dot .. to reference to the

    up level folder.Using the command && to chain two commands inone. using the !$ macro we repeat the last argument,Programming, so we exit and enter the folder. You can checkthe folder you are withpwd

    Try to use the shortcut: Esc + . Try now to do it rapidly.

  • 8/2/2019 Bash Course

    6/23

    There are plenty of shortcuts! Bash by default uses Emacs editor key bindings, so there is plenty of useful

    stuff to programming in the terminal, some of the most cool shortcuts are:

    Ctrl + a: Return to the start of the command you're typing Ctrl + c: Sends the signal SIGINT to the current task, which aborts and closes

    Ctrl + e: Go to the end of the command you're typing

    Ctrl + u: Cut everything before the cursor to a special clipboard Ctrl + k: Cut everything after the cursor to a special clipboard Ctrl + y: Paste from the special clipboard that Ctrl + u and Ctrl + k save their

    data to

    Ctrl + t: Swap the two characters before the cursor (you can actually usethis to transport a character from the left to the right, try it!)

    Ctrl + w: Delete the word / argument left of the cursor Ctrl + l: Clear the screen

    vPS: Tab is like magical

  • 8/2/2019 Bash Course

    7/23

    Foreground and background

    top shows information about the running processes in the system andmetrics about the system usage.

    Use Ctrl+z to interrupt an process and the bg command to put it inbackground.

    Use the command fg to bring the last background process to theforeground. Use the commandjobs to list current running processes Use fg %i where i is the [id] in the jobs output. Instead of doing Ctrl+z and bg, you can use the & simbol

  • 8/2/2019 Bash Course

    8/23

    Cat echo: its like jeans, all day.

    echo prints to stdout the argument it receives, in this examplewe use it to print a string

    catprints to stdout the content of the argument it receives, inthis example we print the content of the file this_course that isat the Programming folder

    As you all can see ls list the content of a folder ;-)vWait ! stdwhat ?!?!

  • 8/2/2019 Bash Course

    9/23

    Standard streams

    Standard streams are Unix concepts, they are channels wheredata can be transported, there are three:

    stdout: The standard output is a channel to be used by a programto export data. Usually, stdout output is received by the terminal

    environment in which the program is running, in our case bash.stdin: The stdin channel can be used by other programs to feed

    data to a running process.

    stderr: The stderr is like the stdout but for errors, when a programneeds to inform that an error has occur it will print to stderr insteadof stdout. Stderr also output to the terminal.

  • 8/2/2019 Bash Course

    10/23

    Substitution

    There are several ways of doing substitution in bash the most used are: The command substitution$(command argument): This will substitute the tag $() with

    the stdout of the command.

    The process substitution

  • 8/2/2019 Bash Course

    11/23

    The find command

    The find command is another powerful Unix program, it can be used together with grep, cp, sed andrename to create complicated features. Some examples are:

    Find files that are over 1 GB but less than 20 GB in size:$ find ~/Movies -size +1024M -size -20480M -print0

    Find .doc files that also start with 'questionnaire' (AND) :$ find . -name '*.doc' -name questionnaire*

    Find files have been modified within the last day:$ find ~/Movies -mtime -1

    Find .doc files that do NOT start with 'Accounts' (NOT):$ find . -name '*.doc' ! -name Accounts*

    Find any .c and .h file that contain the phrase I need$ find . -name "*.[ch]" | xargs grep -E phrase I need

    Find any file that contain the phrase I need and then print the file that match the expression:$ find . -type f -exec grep phrase I need" {} \; -print

  • 8/2/2019 Bash Course

    12/23

    If conditional

    The if conditional is used as usual, in the syntax: if ;

    then

    else

    fi

    But there are super dupper shell stuff in it, for working with files: http://www.linuxtutorialblog.com/post/tutorial-conditions-in-bash-scripting-if-

    statements

    If conditional can also be replaced most times for the test command and themarkers && for if true and || if false.

    PS: -a for and, -o for or

  • 8/2/2019 Bash Course

    13/23

    While loop

    While loop in the terminal, it do test like if conditional, as a loop can alsouse multiline output from stream processing programs

    while do

    done

    An example of using a stream with while loop: cut -c-5 /tmp/testez/comandos.txt | \

    while read LINEdo

    echo $LINHAdone

  • 8/2/2019 Bash Course

    14/23

    For loop

    This loop allows for specification of a list of values. A list ofcommands is executed for each value in the list.

    for NAME [in LIST ]; doCOMMANDS;

    done

    Think about multiple nested for and while loops with ifconditionals, all operating in multiple streams.

  • 8/2/2019 Bash Course

    15/23

    Regular expressions 1/5

    Now that you are more familiar with the way bash works (orshould at least) lets explain what regular expressions are:

    Aregular expression is a pattern that describes a set of strings.They are constructed analogously to arithmetic expressions byusing various operators (metacharacter) to combine smallerexpressions.

    Two regular expressions may be concatenated; the resultingregular expression matches any string formed byconcatenating two substrings that respectively match theconcatenated subexpressions.

    Two regular expressions may be joined by the infix operator "|";the resulting regular expression matches any string matchingeither subexpression.

    Repetition takes precedence over concatenation, which inturn takes precedence over alternation. A wholesubexpression may be enclosed in parentheses to override

    these precedence rules.

  • 8/2/2019 Bash Course

    16/23

    Regular expressions 2/5 These are the most used operators with bash in linux

    Operator Effect

    . Matches any single character.

    ? The preceding item is optional and will be matched, at most, once.

    * The preceding item will be matched zero or more times.

    + The preceding item will be matched one or more times.

    {N} The preceding item is matched exactly N times.

    {N,} The preceding item is matched N or more times.

    {N,M} The preceding item is matched at least N times, but not more than M times.

    -represents the range if it's not first or last in a list or the ending point of a range in alist.

    ^ Matches the empty string at the beginning of a line; also represents the charactersnot in the range of a list. (line anchor)

    $ Matches the empty string at the end of a line. (line anchor)

    \b Matches the empty string at the edge of a word. (word anchor)

    \B Matches the empty string provided it's not at the edge of a word. (word anchor)

    \< Match the empty string at the beginning of word. (word anchor)

    \> Match the empty string at the end of word. (word anchor)

  • 8/2/2019 Bash Course

    17/23

    Regular expressions 3/5

    Some examples of anchor usage together with grep,remember grep ?? There are two programs that are only reallypowerful when used with regular expressions:sed and grep.

  • 8/2/2019 Bash Course

    18/23

    Regular expressions 4/5

    Character classes: Numbers, Strings A bracket expression is a list of characters enclosed by "[" and "]". It

    matches any single character in that list; if the first character of the list is thecaret, "^", then it matches any character NOT in the list. For example, theregular expression "[0123456789]" matches any single digit.

    Within a bracket expression, arange expression consists of two charactersseparated by a hyphen. It matches any single character that sorts betweenthe two characters, inclusive, using the locale's collating sequence andcharacter set. For example, "[a-d]" is equivalent to "[abcd].

    There are named classes of characters predefined within bracketexpressions. They are: [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:],[:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].

    PS: Bracket expressions are aware of locale! Wildcards Use the "." for a single character match and for matching multiple

    characters, use the asterisk *.

  • 8/2/2019 Bash Course

    19/23

    Regular expressions 5/5 Some examples of very very simple regular expressions

  • 8/2/2019 Bash Course

    20/23

    Sed: Stream Editor 1/2 used to perform basic transformations on text read from a file or a pipe. The

    result is sent to standard output. The syntax for the sed command has no outputfile specification, but results can be saved to a file using output redirection. Theeditor does not (but it can) modify the original input.

    Command Result

    a\ Append text below current line.

    c\ Change text in the current line with new text.

    d Delete text.

    i\ Insert text above current line.

    p Print text.

    r Read a file.

    s Search and replace text.

    w Write to a file.

    Option Effect

    -e SCRIPTAdd the commands in SCRIPT to the set of commands to be run while processingthe input.

    -fAdd the commands contained in the file SCRIPT-FILE to the set of commands tobe run while processing the input.

    -n Silent mode.

    -V Print version information and exit.

  • 8/2/2019 Bash Course

    21/23

    Sed: Stream Editor 1/2 used to perform basic transformations on text read from a file or a pipe. The

    result is sent to standard output. The syntax for the sed command has no outputfile specification, but results can be saved to a file using output redirection. Theeditor does not (but it can) modify the original input.

    Command Result

    a\ Append text below current line.

    c\ Change text in the current line with new text.

    d Delete text.

    i\ Insert text above current line.

    p Print text.

    r Read a file.

    s Search and replace text.

    w Write to a file.

    Option Effect

    -e SCRIPTAdd the commands in SCRIPT to the set of commands to be run while processingthe input.

    -fAdd the commands contained in the file SCRIPT-FILE to the set of commands tobe run while processing the input.

    -n Silent mode.

    -V Print version information and exit.

  • 8/2/2019 Bash Course

    22/23

    Sed: Stream Editor 2/2 Some examples of using sed and some basic regular expressions, it can be

    concatenated with other commands, used in for while and if statements.

  • 8/2/2019 Bash Course

    23/23

    This is itThis concludes our very basic bash terminal course, with

    this you can now proceed to the basic C++ course, afterthis you be able to fully understand and make the most

    of our ConvergeBransh offering.