Shell Script Basics

Source: Internet
Author: User


Shell Script


Standardize the general composition of shell scripts

#! Environmental Statement (Sha-bang)

# comment Text

Executable code


Considerations for setting variables

If the specified variable name already exists, it is equivalent to re-assigning a value to this variable

Don't have spaces on either side of the equals sign

Variable names are made up of letters/numbers/underscores and are case-sensitive

Variable names cannot start with a number, cannot use keywords and special characters

Basic format

Reference variable Value: $ variable Name

View variable values: Echo $ variable name, echo ${variable name}


Environment variables

Typically uppercase to set up a user/system environment

PWD PATH USER LOGNAME

SHELL HOME


Custom variables

User-owned settings use


Example


#!/bin/bash

Echo

echo $

Echo

echo $

Echo ${10}

Echo $#

Echo $*

echo [email protected]


Perform


# sh./1.sh a b c d E F

./1.sh# $ $ for Script name

A# $ is the first parameter of a script

b# $ $ for script second argument

C# $ $ is the third parameter of a script

# $ $ This is indicated as a no value status if "${10}" will give no value to null value

6# $ #给脚本参数的的个数

A b c d E F# $* List all parameters

A b c d E F# [email protected] List all parameters


#!/bin/bash

Cat-n $ | Head-$2 # View the first 2 lines of a file


Positional variables

Command-line arguments (non-interactive pass-through values) that are provided when the script is executed

$ ${10}

#无值 is not equal to a null value

#空值对于变量是属于有值的状态 no value for a variable is a non-value state


Pre-defined variables

The execution information used to save the script

Use these variables directly

You cannot assign values directly to these variables

$# $* [email protected]

$? : The status value after the exit of the execution program, 0 indicates normal, the other value is abnormal


###########################################################################################


Operation

# expr 10/3#除法

# 3

# expr \* 3#乘法 * default to wildcard \ Restore

# 30

# expr 1 + 2#加法

# 3

# expr 3-1#减法

# 2

# expr 3#取余

# 1


\#反斜杠将后一个变量变为纯字符


" #相当于 $ ()#将命令执行后的结果输出

# mkdir $ (date +%f) date

Or

# mkdir ' Date +%f ' date


"#单引号内安纯字符输出


""#双引号保留其中变量输出


Useradd $user &>/dev/null # $>/dev/null output discarded (not shown)


&& #逻辑 and

|| #逻辑 or

! #逻辑 No


Common Test Options


Check file status

-e: Document exists as true

-D: Document exists and is directory-True

-F: Document exists and is file-true

-r: Document exists with Read permission true

-W: Document exists with Write permission true

-X: Document exists with Execute permission true


Compare integer size (e=equal g=greater l=less n=not T=than)

-GT: Greater Than

-ge: greater than or equal to

-eq: Equals

-ne: Not equal to

-LT: Less than

-le: Less than or equal to


string alignment

= = #相等

!= #不相等


REDIRECT Output


Covered:

: Correct output

2>: Error Output

&>: Error and correct output

Additional:

>>: Correct output

2>>: Error Output

&>>: Error and correct output


###########################################################################################


If [condition test 1]; Then

Command sequence XX

elif [condition Test 2]; Then

Command Sequence yy

Else

Command Sequence ZZ

Fi


Cases

Create a/root/foo.sh script on Server0

1) When running/root/foo.sh Redhat, the output is Fedora

2) When running/root/foo.sh fedora, output is Redhat

3) when no parameters or parameters are not redhat or

When fedora, its error output produces the following information:

/root/foo.sh Redhat|fedora


Initial wording:


#!/bin/bash

If [$#-eq 0];then

Echo '/root/foo.sh Redhat|fedora '

Exit

elif [= = Redhat];then

echo Fedora

Exit

elif [= = = Fedora];then

Echo Redhat

Exit

Else

Echo '/root/foo.sh Redhat|fedora '

Fi


After improvement:


#!/bin/bash

If ["$" = = Redhat];then # $ No value is not assigned when no comparison can be made to error

echo Fedora# "$" will be given a null value when it is not assigned a value state can be compared

Exit# changed from $ to "$" to reduce a null value

elif ["$" = = Fedora];then

Echo Redhat

Exit

Else

Echo '/root/foo.sh redhat|fedora ' >&2# >&2 defines the output of echo as an error output

Exit 1#退出并将返回值改为1----echo $? is 1

Fi


###########################################################################################


For loop structure


Loop structure: The repeated execution of the statement, loop to execute


For variable name in value list

Do

Command sequence

Done


Cases

Write a bulk Add user script

Create a/root/batchusers script on Server0

1 This script requires that the user Name list file be supplied as a parameter

2 If no arguments are provided, this script should give a hint

Usage:/root/batchusers, exit and return the corresponding value

3 If you provide a file that does not exist, this script should give a hint

Input file not found, exit and return the corresponding value

4 new User Login Shell is/bin/false, no need to set password


#!/bin/bash

If [$#-eq 0];then

Echo ' Usage:/root/batchusers ' >&2

Exit 1

Fi

If [-F $];then

For A in $ (cat $)

Do

Useradd-s/bin/false $a

Echo $A created successfully

Done

Else

Echo ' Input file not found ' >&2

Exit 1.

Fi

Shell Script Basics

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.