In this new opportunity (Entry # 8) about "Learn Shell Scripting" we will focus more on theory than practice. that is, we will not install or study a code or install a specific software (package), but rather we will delve into what is the world of Shell scripting properly speaking, through the answers to small but direct questions, which are shown below, to clarify so far much of what has been taught, which does not refer directly to the internal code that is programmed:
What is the Shell in a GNU / Linux Operating System?
Shell which in Spanish means CONCHA (shell, cover, protection). Applied this term in Operating Systems refers to the command interpreter of the Operating System. In general, it is a high-performance text interface, manifested in the form of a Terminal (Console) and essentially used for 3 important work areas:
1.- Manage the OS,
2.- Run applications and interact with them, and
3.- Serve as a basic programming environment.
Many SO, GNU / Linux they are still more efficiently managed by editing their configuration files, via Terminal. As a general rule, these are on the destination path: «/etc", and within specific directories for each application. For example, the program Lilo (which stands for Linux Loader) is configured by editing the text file located and called as "/Etc/lilo/lilo.conf". In the case of programs (applications), these are launched (executed / activated) by writing the name of the executable, if it is found in the path (default path) for all executables, as it normally is "/ Usr / bin" , or by typing the name of the executable preceded by: ./, from the directory where they are located.
All of this is well known to any Shell user. However, not so well known and appreciated are its capabilities as a programming environment. Scripts (programs) made in the Shell do not need to be compiled. The Shell interprets them line by line. Therefore, these are known or named as Shells Scripts, and can range from simple commands to complex series of instructions for starting the OS itself. In general, have a fairly clean (obvious) syntax (construction, ordering), making them a good starting point to get started in the world of programming.
What is Shell Scripting?
It is the technique (skill / dexterity) of designing and creating Script (task automation file) using a Shell (preferably) of an Operating System, or a Text Editor (Graphic or Terminal). This is a type of programming language that is generally interpreted. That is, while most programs are compiled (coded), because they are permanently converted to a specific (special) code before they can be executed (compilation process), a shell script remains in its original form (its code text source) and are interpreted command by command each time they are executed. Although it is possible that the scripts can be compiled as well, although it is not usual.
What are the characteristics of programs based on programming under Shell Scripting?
1.- They are easier to write (program), but with a higher processing cost when they are executed.
2.- They use interpreters instead of compilers to run
3.- They have a communication relationship with components written in other programming languages.
4.- The files that contain them are stored as plain text.
5.- The final design (code) is usually smaller than what would be the equivalent in a compiled programming language.
What are the most popular types of languages under Shell Scripting?
1.- Task and shell control language:
a) cmd.exe (Windows NT, Windows CE, OS / 2),
b) COMMAND.COM (DOS, Windows 9x),
c) csh, Bash, AppleScript, sh,
d) JScript via Windows Script Host,
e) VBScript via Windows Script Host,
f) REXX, among many others.
2.- GUI Scripting (Macros Languages):
a) AutoHotkey,
b) AutoIt,
c) Expect,
d) Automator, among others.
3.- Scripting language of specific applications:
a) ActionScript in Flash,
b) MATLAB,
c) mIRC script,
d) QuakeC, among others.
4.- Web programming (for dynamic pages):
a) On the server side:
- PHP,
- ASP (Active Server Pages),
- JavaServer Pages,
- ColdFusion,
- IPTSCRAE,
- Lasso,
- MIVA Script,
- SMX,
- XSLT, among others.
b) On the client side:
- JavaScript,
- JScript,
- VBScript,
- Tcl, among others.
5.- Word processing languages:
- AWK,
- Perl,
- Thirst,
- XSLT,
- Bash, among others.
6.- General purpose dynamic languages:
- APL,
- Boo,
- Dylan,
- Ferite,
- Groovy,
- IO,
- Lisp,
- Lua,
- MUMPS (M),
- NewLISP,
- Nuva,
- Perl,
- PHP,
- Python,
- Ruby,
- Scheme,
- Smalltalk,
- SuperCard,
- Tcl,
- Revolution, among others.
What is Bash in GNU / Linux?
It is a computer program whose function is to interpret orders. It is based on the Unix shell and is compatible with POSIX. It was written for the GNU project and is the default shell for most Linux distributions.
What is a Shell Script in GNU / Linux?
The Shell Scripts they are extremely useful. It is a good idea to write those needs that we have and then edit scripts that do this work for us. By now, it's time to ask what exactly a script is. It is a text file, containing a series of shell commands, which the system executes in an orderly fashion, from top to bottom. To edit them, you only need a text editor, such as Emacs, Vi, Nano, among many existing ones. They are saved with the extension “.sh” (or without it, in some cases) and are run from the Shell using the command: sh script name.sh. Scripts behave in the same way as shell commands.
The teaching approach that I personally use to "Learn Shell Scripting" It is very practical and direct, that is, examining a fully functional Script, decomposing it, studying it sentence by sentence, line by line, command by command, variable by variable, until you understand how each element works separately and how it engages in the code general. It's kind of Reverse Engineering or Software Reengineering. All this in order to appropriate the knowledge, improve it (optimize it) and share it, for the collective benefit and a better administration and optimization of the free Operating Systems.
How does it run and work in a GNU / Linux Shell?
The first step in working with a Shell is to run a shell. What seems like a truism has its reason for being. In some very end-user oriented GNU / Linux distributions, the shell is quite hidden. Typically, it's called: Konsole, Terminal, X Terminal, or something similar. Another option is to use a virtual console. Using: Ctrl + Alt + f1, or f2, or f3 to f7 or f8, depending on the GNU / Linux distribution you use. The most used Shell in GNU / Linux is Bash, although there are others, such as ksh or C Shell. In my case, very particular for my publications I use Bash Shell.
Given a Script made in Bash Shell called hello_world.sh the following can be explained:
Content:
#! / Bin / bash
echo hello world
Breakdown:
First line of the script
#! / Bin / bash
Indicates the program that the script should run. If the program cannot be found, an error will occur.
Second line of the script
echo hello world
Execute the echo command with the Hello World arguments, causing them to be displayed on the screen.
Execution: We can run the script in two ways
Invoking the interpreter to run the script:
# bash hello_world.sh
It can also be run as:
# sh hello_world.sh
But since your correct Shell is not invoked, it may work half. Ideally, the Shell invoked in the first line is the one used to execute it.
You can also run the script directly as follows:
# ./hello_world.sh
Nota: ./ indicates run from current directory.
The rest that remains to be analyzed is the code that you insert into it. I hope that as always you are liking (some more than others, according to learning and knowledge needs) this series of Shell scripting.
There are many good links on this topic on the web, but I leave you this little guide located right here at FromLinux.net And this other External Guide.
Until the next post!
