Introduction to shell and introduction to shell Models

Source: Internet
Author: User

Introduction to shell and introduction to shell Models

1) Description
The shell file suffix is sh, which can be understood as writing multiple commands to one file. As long as the shell file is executed, many associated operations can be performed, avoid tedious execution of a single command.
In the shell file, # is required at first #! /Bin/bash to indicate that this is a shell file.

2) execute shell
Chmod + x *. sh
./*. Sh

3) variable usage
Define Variables
Variable name = variable value. There cannot be spaces on both sides of the equal sign. The variable name can only contain letters, numbers, and underscores. It cannot start with a number and contains special characters, you need to add single quotes (''). If the value contains single quotes, you need to add double quotes (" "). If the value is a command, you need to enclose it with backquotes ('').
Name = 'testname ';

Use Variables
$ Variable name or $ {variable name}
$ Name $ {name}

Set variable read-only
Name = 'testname ';
Readonly name;

Delete a variable. Read-Only variables cannot be deleted.
Name = 'testname ';
Unset name;

4) use of symbols
$ Id of the Current shell, which can be viewed by echo $
$0 script file name
$1 ~ $ N refers to the number of parameters passed to a script or function.
$ # Number of parameters passed to a script or function
$ * Parameters passed to the script or function
$ @ Parameters passed to the script or function
$? The result returned after the previous command is executed, or the return value of the function, 0 Success 1 Failure
$ Id of the Current shell

5) after adding-e, the escape character is executed. After adding-E, the escape character is not executed.
Echo 'this a file \ n'; the result is: this a file \ n
Echo-e 'this a file \ n'; the result is: this a file
\ N line feed
\ R press ENTER
\ T horizontal Tab
\ B Return
\ F form feed
\ V vertical Tab
\ Backslash
\ A alert

6) replace variables
Change the value of a variable based on its status.

Original value of $ {var} variable
If the $ {var: + value} variable is defined, return value without affecting the value of var.

$ {Var: = value} is null or has been deleted. value is returned and var = value
$ {Var:-value} is null or has been deleted. value is returned without affecting the value of var.

$ {Var :? If the value} variable is null or has been deleted, the system returns the value and outputs an error to determine whether the variable is defined.

Example:

#! /Bin/bash

Echo $ {var:-"Variable is not set "}
Echo "1-Value of var is $ {var }"

Echo $ {var: = "Variable is not set "}
Echo "2-Value of var is $ {var }"

Unset var
Echo $ {var: + "This is default value "}
Echo "3-Value of var is $ var"

Var = "Prefix"
Echo $ {var: + "This is default value "}
Echo "4-Value of var is $ var"

Echo $ {var :? "Print this message "}
Echo "5-Value of var is $ {var }"

 

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.