Learn about Unix / Linux P2 – Shell Script

Tram Ho

Define

Shell is the program that communicates with the user, meaning that the shell receives the command we enter from the keyboard and executes it. The shell script will be a series of commands written in plain text files. Shell script is like batch file in MS-DOS but more powerful.

So why should we write shell script?

  • You can input input from user, file or output from the screen
  • Convenient to create your own group of commands
  • Save time
  • Automatically execute a few jobs regularly

Simple shell script creation

First, shell script files are often chased as .sh . Now we will create a script.sh file. Before adding any script to the shell script file, you will need to declare that a shell script is about to be executed. This is done with a shebang structure, for example:

For example in my script.sh file:

To run the shell script, you use the command

And the result will be:

Variables in shell scripts

Shell script variable names will also have rules that are quite similar to those in programming languages. The name of a variable can only contain the characters a to z or A to Z, numbers 0 – 9, a trailing half _ . By convention, variables will be capitalized. And remember, don’t leave any spaces when you declare variables (VAR = 1 – this won’t work)

Use variables

Now let’s take a simple example

Result

The shell will not care about your variable type, you will not have to declare the type for the variable, in fact, there will be no difference with the following variables:

Result:

Read only

When a variable is set to read only, you will not be able to change its value

Result:

Delete variables

To delete a variable, we use unset , and we also cannot unset a read only variable

Result

Scope of the variable

The variable inside the shell script doesn’t have to be defined, if you are trying to read an undefined variable it will just return an empty string.

Result:

Now we will try to declare outside the shell script file

When you run the shell script file, a new shell is generated. Hence we need to export the variable to be able to interact between different shells

When the shell script is finished running, its environment will be destroyed, ie in the shell script file you assigned

But when the shell script file finishes running, I echo $MESSAGE and it will still output Helloooo . To be able to get the change from the shell script you need to run the following command

Special variables

The shell script also has special characters that you should not use

Outcontent
$ 0The filename of the current script
$ nThis variable corresponds to the number of parameters passed when the shell script executes (The first parameter will be $ 1, the second parameter will be $ 2).
$$The ID of the running process
$ #Number of parameters provided for a script
$ *All parameters are double quoted. If a script takes two parameters, $ * is equivalent to $ 1 $ 2.
[email protected]All parameters are individually quoted. If a script takes two parameters, $ * is equivalent to $ 1 $ 2.
$?The exit status of the previous script
$!The ID of the previous background command process.
$ _The last parameter of the previous command

Try running this script file

Result

You might be wondering: “What is the difference between $* and [email protected] ?”. For Tet, $* is a string and [email protected] is an array. Try the following example

Result:

Today’s article stops here only. See you all next time.

Refer

Share the news now

Source : Viblo