BASH Getting Started Tutorial

xiaoxiao2021-03-06  38

Wednesday, October 06, 2004

BASH Getting Started Tutorial

Want to play Bash? So entering the Bash Programming Teaching Shell Script (Bash) Introduction, Unix is ​​known as a small tool, using many simple gadgets to complete the work that you need a lot of software development, this Make Unix becomes the ideal system platform in many people's minds. In numerous gadgets, Shell Script calculates the most basic, most powerful and widely used. It uses a wide range, not only from the system start, program compile, regular operation, online connection, and even install the entire Linux system, can be done with it. Because shell script uses some of the instructions used in your weekday, it combines it to become a "program." If you are particularly frequent, you can combine these instructions to become another new instruction. In this way, it can not only simplify and accelerate the operating speed, but also simplify system management work, but also simplify system management. ************************ Bash (GNU Bourne-Again Shell) is a memory shell of many Linux platforms, in fact, there are many traditional UNIX With Shell, like TCSH, CSH, ASH, BSH, KSH, etc., Shell Script is roughly class. When you learn a shell, other shell will quickly go up, most of the time, a shell script usually Can be used on many shells. Here I introduce your BASH usage. In fact, when you "man bash", you can see Bash's instructions, but for many people, this manual is like "no words". In this document, the main information is "Man Bash", and I add some actual daily application examples. I hope that people who have never entering the door will have some concepts. "Hello World" shell Script Photo of traditional programming, this section introduces how shell Script "Hello World" writes. ************************ #! / Bin / sh # filename: Hello Echo "Hello World!" ********* **************** You should pay attention to "#! / Bin / sh" of the first line. Under UNIX, all can perform Script, whether it is the language, its start is "#!", Such as Perl is "#! / Usr / bin / perl", TCL / TK is "#! / Usr / BIN / WISH, see where the Script program you want to perform is there. You can also use "#! / Bin / bash", "#! / Bin / tcsh", etc. to specify the use of a specific shell. Echo is a BASH built-in instruction. ************************ Next, execute Hello this script: There are many ways to perform a script. ************************* 1: Set the permissions of Hello's file to executable.

[Foxman @ Foxman Bash] # chmod 755 Hello execution [Foxman @ foxman bash] # ./hello hello world *********************** Second Type: Use Bash to build instructions "Source" or ".". [Foxman @ Foxman Bash] # Source Hello Hello World or [Foxman @ Foxman Bash] #. Hello Hello World *********************** Third Type: Directly use the SH / BASH / TCSH instruction. [Foxman @ Foxman Bash] # sh Hello Hello World or [Foxman @ Foxman Bash] # Bash Hello Hello World *********************** BASH Execution Option *********************** -C String: Read the string to command. -I: interactive interface. -S: Read commands by stdin. -: Cancel the read option. -Norc: Do not read ~ / .bashrc. -Noprofile: Do not read /etc/profile ,~/.bash_profile ,~/.bash_login ,~/.profile, etc. -Rcfile filename: Execute FileName instead of ~ / .bashrc -version: Display version. -Quiet: Don't hit when startup. -Login: Make sure Bash is a Login Shell. -NobraceExpansion: Do not use Curly Brace Expansion ({{{} symbol expand). -NolineEDITING: Do not read the command column without Readline. -Posix: Change the POSIX 1003.2 standard. Shell Script for automatic backup A shell script for automatic backup we agreed that you can use the shell script with crred to perform regular work. To make a regular work, on UNIX is the use of crones. ************************ First we first study how to back up the system. To back up the system, it is nothing more than some compression tools. On many UNIX systems, TAR and GZIP are DE FACTO data exchange criteria. We can often see some tar.gz or tgz files, these files, called Tarball. Of course, you can also compress the compression tool with BZIP2, ZIP, etc., does not have to be limited to GZIP. But TAR cooperates with GZIP is the most common and most convenient way. To compress the information we want, back up, you can carry out Tar and Gzip. There are many ways, the most commonly used instructions are the following: TAR-C File / Dir ... | Gzip -9> xxxx.tar.gz You can also do it: Tar -R file / dir ... -f xxxx.tar gzip -9 xxxx.tar or tar -r file / dir ... -f xxxx.tar gzip -9xxxx.tar.gz

*******************************

After the basic knowledge of the file backup of Linux, we will write a script that will back up the file.

#! / bin / sh

# Filename: Backup

DIRS = "/ etc / var /your_directories_or_files"backup="/tmp/backup.tgz"

TAR-C $ DIRS | GZIP -9> $ Backup

Where DIRS is placed in the files and directories you want backup, Backup is your backup file. Don't put the / TMP in DIRS, do it, you are a backup of your backup, you may explode your hard disc.

*******************************

Next test

[Foxman @ Foxman Bash] # chmod 755 Backup

[Foxman @ Foxman Bash] # ./backup

After the execution is complete, there will be a backup.tgz in / TMP, which is stored in your important information. You can use

gzip -dc /tmp/backup.tgz | Tar -VT

or

TAR VTFZ / TMP/Backup.tgz

Let's take a look at the list of files.

When you want to unwise, you can use the following instructions to complete the recovery:

gzip -dc /tmp/backup.tgz | TAR-XV

or

TAR XVFZ /TMP/Backup.tgz

