Академический Документы
Профессиональный Документы
Культура Документы
Director of technology
Converge Engineering
Bash
An integrated introduction to: Linux/Unix terminal bash programming.
BE AWARE !
If you are reading this presentation from our OpenSource initiative be aware that are much more capabilities verbally talked in the course. For more details about the commands try: $man command Example $man echo This course is not an comprehensive C++ and bash training. This is the preparatory course to our ConvergeBranch training course.
Drop-in replacement for Bourn shell. One of the first top priority projects of the Free Software Foundation. Bash is a POSIX shell! POSIX "Portable Operating System Interface for Unix is the name of a family of standards specified by the IEEE to define the application programming interface (API), along with shell and utilities interfaces. Initial release in June 1989, last release February 2011, talk about up to date! Bash gives acess to the fundamental unix programs, here we will just teach what we feel that are the best tricks and most useful stuff
!$ Repeating a command
The argument !$ gives you the last used argument. In the example, we create the folder Programming with the command mkdir then we use the command cd to enter the folder Programming and then use the double dot .. to reference to the up level folder.Using the command && to chain two commands in one. using the !$ macro we repeat the last argument, Programming, so we exit and enter the folder. You can check the folder you are with pwd Try to use the shortcut: Esc + . Try now to do it rapidly.
top shows information about the running processes in the system and metrics about the system usage. Use Ctrl+z to interrupt an process and the bg command to put it in background. Use the command fg to bring the last background process to the foreground. Use the command jobs 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
echo prints to stdout the argument it receives, in this example we use it to print a string cat prints to stdout the content of the argument it receives, in this example we print the content of the file this_course that is at the Programming folder As you all can see ls list the content of a folder ;-) vWait ! stdwhat ?!?!
Standard streams
Standard streams are Unix concepts, they are channels where data can be transported, there are three: stdout: The standard output is a channel to be used by a program to 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 program needs to inform that an error has occur it will print to stderr instead of stdout. Stderr also output to the terminal.
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 <(command argument): These strings indicate that bash should substitute the command between the tag <() as a process. This will create a named pipe (usually in /dev/fd). The input substitution|: Pipe is one of the most powerful tools of bash. The "|" operator makes bash redirect all data that the command left of it returns to the stdout to the stdin of the command right of it. Thus it pipe the data through commands. The grep command search the stdin it receives for an regular expression, it then print to stdout the line in the stdin that match the expression. With the l option it print the origin of the match, normally a file, in our case it is stdin.
The find command is another powerful Unix program, it can be used together with grep, cp, sed and rename 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
If conditional
The if conditional is used as usual, in the syntax: if <condition>; then <commands if true> else <commands if false> fi But there are super dupper shell stuff in it, for working with files: http://www.linuxtutorialblog.com/post/tutorial-conditions-in-bash-scripting-ifstatements If conditional can also be replaced most times for the test command and the markers && for if true and || if false. PS: -a for and, -o for or
While loop
While loop in the terminal, it do test like if conditional, as a loop can also use multiline output from stream processing programs while <true> do <commands> done An example of using a stream with while loop: cut -c-5 /tmp/testez/comandos.txt | \ while read LINE do echo $LINHA done
For loop
This loop allows for specification of a list of values. A list of commands is executed for each value in the list. for NAME [in LIST ]; do COMMANDS; done Think about multiple nested for and while loops with if conditionals, all operating in multiple streams.
This is it
This concludes our very basic bash terminal course, with this you can now proceed to the basic C++ course, after this you be able to fully understand and make the most of our ConvergeBransh offering.