SHELL basic syntax 2

Source: Internet
Author: User
Tags echo command
Shell syntax
Variable:
In Shell, we do not need to declare variables before using them. on the contrary, we can simply use it as needed. by default, all variables are stored as strings, although sometimes we use numbers to assign values to them. shell and some other practical conversion programs will convert the numeric string to the corresponding value for the operation. in Linux, the variables foo AND Foo are different.
In Shell, we want to access the variable value. We need to add a $. when we want to assign values to variables, we can only use the variable name, and Shell will dynamically create as needed. an easy way to detect the content of variables is to output the content on the terminal. In this case, add $.

In the command line, we can use the following method to set and detect the value of the variable:
$ Salutation = Hello
$ Echo $ salutation
Hello
$ Salutation = "Yes Dear"
$ Echo $ salutation
Yes Dear
$ Salutation = 7 + 5
$ Echo $ salutation
7 + 5
We can also use the read command to assign user input values to variables. in this way, the variable name will be used as a parameter and will wait for user input. the read command ends when you press Enter. when reading variables from the terminal, we do not need to use quotation marks. example:
$ Read salutation
Wie geht's?
$ Echo $ salutation
Wie geht's?

Quotation marks:
Before continuing our learning, we need to understand the function of quotation marks.
Usually, parameters in the script are separated by blank characters, such as spaces, tabs, and carriage returns. If we want our parameters to contain one or more parameters, we need to use quotation marks.
For example, the behavior of the variable $ foo depends on the type of quotation marks we use. if we use double quotation marks, this line will be replaced with its value, and this will not happen if we use single quotation marks. we can also use the Escape Character \ to remove the special meaning of $.
In general, double quotation marks are used to include strings. This prevents variables from being separated by blank characters and replaces them with variable values.
In the following example, we will see the effect of quotation marks on variable output:
#! /Bin/bash
Myvar = "Hi there"
Echo $ myvar
Echo "$ myvar"
Echo '$ myvar'
Echo \ $ myvar
Echo Enter some text
Read myvar
Echo '$ myvar' now equals $ myvar
Exit 0

The output of this program is:
Hi there
Hi there
$ Myvar
$ Myvar
Enter some text
Hello World
$ Myvar now equals Hello World

Working Principle
We created the variable myvar and assigned it to Hi there. the variable content is displayed by the command echo, so that we can see the influence of the $ character extension on the variable content. from this output, we can see that double quotation marks do not affect the replacement of variables, but single quotation marks and backlash do. we also use a read command to get input from the user.

Environment Variable
When a Shell script is started, some variables are initialized by the environment values. in scripts, these variables are usually uppercase letters, which are differentiated from User-Defined variables. User-Defined variables are usually represented by lowercase letters. the created variables depend on our personal configurations. many of these columns are on the workbook page, but some of the basic columns are in the following table:

$ HOME: HOME Directory of the current user

$ PATH: List of directories separated by colons used for command search

$ PS1 command prompt, usually $, but we can use more complex values in bash. for example, string [\ u @ \ h \ W] $ is a popular default usage to tell us the current user, machine name, and current working directory, and give a $ prompt.

$ PS2 second prompt, used when an additional input is prompted, usually>

$ IFS input delimiter. When the Shell reads the input, a character list is used to separate the input words, which are usually space, tab, and new line characters.

$0 Shell script name.

$ # Number of parameters passed.

$ Script process ID, which is usually used inside a script to create a unique temporary file, such as/tmp-file _ $.
If our script calls some parameters, other variables will be created. Even if no parameters are passed, the environment variable $ # still exists, but the value is 0.

Parameter variables are listed in the following table:
$1, $2,... parameters passed to the script.
$ * All parameter lists are displayed as single variables, which are separated by the first character in the environment variable IFS.
$ @ $ * Is a smart deformation. It does not use IFS environment variables, so if IFS is empty, all parameters will run together.

