Thoughts of Writing shell scripts

There are two ways to execute shell commands:

Interactive (Interactive): interpreted user command, the user enters a command, Shell will execute an explanation.

Batch (Batch): User Shell advance to write a script (Script), of which there are many commands, so once these Shell command executed, one by one without having to type commands.

We want to know bash and sh is not a thing, we need to understand, bash is fully compatible with sh, that is to say, to write the script sh can be executed without modification in bash.

 

Come on, I began to write shell

Vi test.sh input terminal

Start editing input about the content :( actually typing in the vi editor, which is the most difficult !!!)

After echo "Hi, SB" read PERSONecho "$ PERSON, ni shi hou zi pai lai de dou bi ma?" Is entered, press Esc, then type wq, press enter well have saved and exited. (If the above steps are less experienced, I suggest you change posture - again and understand the role of each step)

Then run, run, then there are several ways, here two kinds of bars

A first input ./test.sh (Caution! Front of a point, in the current directory)

Second, enter sh test.sh (of course bash test.sh also be all right, said earlier bash to sh compatible is good, of course, I'm not here to knock easy to see sh Well, you two characters. (Nima! knock two characters could die ..))

Well, look at the results. Huh? How wrong. . .

Linux has a permissions system, so we created the file is not authorized to perform, because the file is created by default permissions -rw-r - r--, as to understand the meaning of a character, you can hold that thought, we It will be mentioned later. Before performing the need to give him added authority, command line, type chmod + x ./test.sh, you can, which increases the executable permissions. (Occasionally I will simply rude chmod 777 ./test.sh, of course, this is not a good way ~, and said earlier that there is a difference of Kazakhstan, by ls -l | grep test.sh see both a privilege conferred difference), as described above, have questions, you can reply to ask questions directly

Well, the instrument allows to run our program, and typing ./test.sh or sh test.sh run, is not a surprise ah! Actually say "Hi SB", followed by nima then I entered the back of the character will come out.

 

Variable concept

Well, the first little run successfully, and then look at variable definitions:

Examples: varibleName = "value"

First thing, with no spaces between the variable name and the equal sign, this point should be careful, because we usually write other programs that may have been the time to develop this habit, the shell should be noted here that habit,

Then you want to know some of the variable naming rules:

1, the first character must be a letter (az, AZ).

2, without spaces, you can use underscores (_).

3, can not use punctuation.

4, can not be used in bash keyword (help command to view the available reserved keywords).

Defined the variables we will begin to use variables, and how to use variables, you need to add braces on their own definition of the variable name, then write on top of the $ character, for example,

who="dog"

echo who is ${who}?

In fact, you can use variable $ who take direct, but in order to develop a good habit, or it parentheses.

Then we make a shell variable type of understanding, there are three variables:

1 local variables

Local variables defined in the script or command, only valid in the current shell instance, start the other shell program can not access local variables. 2 Environment Variables

All procedures, including the shell is started, can access environment variables, some programs require environment variables to ensure their normal operation. When necessary, the shell script can also define environment variables. 3 shell variables

shell variables are special variables set by the shell program. shell variable environment variable part is, in part, local variables, to ensure the normal operation of the shell

 

shell in some special variables:

Special variable $ 0 meaning the current script filename arguments passed to the script or function. Is a number that represents the first of several parameters. For example, the first parameter is 1, the second parameter is $ 2. $ # Number of arguments passed to the script or function. $ * All parameters passed to the script or function. All parameters passed to the script or function. When in double quotes, with * are slightly different, the following will be mentioned. $? Exit status of the previous command, or the return value of the function. Shell $$ current process ID. For Shell scripts, is the process ID of these scripts are located. These variables we back up here, it can be useful to the investigation.

Arithmetic

Bash supports a number of operators, including arithmetic operators, relational operators, Boolean operators, string operators and file test operators. Native bash does not support simple mathematical operations, but can be achieved through other commands, such as awk and expr, expr most commonly used. expr is an expression calculation tool, use it to complete the evaluation of an expression operation.

Then write a short shell, according to the following diagram:

/bin/bash

= value expr 2 + 2echo "the Total value: $ {value}" FIG There are several points to note, the first line is a comment linux, beginning with a # sign. Note that there is no place assigned space in the second row, and between the calculation expression when there is a space. expr 2 + 2 'expression is enclosed in quotes. Note · number is not a single quote, but following symbols on the keyboard Esc. (English one byte mode)

operation result

Lee@Lee-PC /Desktop$ sh test.shTotal value :4

Well, then enter the following with me:

/bin/bash

a=10
​
b=20
​
value=expr 2 + 2
​
echo "Total value :${value}"
​
value=expr $a \* $b
​
echo "a * b : ${value}"
​
value=expr $b % $a
​
echo "a % b : ${value}"
​
if [ a == b ]
​
then
​
    echo "a equals b"
​
fi
​
if [ a != b ]
​
then
​
    echo "a not equals b"
​
fi

Take a look at operating results are not the same.

$ sh test.sh
​
Total value :4
​
a * b : 200
​
a % b : 0
​
a not equals b

To slightly above explanation:

In the calculation of a * b, we represent a useful escape character to \ (backslash) 

The conditional expression to be placed between square brackets, and include a space, for example, [b] is wrong and must be written [b]

if ... then ... fi is a conditional statement, later we'll find out about it again

Relational Operators

Then the script above, we continue to edit the input:

if [ a -eq b ]
​
then
​
    echo "a -eq b"
​
else
​
    echo "a not equals b "
​
fi
​
if [ a -gt b ]
​
then
​
    echo "a gt b"
​
else
​
    echo "a not greater than b"
​
fi
​
if [ a -le b ]
​
then
​
    echo "a le b"
​
else
​
    echo "a not less or equal to b"
​
fi
​

:( operating results have those previously edited entered Kazakhstan)

$ sh test.sh
​
Total value :4
​
a * b : 200
​
a % b : 0
​
a not equals b
​
a not equals b
​
a not greater than b
​
10 le 20
​

Well, when you can write a script and -le -gt guess the role of it. We take a look at the system

Relational Operators List

Operators Explanation Explanation
-eq Detecting whether the two numbers are equal, it returns true equal. [B] return true.
-born Detecting whether the two numbers are equal, not equal Returns true. [B] return true.
-gt Whether the number of detections is greater than the right to the left, and if so, it returns true. [B] returns false.
-lt Whether the number of detections is greater than the right to the left, and if so, it returns true. [B] return true.
-give Whether the number of detection equal to the right of the left side of the large, and if so, it returns true. [B] returns false.
-the Detecting whether the number is less than or equal to the right of the left, if it is, return true [B] return true.

 

Guess you like

Origin blog.csdn.net/wayners/article/details/94732835