Basic knowledge of Linux shell programming

Source: Internet
Author: User

The shell is a scripting language provided by Linux that can perform some programming operations. Essentially, the shell is just a command interpreter, and a shell script is a stack of commands and some simple logic to judge.

So the most important factor in learning shell scripting is being familiar with the use of BASIC commands. and master Some of the shell logic judgments of the statement.

Writing a shell script first declares Shebang, which is:

#!/bin/bash

Because the shell may be involved in a lot of things, I only give you a list of some common things:

1. Command execution status Result: #判断语句的关键
0 #执行成功
1-255 #执行失败

References in 2.shell:
"#tab键上的那个点, the execution result of the reference command; equals $ ()
"#单引号, strong references, variable does not replace
"" #双引号, weak reference, variable will replace

3. Variable assignment and reference:
Assignment: Variable name = "Value" #引号平时可以不加, but must be added if the value is blank;
Reference: $ variable name or ${variable name} #花括号的意义是为了避免变量名后面有字符, resulting in a variable reference error, for example:
[Email protected] tmp]# A=good
[Email protected] tmp]# echo "We are $abye"
We are
[Email protected] tmp]#
#看见了吗? The reference is not a, but a abye, at this time {} comes in handy:
[Email protected] tmp]# echo "We are ${a}bye"
We are goodbye
[Email protected] tmp]#
############# #懂了吗? ########################

4. Custom Script exit code:
Exit [0-255] 0 for success, 1-255 failed
#声明一下:
# The status code after the execution of the script depends on the success or not of the last command executed;

5. Position parameters:
$1/$2 ... #脚本的第一个参数, the second parameter
[Email protected] #脚本的所有参数
@# #统计脚本参数的个数

Logic and logic in 6.shell or:
&& representative and #与运算: true && true = True, true && false = False, false && true = False, false && false = False
|| Representative or #或运算: false && false = False, true && false = True, false && true = True, true && true = True



Condition Judgment:


1. Single-branch conditional judgment statement:
if condition; Then
Statement #如果条件判断成功 (the status code is 0), execute this statement
Fi

2. Two-branch conditional judgment statement:
if condition; Then
Statement 1; #如果条件判断成功 (the status code is 0), execute this statement
Else
Statement 2; #如果条件判断失败 (The status code is 0-255), execute this statement
Fi

3. Multi-branch conditional judgment statement:
If condition 1; Then
Statement 1; #如果条件判断成功 (the status code is 0), execute this statement
Elif Condition 2; Then #如果第一个条件判断失败, make this decision
Statement 2; #如果条件判断成功 (the status code is 0), execute this statement
Elif ...; Then
Statement N;
Else
Statement x; #如果上面的条件判断失败, then execute it.
Fi


For loop:


Circulation body;
Done
# for is a list loop type, list is a listing, and I assigns the data to this list sequentially until the list loop is complete.
There are a number of ways to build a list:
# 1. Place multiple data spaces apart, for example for I in 1 2 3 4; The do meaning is to assign the I to this list (1,2,3,4) in turn and loop. Until the end of the list loop (4 times here)
# 2. Use {1..100} to generate a list of numbers. Meaning is looping 100 times
# 3. Use $ (SEQ 100) to generate a list of numbers. Equivalent to {1..100}


Condition test:

#条件测试使用test  expression, [ expression ], [[ expression ]] symbols. 1. Integer test:    #下面的都是双目测试     -eq   #等于     -ne    #不等于     -lt   #小于     -gt   #大于      -le   #小于等于     -ge   #大于等于2. Character test:    # The following are binocular tests.:        ==      #测试字符串是否相等          !=      #测试字符串是否不等          =~      #左侧是字符串, the right side is a pattern; Determines whether the left-hand string can be matched by the pattern on the right; [[ xxx  =~ X ]] Use     #下面的都是单目测试:         -n       #测试字符串是否不为空         -z       #测试字符串是否为空3. File Test:     #下面的都是单目测试:        -e      #测试是否存在; equals-a   .      -f      #测试是否为普通文件          -d      #测试是否为目录文件         -S       #测试是否为socket文件         -p       #测试是否为命名管道文件         -h     # Test whether the link file;  equals-l        -b     # Test if a block device file;        -c      #测试是否为字符设备文件                  -r       #测试是否可读         -w      #测试是否可写         -x      #测试是否可执行                  -s      #测试文件是否为空

Shift replacement; #替换的意义是用来踢掉位置参数

While loop:

While condition; Do loop body; the Done#while loop is a conditional judgment loop, which starts the loop if the condition is satisfied, exits the loop if it is not satisfied, and a variable is set in the while loop, and the variable changes with the while loop until the exit condition is met; Do loop body; done# This while loop is an infinite loop until the active exit;


Until cycle:

until conditions; do

Circulation body;

Done

#Until循环和while循环正好相反, if the condition is not satisfied to start the cycle, satisfied then exit;


To be added.



This article is from the "Meng Linux" blog, please be sure to keep this source http://minux.blog.51cto.com/8994862/1711202

Basic knowledge of Linux shell programming

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.