The following test shows the differences between $ @ and $:
$ IFS =''
$ Set foo bar bam
$ Echo "$ @"
Foo bar bam
$ Echo "$ *"
Foobarbam
$ Unset IFS
$ Echo "$ *"
Foo bar bam
As we can see, in double quotes, $ @ separates the parameters and is irrelevant to the IFS value. in general, if we want to access the parameter, $ @ is a very sensitive choice.
We can not only use the echo command to print the variable content, but also use the read command to read their content.

Parameters and Environment Variables
The following script shows how to process simple variables. After we enter the following script content and save it as try_var, we must remember to use the command chmod + x try_var to add executable permissions to it.
#! /Bin/sh
Salutation = "Hello"
Echo $ salutation
Echo "The program $0 is now running"
Echo "The second parameter was $2"
Echo "The first parameter was $1"
Echo "The parameter list was $ *"
Echo "The user's home directory is $ HOME"
Echo "Please enter a new greeting"
Read salutation
Echo $ salutation
Echo "The script is now complete"
Exit 0

If we run this script, we will get the following output:
~ $./Try_var.sh foo bar baz
Hello
The program./try_var.sh is now running
The second parameter was bar
The first parameter list was foo bar baz
The user's home directory is/home/mylxiaoyi
Please enter a new greeting
Hello
The script is now complete

Working principle:
This script creates a variable named salutation and displays its inner null, and various parameter variables. The environment variable $ HOME already exists and has an appropriate value. function:
The basic principle of all programming languages is to test the conditions and perform various operations based on these tests. before discussing this topic, let's take a look at the function structures we will use in Shell scripts and the control structures we will use.
A Shell script can be used to test the return code of any command called by the command line, including the script we have written. this is why the exit code is included at the end of each Shell script. test or [command:
In fact, most scripts use a large number of test or [commands for Shell true/false detection. in most systems, [it is synonymous with the test command, but when a [command is used at the same time, a] command is used at the end for readability. using the [command seems a little strange, but this command in the Code will make the command syntax look simple, neat, and very similar to other programming languages.

Ls-l/usr/bin /[
-Rwxr-xr-x 1 root 25040/usr/bin /[

We will use a simple test example to introduce the test command: Check whether a file exists. The command for this purpose is test-f <filename>, so we can use the following script:

If test-f fred. c
Then
...
Fi

We can also write as follows:

If [-f fred. c]
Then
...
Fi

The return code of the test command (whether the condition is met) depends on whether the condition code is run.

Here we must note that we must separate [and conditions with spaces. we can use the following method to remember this: [it is another way to write the test command, and we need to enter a space after the test command.

If we like to put then and if in the same row, we must add a colon to separate then:

If [-f fred. c]; then
...
Fi

The test command can be used for the following condition types: String comparison, arithmetic comparison, and file condition. The following three tables show these condition types:

String comparison:
String1 = string2 true if equal
String1! = String2: true if not equal
-N string is true if it is not null
-Z string: True arithmetic comparison if it is null:
Expression1-eq expression2 true if equal
Expression1-ne expression2 is true if not equal
Expression1-gt expression2 is true if it is greater
Expression1-ge expression2 is true if it is greater than or equal
Expression1-lt expression2 is true if it is smaller
Expression1-le expression2 is true if it is less than or equal
! If expression is false, it is a true file:
-D file is true if it is a directory
-E file is true if it exists. (Note that the-e option is not portable due to historical reasons. Therefore, the-f option is commonly used.
-F file: true if it is a regular file
-G file: true if the group ID is set
-R file: true if the file is readable.
-S file: true if the file size is not zero
-U file is true if the user ID is set
-W file: true if the file is writable
-X file: true if the file is executable.
Now we seem to have gone a little too far, but the following is an example. here we want to test the file/usr/bash, so that we can clearly see the usage of these conditions :#! /Bin/sh
If [-f/bin/bash]
Then
Echo "file/bin/bash exists"
Fi
If [-d/bin/bash]
Then
Echo "/bin/bash is a directory"
Else
Echo "/bin/bash is NOT a directory"
Fi

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.