Scripting
Intro.
There are a lot of programming languages, for simplicity, we will group them:
Low level language (Assembler)
It's a language that can be understood by the CPU.
Compiled Languages such as C/C++
It’s high level code, that prior execution, must be transformed (compiled) into low-level language.
Interpreted Languages such as Bash, Python, Perl, C# and a lot more.
These languages use pre-compiled “programs” that “transform” (interpret)
high-level language into low-level language in real-time (runtime).
Bash.
Look at the terminal window and say: “Hello bash!”.
Yes, your dark terminal window is handled by one of various shells: sh, bash, zsh, ash or another *sh.
The most popular of the above - bash, but it’s not guaranteed that in some linux/unix distribution there will be such.
There are two types of executable files in linux (execute permission set):
Compiled low-level executable, also known as ELF binary
Text-file with script program of the supported languages..
Script is a line-by-line execution of commands.
Almost any script looks like a text-file with instructions, while first line of the file must be similar to these:
#!/usr/bin/python3
#!/usr/bin/ruby
#!/usr/bin/perl
#!/other/interpreter/for/this/script
#! - a.k.a. shebang is a special instruction for bash-shell, instructing which program will handle the remaining lines of the file.
“Hello world” Examples in different languages:
Bash:
Python:
PHP:
Ruby:
That’s pretty simple, with one exclusion: the syntax (rules) of each language differs. But when you learn the syntax of one programming language it is quite easy to learn another language.
Let’s save one of the scripts to a file, set execute permission and finally execute it.
In Ubuntu linux there are at least two programs that are able to edit text files in terminal: nano and vi
Editing files with “nano”:
Open file: nano filename & edit the contents
Exit nano: Ctrl+X -> then Y to save the file
Editing files with “vi”:
Open file: vi filename
Edit: Press Insert key and edit the contents. When finished press Esc Key
Save and quit: Enter :wq then press Enter Key
In the following example, i have saved our first Bash script in /tmp/myscript.sh:
Let’s view its contents:
Next, we need to set executable permissions:
And finally execute it:
Cool.
Variables
In every programming language, bash is not an exclusion, we can make some dynamic programs with use of variables.
Variables are some kind of containers, that are used to store information in computer’s memory, and allow us to access that information by some label.
Look at the code below:
This bash script will print out variable contents
Assignment direction is from the right to the left
Value: Hello World! is being assigned to variable name: var1
There are no spaces between = (equal sign) and variable name and value
Variable is accessible by prepending $ character to the variable name
We can reassign a variable:
After this manipulation, $var1 will hold the string: Other Value.
Script Arguments
It is possible to pass some arguments to the script:
You might have noticed that argument3 was not printed, and we've got only $3. That's because we have used a single quote in an echo statement.
In bash, we use single quotes in cases when we want to store data literally.
Assigning command output to a variable
Bash has a very cool feature of inline command execution and it's output could be used to be assigned to the variable.
One example explains it better than any description:
We can use multiple ways of achieving this, but with slightly different behavior:
Try to understand differences between the following:
Research the subject on your own. Google bash command substitution.
Loops
Loops are used when it is needed to perform identical actions multiple times on different values:
Let's execute file command on each reated fileX:
Another example with file input line-by-line, which counts line length in /etc/passwd file:
If you place these loops in script, consider to make it look pretty:
Conditional flow control (branching)
Every programming language has the ability to control the execution flow depending on some IF/ELSE conditions.
Follow script will check if we execute the script with user: "user":
Multiple conditions with elif:
Functions
Resume
This chapter shows you basic principles of scripting, and with some time, experience, trial and error - i hope you will be comfortable with bash.
It's up to you to master bash scripting. You may get your coding to a very high level and construct very complex algorithms.
Go and solve all challenges at https://cmdchallenge.com
No comments:
Post a Comment