Backup is usually the most important part of the backup system, / etc can be said to be an indispensable part. In addition, seeing important information in your system requires backup. In general, you don't have to back up / bin, / sbin, / usr / bin, / usr / sbin, / usr / x11r6 / bin, etc. These executive directory. As long as you back up your important file, don't back up the entire hard drive, that is a lot of motion.

*******************************

If you have many machines, you can take advantage of one of the lighter internal network hosts, as the main backup host. Automatically perform backups all machines, then use NFS / CODA / Samba such as NFS / CODA / Samba, put the backup information in this backup machine, then charge backup data, then you will make a backup in the machine. .

Here is the illustration of the entire system backup scheme.

Before you do, let's take it, those in the system are backup, those who are not needed.

*******************************

New Backup

#! / bin / sh

Hostname = `Hostname`

DIRS = "/ etc / var / your_important_directory"

Backup = "/ TMP / $ hostname.tgz"

NFS = "/ mNT / NFS"

TAR-C $ DIRS | GZIP -9> $ Backup

MV -F $ backup $ NFS

*******************************

Backup Script: Collect_Backup in the host

#! / bin / sh

NFS = "/ mNT / NFS"

BACKUP = "/ backup"

MV -F $ NFS / *. TGZ $ BACKUP

Here, you cannot put all backups directly in / MNT / NFS, which is dangerous. In case any machine accidentally deletes / mnt / nfs all content, then the backup will disappear. Therefore, you need to move / MNT / NFS to a directory where the backup host is accessible.

*******************************

When these individual scripts are tested, we will put them in crontab. Find your crontab, its location may be on / var / spool / cron / crontabs / root, / etc / crontab, / var / cron / tabs / root.

Select one of the following to join (see your regular time): slonware: / var / spool / cron / crontabs / root

01 * * * * / full_backup_script_path / backup 1> / dev / null 2> / dev / null # hour (too fire a little)

30 16 * * * / full_backup_script_path / backup 1> / dev / null 2> / dev / null # 1 daytime 16:30, backup before get off work

30 16 * * 0 / full_backup_script_path / backup 1> / dev / null 2> / dev / null # 6:30 on Monday

0 5 1 * * / full_backup_script_path / backup 1> / dev / null 2> / dev / null # monthly No. 1 5: 0

Redhat / Debian: / etc / crontab

Redhat can put backup into /etc/cron.Hourly, /etc/cron.daily, /etc/cron.monthly. Or use the way to add / etc / crontab:

For the use of crontab, "Man 5 CRONTAB" can be checked, which is not detailed.

The set class of the backup host is the same.

Note: All machines do not back up simultaneously, otherwise the network will be large. The backup host will be set to the end, otherwise the backup information will not be received. You can adjust the time interval after the actual work.

*******************************

Take a look, two small small SCRIPTs, with Cron this timing tool. It can make the artificial backup of time consuming to take time to simplify to less than ten minutes. Good use of your imagination, add more changes, but you will make your life easy and happy.

Archive system check

System security has always been a matter of most computer users. In the UNIX system, the most important thing is, that is, there is "Trojan Horse" in the system. No matter how Trojan Horse is put in, it will always constantly, that is, the file that is placed in Trojans, the file date will be changed, and there will even be other state changes. In addition, in many cases, there will be some unknown files. Therefore, the state of the entire archive system is changed on weekdays, and all states have changed files, and there are currently those who are being implemented, automatically report to the system administrator, is a good way to sit on "Trojans".

*******************************

#! / bin / sh

# Filename: whatver_you_name_it

DIRS = "/ etc / home / bin / sbin / usr / bin / usr / sbin / usr / local / var / your_directory"

Admin = "email@your.domain.com"

From = "admin@your.domain.com"

# 写入 s 的 标 的

Echo "Subject: $ Hostname FileSystem Check"> /TMP/Today.mail

Echo "from: $ from" >> /tmp/today.mail

Echo "to: $ admin" >> /TMP/Today.mail

Echo "this is filesystem report comes from $ hostname" >> /TMP/Today.mail# report the program currently executing

PS AXF >> /TMP/Today.mail

# Archive system check

Echo "File System Check" >> /TMP/Today.mail

LS -ALR $ DIRS | GZIP -9> /TMP/Today.gz

ZDIFF /TMP/Today.gz /TMP/yesterday.gz >> /TMP/Today.mail

MV -F /TMP/Today.gz /TMP/yesterday.gz

# Send a letter

Sendmail -t

Then put it in an unspeakable place, let others can't find it.

Add it to crontab.

30 7 * * / full_check_script_path / wherever_you_name_it 1> / dev / null 2> / dev / null check before work

Some files are fixed, like / var / log / messages, / var / log / syslog, / dev / ttyx, etc., don't be too strange.

Controlle for

Demonstrate a few simple shell scrips, I believe you should have a little concept of Shell Script. Now let's start carefully study some higher SHELL Script writing. Some further descriptions, such as "$", ">", ">", "1>", "2>" symbols will be explained later.

*******************************

For name [in word;] do list; done

The control ring.

Word is a sequence word, and for expands individual words in Word and set it to the name of Name. List is a sequence of work. If [in Word;] is omitted, the Name will be set to the parameters added behind Script.

*******************************

Example 1:

#! / bin / sh

For i in a b c d e f; do

Echo $ I

DONE

It will display A to F.

*******************************

Example 2: Another Usage, A-Z

#! / bin / sh

