Follow me---quick start shell scripting

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Try to make your work more efficient, write scripts, and use scripts, often with less effort. So, take some time today to learn about shell scripting.

--------------ahead of the high-energy warning, this article is written slightly entertain, if you have to look uncomfortable, please go to the top right corner----------------------

The shell has two ways of executing commands:

Interactive (Interactive): Explains the execution of a user's command, the user enters a command, and the shell interprets the execution of a single rule.

Batch: The user writes a shell script in advance, with a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time.

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


Because I was written with VI, just start with VI will feel this thing is people use it, difficult to die, if decided to use Vim editor, before beginning to write the shell, or silently familiar with the VI Edit command bar. You can refer to the link http://blog.csdn.net/hnulwt/article/details/43062065

Come on, start writing the shell.

Terminal Input VI test.sh

Start editing input content: (in fact, the VI editor in the input content is the most difficult!!!) )

echo "Hi, SB" Read Personecho "$PERSON, ni shi hou zi pai lai de dou bi ma?"
When you are finished typing, press ESC, then enter WQ, and press ENTER

Okay, it's already saved and exited. (If you are not proficient in the above steps, it is recommended that you change your posture ~ again and understand the role of each step)

There are several ways to run and run, and here are two kinds of things.

The first type, enter./test.sh (note!) There is a point in front, which indicates the current directory)

The second, the input sh test.sh (of course bash test.sh can also, the front said that bash for SH compatibility is very good, of course, I am not here to see knock sh convenient, just two characters. (No!) More than two words RP die. ))

Okay, look at the results. Hey? What's wrong with ...?

Linux has a permission system, we create a file is not execute permissions, because the default file permissions created by the-rw-r--r--, as for not understanding the meaning of this character, check it, online information is everywhere. So, you need to give him permission before the execution, command line input chmod +x./test.sh, you can add executable permissions. (occasionally I would simply rough chmod 777./test.sh, of course, this is not a good way ~, this and the previous said there is a difference, ha, can be through the Ls-l | grep test.sh See the difference between the two permissions, as described above in question, with the help of the search engine, you can also directly reply to questions

OK, this can run our program, typing./test.sh or sh test.sh run, isn't there a surprise ah! Actually said "Hi SB", followed by I entered the NIMA and then the characters are out. Running results such as:



Variable concept

OK, the first applet runs successfully, then look at the variable definition:

Example: Variblename= "Value"

First of all, there is no space between the variable name and the equal sign, so be careful, because we may have developed this habit when we write other programs, and we need to pay attention to this habit in the shell.

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

1, the first character must be a letter (a-z,a-z).

2, the middle cannot have the space, can use the underscore (_).

3, you cannot use punctuation.

4, you cannot use the keywords in bash (you can view the reserved keywords using the help command).

Defining variables We are going to start using variables, how to use variables, you need to add braces to the variable names that you define, and then write the $ characters on the front, for example

Who= "Dog"

echo who is ${who}?

In fact, variables can be directly used $who, but in order to develop a good habit, or add parentheses.

Then we have a look at the shell variable type, there are three variables:

1 Local VariablesLocal variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables. 2 Environment VariablesAll programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary. 3 Shell VariablesShell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

Arithmetic operations

Bash supports many operators, including arithmetic operators, relational operators, Boolean operators, string operators, and file test operators.
Native bash does not support simple math operations, but can be implemented with other commands, such as awk and expr,expr, which are most commonly used.
Expr is an expression evaluation tool that uses it to perform evaluation operations on expressions.

Then write a short shell, according to:

#/bin/bashvalue= ' Expr 2 + 2 ' echo "Total value: ${value}"
There are several points to note that the first line is the Linux comment, beginning with the # number.

Notice in the second line that there are no spaces in the assignment, and there are spaces between the time the expression is evaluated. Expr 2 + 2 expression is enclosed by ' number. Note is not a single quotation mark, but the symbol below the ESC on the keyboard. (Half-width English mode)

Run results

[Email protected]/desktop
$ sh test.sh
Total Value:4

Okay, then come with me. Enter the following:

#/bin/basha=10b=20value= ' 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 "fiif [$a! = $b]then    echo" A not equals B "fi

Together to see if the results are not the same.

$ sh test.sh
Total Value:4
A * b:200
A% b:0
A not equals B

A little note on the above:

When calculating A * b, our representation is useful to escape characters \ (backslash)

Conditional expressions are placed between square brackets and have spaces, such as [$a = = $b] is wrong and must be written as [$a = = $b]

If...then...fi is a conditional statement, which we'll look at later

Relational operations

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

If [$a-eq $b]then    echo "$a-eq $b" Else    echo "A not equals B" fiif [$a-gt $b]then    echo "$a GT $b" else
   
    echo "A not greater than B" fiif [$a-le $b]then    echo ' $a le $b "Else    echo" a not less or equal to B "fi
   

Run Result: (with previous edits entered in the content ha)

$ 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
Ten Le 20

Well, when you write a script, you can guess what-gt and-le do. Let's look at the system.

List of relational operators

Operator Description Example
-eq Detects whether two numbers are equal and returns true for equality. [$a-eq $b] returns TRUE.
-ne Detects whether two numbers are equal and returns true if they are not equal. [$a-ne $b] returns TRUE.
-gt Detects if the number on the left is greater than the right and, if so, returns True. [$a-gt $b] returns false.
-lt Detects if the number on the left is less than the right and, if so, returns True. [$a-lt $b] returns TRUE.
-ge Detects if the number on the left is large equal to the right, and returns true if it is. [$a-ge $b] returns false.
-le Detects if the left number is less than or equal to the right, and returns true if it is [$a-le $b] returns TRUE.


This time our shell scripting is here, and there are some operators, strings and so on.


Follow me---quick start shell scripting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.