Introduction to shell programming and use of simple commands

Source: Internet
Author: User

Let's take a look at the basic knowledge of shell. I am tired .. Haha 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0559241401-0.gif "alt =" j_0003.gif "/>



Shell programming


Shell Basics

What is shell?

Shell is a command line interpreter. kerne is used for memory scheduling and resource allocation.

What shell does linux support?

[Root @ xu/] # vim/etc/shells


Bash benefits

Many built-in commands have better compatibility and support many features.

Built-in commands are carried by the system itself

The external creation command was later installed.


Shortcut Key

History stores the latest 1000 entries

! The ser calls the command file that has been used before in ~ /. Bash_history

History-c: Clear the historical commands on the current terminal

History-w overwrites the current command into the file, or it will not be saved until it is restarted.


[Root @ xu/] # vim/etc/profile system-level environment variables

[Root @ xu/] # vim/etc/bashrc


Cd/home/test/

Ls-

. Bash_profile user-level environment variables

. Bashrc


The order of the four is

1 vim/etc/bashrc

2 vim/etc/profile

3. bashrc

4. bash_profile


Modify historical storage quantity

[Root @ xu/] # vim/etc/profile

39 HISTSIZE = 1000 modify


Support for alias configuration files

[Root @ xu test] # vim/root/. bashrc


; Indicates that commands are executed sequentially regardless of whether the preceding commands are successful or not.

& Executed only after successful execution

| Executed only after the previous failure

''Give priority to the commands in''

2> standard error coverage

2> standard error appending

&> Make sure that all errors are correct.


--------------------------------------

Shell variable

A variable stores changed characters with fixed characters.



Variable types include


1. system environment variable 2poi65z2qa

Automatically generated when the system is started. Do not modify it.

View

[Root @ xu ~] # Env can only display system environment variables. Custom variables cannot be displayed.

[Root @ xu ~] # Set can display custom Variables




2 pre-defined Variables

The values of predefined variables are also fixed.


[Root @ xu ~] # Echo $ # Number of location Variables

[Root @ xu ~] # Echo $ * location variable content

[Root @ xu ~] # Echo $ the pid of the background process is the pid of the bash background process.

[Root @ xu ~] # Echo $? If the status of the execution result of the previous command is 0, the execution is correct. If the execution result is not 0, the execution is incorrect.

[Root @ xu ~] # Echo $0 name of the current process or Script Name

[Root @ xu ~] # Echo $!



1 #! /Bin/bash

2 # script description

3 echo $1 location variable

4 echo $ # Number of location Variables

5 echo $ * content

6 echo $0 name



Script Execution Method

Sh a. sh

Bash a. sh

Chmod + x a. sh

./A. sh

/Root/a. sh



3. location variable

$1 ...... $9 is the location variable.


Custom Variables

A = 1

Define your own variables as needed

Note:

1 = no space on both sides

2 case sensitive

3. When the variable names in the script are the same, the defined values take effect.

4. The variable name cannot start with a number.

When it cannot be calculated, all characters are classified as characters, which will be converted into numerical values During computation.


5. Do not use special characters or keywords to define variable names.




Use Variables

Echo $


Undo variable

Unset


Variable value Accumulation


[Root @ read ~] # Xing = xu

[Root @ read ~] # Ming = yang

[Root @ read ~] # Name = $ xing $ ming

[Root @ read ~] # Edcho $ name

[Root @ read ~] # Echo $ name

Xuyang


------------------------------------------

Prepare for Process Control)

Target object of the test option

[Option target object]


File status test man test)

-Whether the e file exists is true

-D does not exist and is a directory

-F is not a file

-R readable

-W writable

-X executable


0 indicates true

1 indicates false



Number comparison

-Eq =

-Lt is less than <

-Gt>

-Ne is not equal

-Le is less than or equal to <=

-Ge greater than or equal to> =




$? View results



Character comparison


= Assign value

= Compare the left and right

! = Not equal to null

-Z: determines whether the string is null.



Symbol

"" Interpretation variables in double quotes

''Meaning of single cited cancellation variable


[Root @ read Server] # name = xu

[Root @ read Server] # echo "my is $ name"

My is xu

[Root @ read Server] # echo 'my is $ name'

My is $ name


Set variable time

[Root @ read Server] # date "+ % F"

2013-03-03

[Root @ read Server] # daytime = 'date "+ % F "'

[Root @ read Server] # echo $ daytime

2013-03-03


Capacity used for capturing/

[Root @ read Server] # df-H

File System capacity used available % mount point

/Dev/sda2 204G 54G 140G 28%/

/Dev/sda1 104 M 38 M 61 M 39%/boot

Tmpfs 1.1G 0 1.1G 0%/dev/shm

/Rhel5u. iso 3.6G 3.6G 0 100%/mnt



[Root @ read Server] # df-H | grep "/dev/sda2" | awk '{print $5}' | awk-F "%" '{print $1}'

[Root @ read Server] # rootdata = 'df-H | grep "/dev/sda2" | awk '{print $5}' | awk-F "%" '{print $1 }''

[Root @ read Server] # echo $ rootdata

28


[Root @ read Server] # [$ rootdata-gt 20] & mail-s "aaa" root @ localhost </etc/a.txt





Input name script


1 #! /Bin/bash

Waiting for input time

Read-t 15-p "pleass your name" name

Read-t 15-p "pleass your name" age

[-Z $ name] & echo "name is null"



Logical comparison

Two or more conditions are used.


&-

|-O

! Invert

! -Z is not empty


[Root @ read Server] # [10-eq 10] | [5-gt 6]

[Root @ read Server] # echo $?

0


[Root @ read Server] # [10-eq 10-o 5-gt 6]

[Root @ read Server] # echo $?

0










Python language

Installation Package

Rpm-ivh python-2.4.3-46.el5.i386.rpm




Variable range

The subshells of the current shell are all useful.


Customize global variables

Name = xu

Export name = xu



The variables in the script are valid only in the script.

No use outside


This article is from the "history_xcy" blog, please be sure to keep this http://historys.blog.51cto.com/7903899/1296706

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.