Word = "A B C D e f G H i J L M N o P Q R S T U V W x Y Z"

For i in $ word; do

Echo $ I

DONE

This Script will display A to Z.

*******************************

Example 3: Modify the secondary file name

If you have a lot of .txt files want to change your name .DOC file, you don't need one.

#! / bin / sh

Files = `ls / txt / *. Txt`

FOR TXT IN $ FILES; DO

DOC = `Echo $ txt | sed" s / .txt / .doc / "`

MV $ TXT $ doc

DONE

This can modify the * .txt file to * .doc file.

*******************************

Example 4: Meow

#! / bin / sh

# Filename: meow

FOR i; do

Cat $ I

DONE

When you enter "Meow File1 File2 ...", its role is the same as "Cat File1 File2 ...".

*******************************

Example 5: Listbin

#! / bin / sh

# Filename: Listbin

For i in / bin / *; do

Echo $ I

DONE

When you enter "Listbin", its role is the same as "LS / BIN / *".

*******************************

Example 6: /etc/rc.d/rc

Take an actual example, a piece in the startup of the Red Hat /etc/rc.d/rc.

For i Iin /etc/rc.d/rc (DO)

# Check if the script is there.

[! -F $ I] && Continue

# Check if the subsystem is already up.

Subsys = $ {i # / etc / rc.d / rc $ runlevel.d / s ??}

[-f / var / lock / subsys / $ subsys] ||

[-f /var/lock/subsys/$ @ sbsys }.init] && Continue

# Bring the subsystem up.

$ I start

DONE

In this example, it finds all files to check if it exists, and then execute one by one.

Process Control Case

Case Word In [Pattern [Pattern] ...) list ;;] ... ESAC

Case / ESAC standard usage is approximately as follows:

Case $ arg in

Pattern | SAMPLE) # arg in pattern or sample

;

Pattern1) # arg in pattern1

;

* #default

;

ESAC

Arg is the parameters you are introduced. If the Arg content is in line with the Pattern project, then the following program code is executed, and the segment code is two -more - (28%) semicolon ";;" Do the end.

It can be noted that "Case" and "ESAC" are symmetrical. If you can't remember, "Case" is reversed.

*******************************

Example 1: paranoia

#! / bin / sh

Case $ 1 in

Start | begin)

echo "start something"

;

STOP | END)

Echo "stop Something"

;

*)

Echo "Ignorant"

;

ESAC

carried out

[Foxman @ Foxman Bash] # chmod 755 Paranoia

[Foxman @ foxman bash] # ./paranoia

Ignorant

[Foxman @ foxman bash] # ./paranoia start

START Something

[Foxman @ Foxman Bash] # ./paranoia begin

START Something

[Foxman @ foxman bash] # ./paranoia stop

Stop Something

[Foxman @ Foxman Bash] # ./paranoia end

Stop Something

*******************************

Example 2: inetpanel

Many Daemon will attach a managed shell script, attached to NDC like bind, and apache is attached to ApachectL. These management programs are written in SH ELL Script, and the following shows a shell script that manages inetd. #! / bin / sh

Case $ 1 in

Start | begin | Commence

/ usr / sbin / inetd

;

STOP | End | Destroy)

KILLALL INETD

;

Restart | AGAIN

KILLALL-HUP inetd

;

*)

Echo "Usage: inetpanel [start | begin | commence | stop | end | destory | restart | again]"

;

ESAC

*******************************

Example 3: Judgment System

Sometimes, the script you have written may span several platforms, such as Linux, FreeBSD, Solaris, etc. Decrease. At this point, we can use uname to find system information.

#! / bin / sh

System = `uname -s`

Case $ SYSTEM IN

Linux)

Echo "My System Is Linux"

Echo "Do Linux Stuff Here ..."

;

FreeBSD)

Echo "My System Is FreeBSD"

Echo "Do Freebsd Stuff Here ..."

;

*)

Echo "Unknown System: $ SYSTEM"

Echo "I don't what to do ..."

;

ESAC

Process Control SELECT

Select Name [in Word;] do list; DONE

SELECT As the name implies is to select an item in Word. As with for, if [in Word;] is omitted, the parameters added later after Script will be used.

example:

#! / bin / sh

Word = "a b c"

SELECT I IN $ WORD; DO

Case $ I in

a)

ECHO "I am A"

;

b)

ECHO "I am B"

;

c)

ECHO "I am C"

;

*)

Break;

;

ESAC

DONE

Results of the

[Foxman @ Foxman Bash] # ./select_demo

1) a

2) B

3) C

#? 1

I am A

1) a

2) B

3) C

#? 2

I am B

1) a

2) B

3) C

#? 3

I am C

1) a

2) B

3) C

#? 4

Return status EXIT

Before we continue, we must cut another topic, return status value - exit status. Because IF / While / Until moved to control program processes using EXIT STATUS.

*******************************

Many people know that in many languages ​​(C / C / perl ....) have an exit function, even Bash has an exit built-in command. The number behind the exit is the return status value - exit status.

The return status value can make the probaches and probabilities, using the shell script to combine the possibility of combination, using small-range, through the shell script, to complete very miscellaneous work. In the shell, the return value is zero means success (TRUE), the non-zero value is failed (false).

*******************************

For example, the following two small-range YES / NO will return 0/1 (success / failure), respectively:

/ * yes.c * /

