Shell Programming Basics

Source: Internet
Author: User
Tags rsyslog

1.1 Shell Programming Basics

1.1.1 basic knowledge of Shell programming

Be familiar with the VIM editor

Familiar with SSH terminal

Master the common Linux commands

Number Master Linux regular expressions and Three Musketeers commands (grep, sed, awk)

/etc/init.d/functions

1.1.2 Variable Classification

Replace more replicated content with a fixed string

Local variables (normal variables):

Must consist of letters, numbers, and underscores that begin with a letter or underscore

The definition of a canonical variable name: see the meaning of the name

HUMP syntax: First word letter lowercase, remaining word letter Capital Oldboyagesex=1

[Email protected] files]# week=6

[Email protected] files]# echo $week

6

Global variables (environment variables):

Uppercase, Linux can be used anywhere

Used in creating their shells and their sent out child shells

File directories related to user environment variables

Global environment variable configuration file

/ETC/PROFILE/ETC/BASHRC/ETC/PROFILE.D the user logs on to the system will run this directory with the following script ending with. sh

User environment variable configuration file

~/,bash_profile ~/.BASHRC

Special variables:

$ number

$ A script name

The 1th parameter of the command-line script

The 2nd parameter of the $ Zero command-line script

The 3rd parameter of the $ A command-line script

The 4th parameter of the $4 command-line script

The number of parameters for the $# script

$? Result of the previous command 0 correct not 0 error

#!bin/bash

A=$1

B=$2

awk-vnum1= $a-vnum2= $b ' begin{print num1+num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1-num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1*num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1/num2} '

[[Email protected] files]# SH num.sh 10 20

30

-10

200

0.5

1.1.1 basic knowledge of Shell programming

Be familiar with the VIM editor

Familiar with SSH terminal

Master the common Linux commands

Number Master Linux regular expressions and Three Musketeers commands (grep, sed, awk)

/etc/init.d/functions

1.1.2 Variable Classification

Replace more replicated content with a fixed string

Local variables (normal variables):

Must consist of letters, numbers, and underscores that begin with a letter or underscore

The definition of a canonical variable name: see the meaning of the name

HUMP syntax: First word letter lowercase, remaining word letter Capital Oldboyagesex=1

[Email protected] files]# week=6

[Email protected] files]# echo $week

6

Global variables (Environment variables):

Uppercase, Linux can be used anywhere

Used in creating their shells and their sent out child shells

File directories related to user environment variables

Global environment variable configuration file

/ETC/PROFILE/ETC/BASHRC/ETC/PROFILE.D the user logs on to the system will run this directory with the following script ending with. sh

User environment variable configuration file

~/,bash_profile ~/.BASHRC

Special variables:

$ number

$ A script name

The 1th parameter of the command-line script

The 2nd parameter of the $ Zero command-line script

The 3rd parameter of the $ A command-line script

The 4th parameter of the $4 command-line script

The number of parameters for the $# script

$? Result of the previous command 0 correct not 0 error

#!bin/bash

A=$1

B=$2

awk-vnum1= $a-vnum2= $b ' begin{print num1+num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1-num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1*num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1/num2} '

[[Email protected] files]# SH num.sh 10 20

30

-10

200

0.5

1.1 Shell conditional Expressions

Determine if the/oldboy directory exists if there is a display dir exist

[Email protected]/]# ls-d/oldboy/&& echo dir exist

/oldboy/

Dir exist

Conditional expression:-D directory exists if the-F file exists

[Email protected]/]# [-d/oldboy]

[[email protected]/]# echo $?

0

[Email protected]/]# [-f/etc/hostsa]| | Touch/etc/hostsa

[Email protected]/]# Ll/etc/hostsa

-rw-r--r--. 1 root oldboy 0 June 2 17:09/etc/hostsa

[1-eq 1]

-eq equal = =

-ne Not Equal! =

-GT great than >

-ge Great equal >=

-lt less than <

-le less equal!<

1.2 If statement

#单分支的判断

If [If you are Gaofu];then

You can marry mating Cinderella ... Pick whatever you want

Fi

[email protected] scripts]# cat cal.sh

#!/bin/bash

A=$1

B=$2

If [$#-ne 2];then

echo "Usage $ NUM1 num2"

Exit

Fi

awk-vnum1= $a-vnum2= $b ' begin{print num1+num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1-num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1/num2} '

awk-vnum1= $a-vnum2= $b ' begin{print num1*num2} '

#双分支的判断

If [If you are Gaofu];then

You can marry mating Cinderella ... Pick whatever you want

Else

As long as it's a woman.

Fi

#输入两个数字

#比较两个数字的大小

# a > B show a bigger than B

#a <= b shows a less equal b

#if [1th number greater than 2nd digit];then

#显示 $oldboy > $lidao

#否则

#显示 $oldboy <= $lidao

#fi

[email protected] scripts]# cat compare.sh

#!/bin/bash

Oldboy=$1

Lidao=$2

If [$#-ne 2];then

echo "Usage:$0 num1 num2:"

Exit

Fi

If [$oldboy-gt $lidao];then

echo "$oldboy bigger than $lidao"

Else

echo "$oldboy less equal $lidao"

Fi

1.3 Read interactive mode

[Email protected] scripts]# read-t 5-p "Input password:" A b-t dwell time –p Show input password

Input Password:10 20–s does not display content

[Email protected] scripts]# echo $a $b

10 20

[email protected] files]# cat a.sh

#!/bin/bash

Read-p "Input:" a B

If [$a-gt $b];then

echo "$a > $b"

elif [$a-eq $b];then

echo "$a = $b"

Else

echo "$a < $b"

Fi

[Email protected] files]# sh a.sh

Input content: 20 10

> 10

[Email protected] files]# sh a.sh

Input content: 20 20

20 = 20

[Email protected] files]# sh a.sh

Input content: 10 20

Ten < 20

1.4 for Loop

For on Do

[[email protected] files]# for Lidao in 3 2 1 6 5 4

> Do

> Echo $lidao

> Done

3

2

1

6

5

4

[[email protected] files]# for n in {01..7}; Do echo Zhao,${n}week ${n}group take your to great care, find ${n}woman.; Done

Zhao,01week 01group take your to great care, find 01woman.

Zhao,02week 02group take your to great care, find 02woman.

Zhao,03week 03group take your to great care, find 03woman.

Zhao,04week 04group take your to great care, find 04woman.

Zhao,05week 05group take your to great care, find 05woman.

Zhao,06week 06group take your to great care, find 06woman.

Zhao,07week 07group take your to great care, find 07woman.

Optimize Linux boot project, keep only crond rsyslog sshd network sysstat others off

[[email protected] files]# for n ' Chkconfig|awk '!/crond|sshd|network|rsyslog|sysstat/{print $ ';d o chkconfig $n off ;d One

[Email protected] files]# chkconfig |sed-nr '/crond|sshd|network|rsyslog|sysstat/p '

Crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Network 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off

sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

Sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off

Shell Programming Basics

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.