Shell variables and basic flow control statements

Source: Internet
Author: User
Tags aliases sleep function

An environment variable 1 overview

The bash shell uses a feature called environment variables to store information about the shell session and the work environment, which also allows false data to be stored in memory so that the shell can run programs or scripts to access them, a convenient way to store persistent data. This data can identify user accounts, systems, shells, or any content features that need to be stored.

2 Classification of Environment variables 1 Current environment variables

Valid only for the current environment and will disappear when redefined

2 User Environment variables

This environment variable will not be valid when the user switches to a normal user

3 System Environment variables

Valid for all users of the system
View system environment variables,

Setting system-Level environment variables



Refresh

Two-user aliases

Alias

1 Environment Level name



Re-open a window, you are prompted

2 User-level name:



Refresh

Local window view

To reopen a window query, it must be reopened, since it will read the contents when it is opened, and the definition of the alias will not be read into it if it was last opened:

Switch to a generic user, the directive fails

3 System-level aliases

Define it to be written in the/ETC/BASHRC file


To refresh

View the global alias under the root user

Switch to half-user mode for viewing

Use

4 deletion of user aliases
1 Removing system-level names
2 Deleting a user-level name
3 Delete
To delete files

and refresh the view

To delete

View

Three parameters passed in 1 read parameter pass


If you do not specify-p, the display is more monotonous
Options
-P: Output instructions to display

-S: Indicates that the result of the input is not echoed

    • T: Indicates the time-out period not entered

    • -N: Number of characters accepted
      2 passing parameters in the form of arguments after the script

      $ A is the name of the script that passed in the parameter,
      $ = The first parameter passed in
      $ = A second parameter passed in
      And so on
      $# represents the sum of the number of arguments passed in
      $* indicates that all parameters passed in will be displayed as a whole string
      [Email protected] indicates that all parameters are displayed individually

      3 Passing of function parameters


      Where $ represents the first argument after a function call
      $ = The second argument after a function call
      And so on

Quad Flow control Statement 1 if

1 Common IF statements
Format
If
Conditions
Then
Action
Fi
2 Multi-Branch IF statements
If
Conditions
Then
Action
Elif
Conditions
Then
Action
...
Else
Action
Fi

The If loop is less efficient because it is matched from the top to the second, and the if is more efficient than the case

2 case

Case value in
Mode 1)
Command1
Command2
...
CommandN
;;
Mode 2)
Command1
Command2
...
CommandN
;;
Esac


3 for Loop statement 1 General format

For I in the object that can be traversed
Do
Action
Done

2 Special formats

for ((I=1; i<=9;i++))
Do
Action
Done

4 While Loop statement

The while loop is used to continuously execute a series of commands and to read data from the input file, ordering the sugar factory to test conditions in the form:

1 while command format

While condition
Do
commanf
Done

2 General circulation




2 dead Loop


5 expect statements

Expect is generally used to implement automatic remote login with a script to perform related operations

1 installation

2 The purpose of its script is to telnet without having to enter Yes or no:

The above set IP [lindex $argv 0] represents the variable that gets the value of the first parameter to the IP
Spawn means call ssh [email protected] $IP this action
"Yes/no" {send "yes\r"; Exp_continue} means execute Yes and enter when the previous command is encountered
"Password" {send "$AUTH \ r"} means that the following command is executed when password is encountered.
Interact indicates that the operation is continuing and exits automatically if this argument is not added. Instead of logging on to the Node2 node.

3 expect statements nested into bash statements


4 operator

1 (())

2 Let

3 expr

4 $ [$a + $b], you must have a space on both sides of the plus sign:

5 BC Computers on Linux

5 If you compile multiple lines into a file, you can use the cat

Cat > file name <<\eof
The contents of the file to insert
First line of text
Second line of text
Third line of text
Eof
6 sleep function: Used to set the delay for the script to run.

7 Shift function
The position parameter can be shifted left with the shift command. For example, shift 3 means that the original $4 now become $ $, the original $ $ now becomes $ $, and so on, the original $, $, $ discard, $ not move. The shift command with no parameters is equivalent to shift 1. A very useful Unix command: Shift. We know that for positional variables or command-line arguments, the number must be deterministic, or when the Shell program does not know its number, you can assign all parameters together to the variable $*. If the user requires that the Shell do not know the number of positional variables, but also one by one to the parameter one by one processing, that is, after $ $, after the $ $ $, and so on. The value of the variable before the shift command executes is not available after the shift command executes.

Six Practical cases

1 automatically obtain the host name of the host by configuring the file IP and user password


2 Writing user creation and deletion scripts


3 Creating and deleting scripts for writing files

Shell variables and basic flow control statements

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.