Beginner Linux--shell

Source: Internet
Author: User
Tags arithmetic

Shell

The shell is a weakly typed programming language.

Distinction between strong and weak:

Strong: Variables must be declared prior to use, and even require initial

Weak: Variables are declared when they are used, even the part is differentiated by type

Bash Variable type
Environment variables
Local variables (local variables)
Positional variables
Special variables

Local variables:
Bash:
Vatname=value: scope for the entire bash process
Local variables:
Local varname=value; Scope is the current code snippet
Environment variable: scope is the current shell process and its child processes
Export Varname=value
Name=value
Export name
Export

Positional variables:
$1,$2 ...
Shift

Such as:
[email protected] shell]# cat shift.sh
#!/bin/bash
echo $
Shift
echo $
Shift
echo $
[Email protected] shell]#

[Email protected] shell]#./shift.sh 1 2 3
1
2
3
[[email protected]shell]#

Special variables:
1. $? The execution status return value of the previous command
program execution, there may be two types of return values
Program execution Results
Program Status return codes (0-255)
0: Correct execution
1-255: Error execution 1,2,127 system reservation
2. Number of $# parameters
3. $* parameter list
4. [Email protected] parameter list

/dev/null soft device bit bucket data black hole
Undo Variable
Unset VARNAME
View variables in the current shell
Set
View environment variables in the current shell
Set
Env
Export

Script: Command stack, according to the actual needs, combined with the command flow control mechanism to implement the source program
Shebang: Magic number
#!/bin/bash
#开头为注释行, do not perform

The script starts a child shell process when it executes
Scripts that are started on the command line inherit the current shell environment variable
The system automates the footstep (non-command line startup)

Reference variable: Scope is the current shell process and its child processes

${varname}
Name=pao;
echo "The Hello Word ${name}",

Conditional judgment
If the user does not exist
Add user, give password and show add success
Otherwise
Displays if it already exists, does not add
How do I make conditional judgments in bash?
Condition Toilet Type
Integer test
Character test
File test
Expressions for Conditional tests:
"Expression"
"Expression" "
Test expression
Integer comparison:
-EQ: Tests whether two integers are equal; $a-eq $b
-ne: Tests whether two integers are equal, unequal to true, and equal to False
-GT: Tests whether one number is greater than the other, is greater than true, or false
-LT: Test whether a number is small with another number, small and true, otherwise false;
-ge: greater than or equal to
-le: Less than or equal to
File test
-e file test file exists
-F file Test file is a normal file
-D file Test knows if the path is a directory
-R file tests whether the current user has read access to the specified file
-W file tests whether the current user has write access to the specified file
-X file tests whether the current user has executable permissions on the specified file
"-e/etc/inittab"

The logical relationship between commands:
Logic and:&&
When the first condition is false, the second condition is not judged and the end result has
When the first condition is true, the second condition must be judged
Logical OR: | |

Conditional judgment, control structure
Single Branch if statement

if judgment condition; then
Statement1
Statement2
....
Fi

Dual-Branch If statement
If judgment statement; then
Statement1
Statement2
....
Else
Statement3
Statement4
......
Fi

Multi-Branch If statement
if judgment condition; then
Statement1
......
Elif Judging Condition 2;then
Statement2
......
Else
......
Fi


Test script for syntax errors
Bash-n script File
Script single-Step execution
Bash-x script File
Defining script Exit Status codes
Exit: Exit Script
Exit #
If the script does not explicitly define the exit status code, then the exit code of the last command executed is the exit code of the script

How to perform arithmetic operations in the shell
A=3
B=6
1. Let arithmetic operation expression arithmetic operation command
Let =c= $a + $b
2. $ (arithmetic op-expression)
d=$[$a + $b]
3. $ (arithmetic expression)
d=$ (($a + $b))
4. Expr commands an arithmetic expression that has spaces between the operands and operators in the expression, and to use a command reference
C= ' expr $a + $b ' Note: ' for inverted single quotes '


This article is from the "Linux Operations Technology Exchange" blog, please be sure to keep this source http://linuxzhang.blog.51cto.com/2580498/1657289

Beginner Linux--shell

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.