Linux Shell Script (i)

Source: Internet
Author: User
Tags stdin

first, the initial knowledge of the scriptShell: A class of interpreter between the system kernel and the user.
Scripts: A class of files that are executed in a preset order using a specific language
batch, macro, and interpreter programs

To create a shell script:
Clearing the task process--Organizing execution statements--Perfecting the file structure
1. Mission Objective 1. By task Order 1. Execution Environment
2. What to do first 2. Necessary structure control 2. Necessary annotation Information
3. What to do 3. How to avoid interaction 3. Friendly Tips
4. How to achieve each step

Example:
1) Clarify the task process
1. Add a user named Zhangsan
Add a user named Zhangsan

Useradd Zhangsan

2. Set the password for user Zhangsan to 123456

passwd Zhangsan


2) Organizing execution statements
Useradd Zhangsan

Echo 123456| passwd--stdin Zhangsan


3) Perfect document structure
Script file: uad.sh
#! /bin/bash
#2015 -04-12,by Moon
echo "Creating User account Zhangsan ..."
Useradd Zhangsan
echo "Setting password for user Zhangsan ..."
echo 123456|passwd--stdin Zhangsan

echo "created successfully. "


4) Execute shell script
Method: 1. chmod +x uad.sh
./uad.sh
2. Sh/bash uad.sh

3. source/. uad.sh


second, the combination of shell command applicationPipeline operation: the command output at one end is handed to the other end of the command processing direction: unidirectional
Format: Command 1| command 2

Example:

1.

Find/etc-name "*.conf"-type f//Find files ending with. conf

/etc/autofs_ldap_auth.conf
.. ..
Find/etc-name "*.conf"-type F | Wc-l//WC Statistics-l by row statistics
308

2.

PS aux | grep httpd//gerp Filter leaves only information related to HTTPD


Redirect: Changes the default input and output when executing commands

Type | operator | use
REDIRECT Input |< | Reads data from the specified file instead of typing from the keyboard
REDIRECT Output | >, >> | Overwrite and append output to specified file
Standard error Output |2>, 2>> | Overwrite, append error message to specified file
Mixed output |&>, &>> | Overwrite and append standard output and error information to the specified file

Example:

1.

Uname-r//linux Kernel version information

2.6.18-194.el5
Uname-r > Version.txt//version information saved to Version.txt

2.

Cat Version.txt Version2.txt

2.6.18-194.el5
Cat:version2.txt: No file or directory

Cat version.txt Version2.txt 2>error.txt//Save error messages to Error.txt
2.6.18-194.el5
Cat Error.txt
Cat:version2.txt: No file or directory

Logical separation: Handling the logical relationship between multiple commands
Logic and:&&
Logical OR: | |
Sequential execution::
Example:

1.

echo "Radish" && echo "cabbage"

Radish
Cabbage

echo "Radish" | | echo "Cabbage"


2.

mkdir/mulu/a 2>/dev/null && echo "Success"

mkdir/mulu/a 2>/dev/null | | echo "Failed"

Failed!


3.

CD/BOOT/GRUB:LS-LH grub.comf//-h means easy handling.



iii. use of variables1. Variable Basic operation:
Definition and Assignment: Format: variable name = variable name
Reference variable: Format: $ variable name, ${variable name}

Example:

Title = Moon

Echo $Title
Moon

2. Delimitation Symbol
Double quotes, single quotes, anti-apostrophes
Double quotes: Allow reference, \ Escape
Single quote: Prohibit reference, escape
Anti-apostrophe, or $ ()
Replace with command output

Example:
1.
echo "$Title Group"
Moon Group

Echo ' $Title Group '
$Title Group

2.

Uname-r

2.6.18-194.el5

Ver= ' Uname-r '
Echo $ver
2.6.18-194.el5

3. Common environment variables
Used to record/set operating parameters
System assignment: USER, LOGNAME, HOME, SHELL 、......
User action: PATH, LANG, CLASSPATH 、.....

Example:

1.
Env #列出所有环境变量
Hostname=pc05.benet.com
Shell=/bin/bash
Home=/root
Lognam=root
.. ..
2.echo $USER $HOME SHELL
Zhangsan/root SHELL


Echo $LANG
Zh_cn. UTF-8


Other special variables:
Controlled by a system or script, not directly assigned:
$?: The status value of the previous command, 0 is normal, not 0 exception
$: The program name or path of the script itself
$1-$9: Parameters of the first-nineth position command
$*: The contents of all positional parameters of the command line

$#: Number of positional parameters for the command line


Example:
1.
Mkdir/mulua
echo $?
0

mkdir/mulu/a
mkdir: Unable to create directory "/mulu/a": No file or directory
echo $?
1

2.

Cat test.sh

#!/bin/bash
echo "This program name: $ A"
echo "input $ #个位置参数 at execution time"
echo "One of the first parameters is: $ $"
echo "All parameters are as follows: $*"

./test.sh Hello everyday!

This program name:./test.sh
2 positional parameters are entered at execution time
The first of these parameters is: Helo
All parameters are as follows: Hello everyday!

Four, numerical calculation and processing1. Integer operation:
Evaluate an expression using the expr command
Format: Expr value 1 operator value 2

Use the $[] expression, a formula to change
Format: $[numeric value 1 operator value 2]

Example:
1.
Expr 45+21
66
Expr 45-21
23
Expr 45\*21 the * as a wildcard in #shell, so it should be escaped
945
Expr 45/21
2
Expr 45%21
3
x=45; y=21;expr $X-$Y
24

2.
Echo $[45+21]
66
Echo $[45*21]
945

x=45; Y=21;echo $[x-y]
24

2. Several numerical processing techniques
Recursive handling of variables
Format: Let variable name + +, let variable name--

Using random numbers
RANDOM variable

Generating a sequence of numbers
Format: SEQ first number of last, SEQ first increment last number

Example:
1.
X=45;y=21
Let X++;echo $X
46
Let Y--;echo $Y
20

Let X+=2;echo $X
48

2.
echo $RANDOM #RANDOM variable has a value range of 0-32767
4411
Echo $RANDOM
26911


Echo $[random%100]
54
Echo $[random%100]
83

3.
Seq 3 #第一个数是从1 Start of 1 can be omitted
1
2
3

Seq 3 5
3
4
5

Seq 3 2 10
3
5
7
9

Decimal Run Operation:
Processing an expression to the BC command

Example:
echo "45.67-21" | Bc
24.62

echo "SCALE=4;10/3" | BC #通过scale来约束小数位
3.3333

Linux Shell Script (i)

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.