Quantcast
Viewing all articles
Browse latest Browse all 10

Generalities about shell programming

Interpreter commands of the Unix operating system provides, besides the possibility of execution of orders, a set of instructions that allows writing programs like those written in high level programming languages. Naturally, the possibilities of this language are much weaker than those of a language like C or Pascal, but there are applications where programming effort is much reduced. Besides commands “regular”, which appear in any operating system, Unix and provides plenty of tools, especially text files. Shell language is powerful and effective:
• Applications whose resolution involves several transactions in the shell commands;
• Applications that manipulate data as text files or lines of text;
• Applications that require consultation browse directories and file system;

Last but not least, when we do regular operations involving the use of shell commands, we can automate work by creating a batch file.
Batch file will call any sequence of commands stored in a disk file. The program will understand Shell script file that contains, besides orders execution control structures (decision and repetitive instructions) and variable. This chapter presents the most important concepts in programming scripts in Bourne Again Shell (BASH), the most widespread in Linux. To schedule under another shell, appropriate documentation should be consulted to see what the differences are.
Objects that compose a script are: Image may be NSFW.
Clik here to view.

• Linux Commands;
• Variables:
1. User-defined variables;
2. Predefined variables (system);
3. Particular variable;
• Instructions for read / write:
1. read to read data;
2. echo to display data and messages;
• Instructions for Decision:
1. simple if-statement of decision;
2. Multiple decision-home instruction;
3. select-statement to create simple menus;
Iterative Instructions:
1. forum
2. while
3. Until
• Instructions jump:
1. Forced break out of a cycle
2. continuous-forces continued for the next cycle counter value
As you noticed, there is an unconditional jump instruction (goto), thus gaining programs readability. Placing instructions allow repetitive writing structured programs, unlike the “language” of the operating system MSDOS batch.
A script can receive command line arguments. Also, you can call from within a script, other scripts.

Image may be NSFW.
Clik here to view.

Scripts can be written using a text editor like vi, ed, emacs. Then it sets the right execution of the file name can be used as a regular command. The shell command will execute the file command.
An example of a simple batch file is:
pwd
ls-l
finger

11


Viewing all articles
Browse latest Browse all 10

Trending Articles