Bash Scripting
Learning the bash Shell
By John Bach
Table of contents
Introduction
Is a program that works to receive commands from you and then ask the kernel to execute these commands. Ie acts as an intermediary between you and the operating system or in other words it represents the outer layer or interface of the system, it takes orders from you and gives you the result. Then it shows the inductor again, indicating termination, and waiting for you to enter more commands. This is why the shell is called a command interpreter, which is similar to command.com in windows. It also contains a programming language similar to high-level languages is very strong through which you can solve any problem you face. Also, one of the important features is the possibility of making a file containing a set of programs to create another program and this enables you to produce what is known as script, and there are many types of shell, but the most famous and used are the two main types:
The Bourne shell includes the sh, ksh, bash
The C shell includes csh, tcsh
If you are using a Bourne shell, the default inductor for you as a user is $.
What is shell propmt?
The inductor, whether it is% or $, depends on the type of shell you are using.When the prompt appears, it means that the shell is waiting for you to type the commands to execute.If you have entered the system as the root of any system administrator, the prompt will appear. The following are the advantages and disadvantages of each type of shell quickly:
The first type includes:
The Bourne shell and symbolized by sh The Korn shell and symbolized by ksh The Bourne againshell and symbolized by the bash
The second type includes:
C shell TENEX / TOPS C shell TCSH
The beginning of the shell was with the Bourne shell where stephen R.Bourne wrote it on the unix system in the mid 1970's at the AT&T lab. Therefore, some call it the shell, "The Shell," because it first appeared. This shell is usually located in the path / bin / sh As mentioned earlier, the shell is a command interpreter and also contains a programming language using syntax formula ALGOL language, and contains the following things:
Variables, functions, program control commands, repeating commands, processing commands, mathematical expressions, etc.
All the Bourne-type types share these features, but the disadvantage of this original type is the difficulty in using it. There is no auto-complete feature, and there is no save command that has been executed.
Ten years later, in 1980, Bill Joy wrote the C shell, avoiding the difficulty found in the previous type and also making the syntax formula from ALGOL to C language.
Some of the features found in it:
The property of completing the text, save previously written commands. Usually we find this shell inside the / bin / csh path
Its disadvantages are: Weakness in supporting I / O operations lacks functions, so it is not recommended for programming, only for use.
The TENEX / TOPS C shell, symbolized by tcsh, is an improved version of the cshell where additions of many features, including the use of the arrows (up - down) to view the commands that have been executed as well as the arrow (right - left) to modify these commands. At this time, after two types of shell were available, David Korn in AT&T wrote the Korn shell and symbolized by ksh
It combines the ease of the C shell, as well as the Bourne shell, making ksh the preferred choice for many users.
In general, the ksh is compatible with the first type, which is the sh. There are 3 copies of this paralysis:
The official symbolized by ksh which we talked about and the public domain and symbolized by pdksh and finally the version of Desktop and symbolized by dtksh. Shell programmers often use the first and second versions. The third dtksh is for Shell users. The ksh is usually found in the / bin / ksh or / usr / bin / ksh paths.Other versions can be downloaded from the net if it does not come with your system.
Finally, the Bourne again shell, which is currently used in most distributions where the developer Brian fox collected the previous features and put it in it so it is now preferred and is usually found in the path / bin / bash or path / bin / sh where it became the default shell.
When does paralysis begin and end?
When you log on to the system, getty starts to work where you are asked to type your username and passwords and then passes it to the login program that checks the entered data, by comparing it with / etc / passwd and it looks like this:
SudaNix: x: 500: 500: ahmad: / home / SudaNix: / bin / bash
In the case of matching, the paralysis in the file will be executed, in this case is / bin / bash and when you exit logging Shell ends work and to change the type of paralysis change it by typing the command: / bin / csh Here changed to Cshell and exit
Interactive and non-interactive paralysis !!
When you see the prompt prompt, this means that immobilization works interactively interactive, I mean interactively, that is, you are expected to write commands to execute This is the usual normal situation where you enter the system, then the immobilization begins to work .. Then you write some commands, and exit the system Shell is suspended.
It is contradicted by non-interactive paralysis:
It means that there is no interaction between you and Shell, that is, you will not be waiting for you to write any commands, where he executes commands saved inside a file and when it reaches the end of the file stops working and this is known as the script. Two basic concepts are:
Commands
It is a program that you can run by typing its name and then press enter. Example: The date command shows the day, the date, the hour. Note that the $ prompt is reappeared and this indicates that the program has finished executing.
Another example is the command, which shows the names of all users of this system in addition to some information and there are several types of commands, namely, simple commands, complex, complex.
Simple commands
It is clear from its name that it is simple, which contains only the name of the program, such as the two previous date, who as well as many of the commands that I will address later.
Complex commands
A simple program name plus one or more arguments. An example to make it clear:
Think of a script for a play, or a movie, or a TV show. The script tells the actors what they should say and do. A script for a computer tells the computer what it should do or say. In the context of Bash scripts we are telling the Bash shell what it should do.
A Bash script is a plain text file which contains a series of commands. These commands are a mixture of commands we would normally type ouselves on the command line (such as ls or cp for example) and commands we could type on the command line but generally wouldn't (you'll discover these over the next few pages). An important point to remember though is:
Anything you can run normally on the command line can be put into a script and it will do exactly the same thing. Similarly, anything you can put into a script can also be run normally on the command line and it will do exactly the same thing.
You don't need to change anything. Just type the commands as you would normally and they will behave as they would normally. It's just that instead of typing them at the command line we are now entering them into a plain text file. In this sense, if you know how to do stuff at the command line then you already know a fair bit in terms of Bash scripting.
It is convention to give files that are Bash scripts an extension of .sh (myscript.sh for example). As you would be aware (and if you're not maybe you should consider reviewing our Linux Tutorial), Linux is an extensionless system so a script doesn't necessarily have to have this characteristic in order to work.