Void main (void) {exit (0);

/ * no.c * /

Void main (void) {exit (1);

Then the following "Yes" shell script will display "Yes".

#! / bin / sh

# Yes

IF Yes; THEN

Echo "YES"

Fi

And "No" does not show anything.

#! / bin / sh

# No

if NO; ​​Then

Echo "YES"

Fi

*******************************

Test Express

[express]

In Shell Script, Test Express / [Express] This syntax is used in a lot of use, it is a very practical instruction. Since its return value is EXIT STATUS, it is often used in an IF / While / Until case. And after we will use a lot, it is necessary to understand before entering the introduction of if / while / usl.

Its return value is 0 (TRUE) or 1 (FALSE), and it is to look at the result of the expression (Express).

EXPRESS format

-b file: When the file exists and attributes is Block Special (usually / dev / xxx), returns TRUE.

-c file: When the file exists and attribute is Character Special (usually / dev / xxx), returns TRUE.

-d file: Returns true when the file exists and attribute is a directory.

-e file: When the file exists, returns true.

-f file: Returns true when the file exists and is a normal file.

-g file: When the file exists and is a set-group-id, returns True.

-k file: Returns true when the file exists and has a "sticky" bit is set.

-L file: Returns true when the file exists and is Symbolic Link.

-p file: When the file exists and is Name PIPE, returns TRUE.

-r file: Returns true when the file exists and readable.

-s file: True is returned when the file exists and the file size is greater than zero.

-S file: Returns True when the file exists and is socket.

-t fd: Returns true when the FD is turned on to Terminal.

-u file: True is returned when the file exists and set-user-id bit is set.

-w File: Returns true when the file exists and writes.

-x file: Returns true when the file exists and execures.

-O file: Returns true when the file exists and is owned by the User ID executed.

-G file: Returns True when the archive is exist and is owned by the Group ID.

File1 -NT file2: Returns true when File1 is new than file2 (according to the modification time).

File1 -ot file2: When File1 is older than file2 (according to the modification time), returns True. File1 -EF file2: When File1 has the same device and inode Number as File2, returns true.

-z String: Returns true when the length is zero.

-n string: When the length of the String is not zero, returns True.

String1 = String2: String1 Returns true when string2 is equal to String2.

String1! = String2: String1 Returns True when string2 is not equal.

! Express: EXPRESS Returns True when false.

EXPR1 -A EXPR2: EXPR1 and EXPR2 are TRUE.

EXPR1 -O EXPR2: EXPR1 or EXPR2 is one of true.

Arg1 OP Arg2: OP is -EQ [Equal], - Ne [not-equal], - lt [less-than], - le [less-than-or-equal], - gt [Greater-Than], -gee One of the [Greater-Than-or-equal].

*******************************

In Bash, the BASH returns 128 Signal Number as the return value when the error occurs in a fatal signal. If the command is not found, 127 will be returned. If the command is found, the command is not executable, and 126 will be returned. In addition, Bash itself returns the return value of the last instruction. If an error occurs during execution, a non-zero value will be returned.

Fatal Signal: 128 SIGNO

Can't NOT FIND COMMAND: 127

Can't NOT Execute: 126

Shell Script successful exit status

Fatal During Execution: Return Non-Zero

Process control IF

IF list damt [elif list the list] ... [else list] FI

Several possible ways

*******************************

The first

IF List kil

Do Something Here

Fi

"Do Something Here" will be executed when the List represents the return value is true (0).

Example 1: When we want to execute a command or program, sometimes you need to check if the command exists, and then execute.

IF [-x / sbin / quotaon]; then

Echo "Turning on Quota For Root FileSystem"

/ sbin / quotaon /

Fi

Example 2: When we use a file as a setting file, you can check if there is, and then load the file set value.

# Filename: / etc / ppp / settings

Phone = 1-800-color

#! / bin / sh

# Filename: Phonebill

IF [-f / etc / ppp / settings]; then

Source / etc / ppp / settings

Echo $ Phone

Fi

carried out

[Foxman @ Foxman PPP] # ./phonebill

1-800-Collect

*******************************

Second

IF List kil

Do Something Here

Else

Do Something Else Herefi

Example 3: Hostname

#! / bin / sh

IF [-f / etc / hostname]; then

Hostname = `Cat / etc / hostname`

Else

Hostname = localhost

Fi

*******************************

Third

IF List kil

Do Something Here

Elif List kil

Do Another Thing Here

Fi

Example 4: If a setting file allows several locations, such as crontab, you can use the if Ten Elif Fi to find.

#! / bin / sh

IF [-f / etc / crontab]; then

Crontab = "/ etc / crontab"

Elif [-f / var / spool / cron / crontabs / root]; then

Crontab = "/ var / spool / cron / crontabs / root"

Elif [-f / var / cron / tabs / root]; then

Crontab = "/ var / cron / tabs / root"

Fi

EXPORT crontab

*******************************

Fourth

IF List kil

Do Something Here

Elif List kil

Do Another Thing Here

Else

Do Something else Here

Fi

Example 5: We can use uname to judge the current system and do something different from the system conditions.

#! / bin / sh

System = `uname -s`

IF [$ system = "linux"]; then

echo "linux"

Elif [$ system = "freebsd"];.

Echo "freebsd"

Elif [$ system = "solaris"]; then

Echo "Solaris"

Else

echo "what?"

Fi

WHILE / UNIL

While List Do List Done

When List is True, the circle will not perform.

Example 1: Unlimited back ring

#! / bin / sh

While:; do

Echo "Do Something Forever Here"

Sleep 5

DONE

Example 2: Forced to kill PPPD.

#! / bin / sh

While [-f /var/run/ppp0.pid]; do

KILLALL PPPD

DONE

*******************************

Until List Do List Done

When List is false (Non-Zero), the circle will do constantly.

Example 1: Waiting for the PPPD to go online.

#! / bin / sh

Until [-f /var/run/ppp0.pid]; do

Sleep 1

DONE

Parameter and variable

Before you continue to introduce Function, we must stop introducing "parameters and variables".

*******************************

Parameters is a data type used to store "value", a bit like a variable in general language. It can be a name (name), a number, or some special symbols listed below (Special Parameters).

In the shell, the variation is configured by the parameters in the form of the NAME. *******************************

In many examples in the previous example, we have seen many of the use of many parameters. To set a parameter, it is very simple:

Name = value

for example:

Myhost = "foxman"

To use it, add a "$" symbol.

Echo $ MyHost

*******************************

Positional parameters

*******************************

The so-called positional parameters are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 .... Take $ 0, $ 1, $ 2 ....

The position parameter is the parameter attached later when the Script is loaded. $ 0 is itself, $ 1 is the first parameter, $ 2 is the second, and so on. When Positional Parameters are used by Function, they are temporarily replaced (the next section will introduce function).

For example, the following Script:

#! / bin / sh

# Filename: Position

Echo $ 0

Echo $ 1

Performance:

[Foxman @ foxman bash] # ./position abc

./position

ABC

When the position parameter exceeds two digits, there is a special method to expand, called Expansion.

*******************************

Special Parameters

These symbols are very unmanned, which is very troublesoned for novices. But after picking up, I feel convenient, some if you don't understand, just - count, no need to waste too much time on it.

*******************************

* Asterisk

Synthesate a parameter to the first product of the IFS's internal parameters (see the built-in variable section).

example:

#! / bin / sh

# starsig

ECHO $ ​​*

carried out:

[Foxman @ Foxman Bash] # starsig a b C D e f G

A b C D e f g

*******************************

@ AT symbol

With * asterisk class. Different are not to refer to IFS.

example:

#! / bin / sh

# Atsig

Echo $ @ @ @

carried out:

[Foxman @ foxman bash] # atsig a b C D e f g

A b C D e f g

*******************************

# 字

Expand the number of Positional parameters.

example:

#! / bin / sh

# Poundsig

Echo $ #

carried out

[Foxman @ Foxman Bash] # Poundsig A B C D e f G

Seduce

*******************************

? Question mark

Recently, the status of Foreground Pipeline.

*******************************

- minus

The option parameters for the recently executed Foreground Pipeline.

*******************************

$ Money money

The process ID itself.

[Foxman @ Foxman Bash] # PS AX | GREP BASH

1635 p1 s 0:00 / bin / bash

[Foxman @ Foxman Bash] # echo $$

1635

*******************************

! Shocking

The process ID of the recent background command.

*******************************

0 zero

A part of the Positional Parameters has been explained, it is the execution shell script itself. But if it is "Bash -C", $ 0 is set to the first parameter.

[Foxman @ Foxman Bash] # Echo $ 0

/ BIN / BASH

*******************************

_ Bottom line symbol

Show the last execution command.

[Foxman @ Foxman Bash] # echo $ _

Bash

*******************************

Built-in variable (shell variables)

Bash has many internal variables, like Path, Home, Env ... and more. These built-in variables will be specifically described in another section.

Function function

[function] name () {list;

The parameters of Function are Positional Paraments.

example

#! / bin / sh

Function func () {

Echo $ 1

ECHO $ ​​2

Return 1

}

Func "Hello" "Function"

Local variables can be declared by local.

Function can be export, using the shell of the next layer can be used.

Function can be delivered, without the limit of the number of layers.

Bash internal instruction set

The following commands, most of them have no use cases, you may not see if it doesn't want to mind. Before I join the example, I suggest you "man bash" and then actually operate once.

*******************************

: [arguments]

Do not do anything, except [arguments] to expand and some specific redirective operations.

Always return zero. Its usage is like TRUE.

*******************************

Filename [arguments]

Source FileName [arguments]

Read commands in FileName and execute.

You will find a lot in /etc/rc.d/*

. / xxxx

Directive, and XXXX's Permission is not executable. In fact, in TCSH, you need

Source / xxxx

To do the same directive.

Note "." The back is spaced (more ". /" Heel "./", not the same). FileName is a pure text file containing instructions, without Chmod 755 filename.

example

FILENAME: MY_SOURCE

DEV = LO

IP = 127.0.0.1

Netmask = 255.0.0.0

Broadcast = 127.255.255.255

IFCONFIG $ IP NETMASK $ NETMASK BROADCAST $ BROADCAST DEV $ DEV

Next

MY_SOURCE

or

Source my_source

This script can be implemented without "chmod 755 my_source"

*******************************

Alias ​​[name [= value] ...]

Nickname command

For example, if you are from the world from DOS, you are not used to UNIX instructions, you can use Alias ​​to modify to meet your habits.

example

Alias ​​ls = "ls --color"

Alias ​​Dir = "ls"

Alias ​​Cd .. = "CD .."

Alias ​​Copy = "CP -F" # dangerous, recomment, "cp -i" alias del = "rm -f" # dangerous, recomment, "rm -i"

Alias ​​Move = "MV-F" # dangerous, recomment, "mv -i"

Alias ​​md = "mkdir"

Alias ​​rd = "rmdir"

*******************************

UNALIAS [-A] [name ...]

Unalias cancels Alias ​​settings. "UNALIS -A" will be canceled all Alias.

example

UNALIAS COPY

*******************************

BG [Jobspec]

Put the specified task into the background if JobSpec is not specified, is within current.

*******************************

FG [Jobspec]

Put the specified task in the foreground, if JobSepc is not specified, it is within current.

*******************************

Jobs [-lnp] [jobspec ...]

The first form lists the tasks currently being working.

-l: In addition to listing the general information, it also lists the Process IDS.

-p: Only the Process ID of the work group "Summit" is listed.

-n: The state of the changed JOBS is listed.

If a given Jobspec, the output information is only the JobSpec.

The return value is zero unless there is an illegal option.

Jobs -x command [args ...]

If you use the second form (-X), Jobs replaces the specified Command and Args, and performs returning EXIT STATUS.

*******************************

Kill [-s sigspec | -sigspec] [PID | JOBSPEC] ...

Send SIGSPEC's signal to PID or JOBSPEC.

SigSpec can be a form or signal number in Sigkill / Kill. If SIGSPEC is Signal Name, the case is not related, and there is no SIG.

Kill -l [Signum]

List the signal name.

[Foxman @ Foxman Bash] # kill -l

1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL

5) Sigtrap 6) Sigiot 7) Sigbus 8) SIGFPE

9) Sigkill 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2

13) SIGPIPE 14) SIGALRM 15) Sigterm 17) Sigchld

18) Sigcont 19) Sigstop 20) Sigtstp 21) Sigttin

22) Sigttou 23) Sigurg 24) SIGXCPU 25) SIGXFSZ

26) SigvTalRM 27) SigProf 28) Sigwinch 29) SIGIO

30) Sigpwr

*******************************

Wait [n]

Waiting for the specified stroke and returns its end state. n can be a JobSpec or Process ID. If n is not specified, wait for all subunits, and return values ​​to zero. If n is a Job or Process that does not exist, it returns 127. Otherwise, the return value is the last job / process of Exit Status. *******************************

BIND [-M keymap] [-lvd] [-name]

BIND [-M keymap] -f filename

BIND [-M keyMap] keyseq: function-name

Show current readline's button and link function settings or gauges.

-m KeyMap: Set keyMap binding.

-l: Displays the name of all Readline function.

-v: Show current Function Name and Bindings.

-d: Displays Function Name and Bindings.

-f filename: Read Key Bindings from FileName.

-q function: Inquiry that button triggers function.

*******************************

Break [n]

Jump out the control back to the loop for / while / us. If you have a specified N, you jump out of the N layer. n must be greater than or equal to 1. If n is larger than the nest of the nest, all the circles will be jumped. The return value is zero.

*******************************

Continue [n]

Restore Control Backlines for / While / Until. If you have a specified N, the N layer is returned. n must be greater than or equal to 1. If N is larger than the neutral number, restore to the top. The return value is zero.

*******************************

Exit [n]

Leave the program. n is Exit Status.

*******************************

Return [n]

Use in Function. n is the return value, which is the same as EXIT STATUS.

*******************************

Builtin shell-builtin [arguments]

Execute the internal function. When you define the same instructions as the built-in function, this command can be used to perform the internal function.

*******************************

CD [DIR]

Replace the directory to DIR. If not specified, the directory specified by Home is set.

*******************************

Command [-PVV] Command [arg ...]

Specify with Command to cancel normal shell function. Only built-in commands will be executed. "-p" option, the way the search command is to use PATH. "-V" or "-v" option shows some simple descriptions of the command.

*******************************

Declare [-frxi] [name [= value]]

Typeset [-frxi] [name [= value]]]]

Declave parameters and set properties to them. If no name is given, each parameter is displayed.

-f: Use only the function name.

-r: Set Name to Readonly.

-x: Use the NAME to be used to follow-up environment.

-i: This parameter is set to Integer to use, which can be used for arithmetic representation.

When using " ", turn off the property.

*******************************

DIRS [-L] [ /-]

Shows the current record of memory. Directory can be operated by pushd / popd.

N: Display started records n.

-n: Record N of the display ends.

-l: Show more information.

*******************************

Echo [-nee] [arg ...] output Displays Args, separated by blank. The return value is always zero.

-n: Do not jump.

-e: Launch "" Interpretation of symbols.

-E: Cancel the ESC interpretation function.

"a": Alert (Bell), sounds.

"B": backspace, reverse.

"c": support trailing newline, no jump line.

"f": form feed, jump, jump.

"N": New line, new row.

"r": carriage return, go back to the line.

"T": Horizontal Tab, horizontal pitch.

"V": Vertical Tab, vertical job.

": Output" ".

"NNN": Output ASCII Code NNN (Eight Granses).

*******************************

Enable [-n] [-all] [name ...]

Start or turn off the built-in function command. Use "-n" to turn all specified commands, otherwise it is started. If only the "-n" parameter, it will display all the functions. If only "-all", it will display all built-in commands.

*******************************

Eval [arg ...]

Read Args and combine the args as a command, and then execute. Its return value becomes the return value of EVAL. If there is no parameters, EVAL returns true.

*******************************

Exec [[-] command [arguments]]]

When the command is executed, the command replaces the shell and there is no new Process generation. If the first parameter is "-", the shell will put "-" into the zero parameter and pass it to Command.

*******************************

Export [-nf] [name [= word]] ...

Export -P

Output Name to the environment, given the command to be used. "-f" option indicates that Name is a function. "-p" shows the name of all exports. "-n" removes Name.

*******************************

Set [--abefhkmnptuvxldchp] [-o Option] [arg ...]

-A: Automatically marks the variable as used by the back environment.

-b: Immediately report the termination background program.

-e: When a non-zero value is returned when the command (Simple-Command, see it), immediately jump out.

-f: Cancel Pathname Expansion.

-h: Find the location of the function command.

-k: All Keyword parameters are placed in the environment.

-M: Supervision mode.

-n: Read the command, but don't execute it. Can be used for syntax checks.

-p: Open Privileged mode.

-t: leave it immediately when a command is read and executed.

-u: When the parameter is expanded, the unset parameter is made as an error.

-v: List Shell Input Lines.

-x: After opening each Simple-Command, the BASH displays the expansion value on the PS4.

-l: Store and restore Name Binding in the For syntax.

-d: Close Hasing Command Search.

-C: Like the `noclobber =` ` See the internal parameter section.

-H: Start! Style History Substitution. -P: Do not follow Symbolic Links when using this command like CD.

-: "-", there is no parameters follow.

-: Specify all back parameters as a positional parameter.

-o Option-name: Option-name can be one of the following

AlLexport: The same as "-a".

BraceExpand: Start Brace Expansion. This is the internal setting.

Emacs: Use the Emacs-Style command to edit the interface.

Errexit: The same as "-e".

Histexpand: The same as "-h".

Ignoreeof: The effect is the same as `ignoreeof = 10`

Interactive-Commands: Allow # to be a solution.

Monitor: The same as "-m".

Noclobber: The same as "-C".

NOEXEC: The same as "-n".

Noglob: The same as "-f".

NOHASH: The same as "-d".

NOTIFY: The same as "-b".

Nounset: The same as "-u".

Physical: The same as "-p".

POSIX: Bash behavior is modified to POSIX 1003.2 standard.

Privileged: The same as "-p".

Verbose: The same as "-v".

VI: Edit the program using the Vi-style command.

Xtrace: The same as "-x".

*******************************

Unset [-fv] [name ...]

Remove parameters that are incorporated by Name. Pay attention to PATH, IFS, PPID, PS1, PS2, UID, and EUID cannot unset. If Random, Seconds, Lineno, HistCMD is subjected to the original meaning, which will begin later. The return value is TRUE unless Name cannot be subjected to Unset.

*******************************

FC [-e eName] [-NLR] [first] [last]

FC -S [PAT = rep] [cmd]

Correction command.

*******************************

Getopts optstring name [args]

Analytic location parameters.

*******************************

Help [Pattern]

Display assistance information.

*******************************

History [n]

History -rwan [filename]

When there is no parameter, the history of the down command is displayed. With parameter "n", the last N is displayed.

Other parameters are as follows:

-a: New "New History" to historical files.

-n: The reading has not read the record in history.

-r: Read filename as historical files and use it for current history.

-w: Write existing history into filename.

*******************************

Let arg [arg ...]

Arithmetic expression. Please refer to the arithmetic representation section.

*******************************

Local [Name [= Value] ...]

Generate a partial parameter. If used for Function, its effect is surrounded in function and its subunies.

*******************************

Logout

Leaving the Login Shell.

*******************************

POPD [ /- n]

Remove the directory stack. " N" Removes the above N, "- N" removes the following n. *******************************

Pushd [dir]

Pushd /- n

Add the directory to the top of the directory stack. " N" rotates the stack to make the nth directory become the top. "-n" rotates the stack to make the countdown nth directory becomes the top.

*******************************

PWD

List the absolute path of current working directory.

*******************************

Read [-r] [Name ...]

After reading a row, then the first word is set to the first Name, the second is set to the second Name, and so on. If there is no NAME in the parameter, the READ will set the value to reply. The return value is zero unless END-OF-FILE is encountered. If there is a "-r" option, "N" is considered as part of the line.

*******************************

Readonly [-f] [Name ...]

Readonly -P

Tag a given NAME as Readonly. If it is a "-f" option, the function is also marked as Readonly. "-p" will list all readonly's Name. "-" Cancel the remaining parameters.

*******************************

Shift [n]

Positional Parameters starts from N 1 ... will be changed to $ 1 .... If n is zero, there is no change. n If not given, it is within 1. n must be non-negative and less than or equal to $ #. If n is greater than $ #, there is no change. The return value is zero, unless n is greater than $ # or less than zero.

*******************************

SUSPEND [-F]

Pause this shell's execution until it receives a sigcont signal. The "-f" option is called the login shell. Don't complain, but it is still as paused. Returns status zero unless the shell is a login shell, and there is no "-f" option.

*******************************

Test expr

[EXPR]

We have said in some of Exit Status, no longer heavy.

*******************************

Times

List the accumulated user and system time of the shell and the process time executed from the shell, the return value is zero.

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

Trap [-l] [arg] [sigspec]

When a SIGSPEC signal is received, the arg command is executed. "-L" displays the signal name and number.

*******************************

TYPE [-All] [-type | -path] name [name ...]

In the case where there is no parameters, it will display the shell to interpret Name as a command. If there is "-type", it will display Alias, Keyword, Function, Builtin or File. If there is a "-Path" parameter, it will display the path of the command, if you can't find it, do not display anything. If there is a "-all" parameter, it will display all possible paths that can perform NAME. TYPE accepts "-a", "- t", "- p" as an abbreviation.

************************ ULIMIT [-SHACDFMSTPNUV [limited]]]

Ulimit provides the ability to obtain resource control for the shell.

-A: Report all restrictions.

-c: Set the maximum generated Core file.

-d: The maximum value of the Process's Data Segment.

-f: The largest file can be generated by this shell.

-m: Resident Set size maximum.

-s: Stack maximum.

-T: CPU Time maximum (in seconds).

-p: The maximum of Pipe Size in 512-Byte Blocks.

-N: The maximum File Descriptor can be opened.

-u: The maximum number of processs that a single user can use.

-v: The maximum virtual memory available value for this shell.

All items are units in 1024.

*******************************

umask [-s] [mode]

Set the user's File-Creation Mask to Mode. "-S" option prints Mask into symbolic forms.

BASH built-in parameters

PPID: The Bash's caller process ID.

PWD: Current work directory.

Oldpwd: Previous work directory.

Reply: When the read command does not have a parameter, it is located directly on the reply.

Uid: User ID.

EUID: Effective User ID.

Bash: The full path of Bash.

Bash_version: bash version.

SHLVL: When there is BASH per time, the number adds one.

Random: Every time this parameter is used, a mess is generated on random.

Seconds: From this shell, start up.

Lineno: The number of rows of Script.

Histcmd: History number.

Optarg: The last option parameter for getopts processing.

Optind: The next parameter number to be processed by getopts.

HostType: Machine type.

Ostype: The name of the operating system.

IFS: INTERNAL FIELD SEPARATOR.

PATH: Commands the search path.

Path = "/ usr / gnu / bin: / usr / local / bin: / usr / ucb: / bin: / usr / bin :."

Home: The current user's Home Directory;

CDPATH: The search path of the CD command.

ENV: If this parameter is set, it will execute the file name as an environment for each time there is a shell script being executed.

Mail: If this parameter is set, and the mailpath is not set, the BASH will notify the user when there is a letter coming in.

MailCheck: Set how long checks the email.

MailPath: a string of mail checkpread.

Mail_warning: If there is a setting, the message will be displayed after the message is read.

PS1: Tips message setting, fixed to "bash $". (Please see the message for details.)

PS2: Second prompt message setting, fixed to ">".

PS3: The prompt message used by the select command.

PS4: The prompt message setup used in the tracking, which is fixed to " ".

Histsize: Command history amount, fixed to 500.

Histfile: History file, internal order ~ / .bash_history.

HistfileSize: The history of the number of files, the maximum value of the file, the internal 500.

Opterr: If set to 1, BASH will display the error of getopts.

Prompt_command: If set, this value will be displayed before each execution command.

Ignoreeof: Use the EOF value as an input, which is within 10.

Tmout: If set to be larger than zero, this value is interpreted as an input waiting second number. If there is no input, it is not input.

FCEDIT: The internal representation of the FC command.

FIGNORE: Please refer to Readline for details.

Inputrc: Readline's Startup File, Contact ~ / .inputrc

NOTIFY: If set, Bash immediately reports the ended background program.

History_Control, HistControl: History.

Command_oriented_history: A multi-line directive is deposited.

GLOB_DOT_FILENAMES: If set, Bash will "." inclose into the file path.

ALLOW_NULL_GLOB_EXPANSION: If set, Bash allows the path to be called Null String.

Histchars: History.

NOLINKS: If you are set, you will not follow Symbolic Links when you execute the instruction.

Hostname_completion_file, hostfile: contains the same format as / etc / hosts.

NOCLOBBER: If set, BASH does not overwrite any files operating by ">", "> &", ".

Auto_RESUME: Please see the task control section.

NO_EXIT_ON_FAILED_EXEC: If this value exists, the non-interactive shell will not jump out of the failure of EXEC.

CDABLE_VARS: If started, and the CD command does not find the directory, switchable to the specified directory specified by the parameter.

Tip symbol

Bash uses PS1 to PS4 to display prompt symbols, and its format is as follows:

*******************************

T: Today.

D: The date is now.

N: New line.

S: the name of the shell.

W: Currently a catalog.

W: Currently working the full path of the directory.

u: User name.

H: hostname.

#: The number of this command.

!: Historical number.

$: If EUID is 0, then #, otherwise $.

NnN: Eight-carrying characters.

"" "" Symbol.

[: Start a sequence that is not printable.

]: End a sequence of non-printed characters.

Arithmetic expression

-

! ~

* /%

-

>

=

==! =

&

^

|

&&&&

||

= * = / =% = = - => = & = ^ = | =

Heavy guide Redirection

>

>>

1>

.

.

grammar

Simple Command

Pipelines

Lists

(List)

{list;}

转载请注明原文地址:https://www.9cbs.com/read-71235.html

New Post(0)