Shell Programming (1)

xiaoxiao2021-03-06  53

Why do shell programming

In the Linux system, although there are a wide variety of graphical interface tools, Sell is still a very flexible tool. Shell is not only a collection of commands, but also a great programming language. You can use shell to automate a large number of tasks, Shell is particularly good at system management tasks, especially for more important tasks that are more important, maintainability, and portability.

Below, let's take a look at how shell works:

Create a script

There are many different shells in Linux, but usually we use Bash (Bash Again Shell) for shell programming, because Bash is free and easy to use. So the scripts provided in this article are bash (but in most cases, these scripts can also run in Bash's big sister, Bourne Shell).

Like other languages, we use any of the text editor, such as Nedit, Kedit, Emacs, VI

Wait, write our shell programs.

The program must start with the following row (must be in the first line of the file):

#! / bin / sh

Symbol #! It is used to tell the system that the parameters behind it are used to execute the file. In this example we use / bin / sh to execute the program.

When editing the script, if you want to execute this script, you must also make it executable.

To make the script executable:

CHMOD X FileName

Then you can perform your script by entering: ./filename.

Comment

When the shell programming is performed, the sentence is indicated by the sentence until the end of this line. We sincerely recommend that you use comments in the program. If you use a comment, even if you don't use this script, you can understand the role and working principle of this script in a short period of time.

variable

You must use a variable in other programming languages. In the shell program, all variables are composed of strings and you do not need to declare variables. To assign a variable, you can write this:

Variable name = value

Removing the variable value can add a dollar sign ($) in front of the variable:

#! / bin / sh

# Assign the variable:

A = "Hello World"

# Now print the contents of the variable A:

echo "a is:"

Echo $ A

Enter the above content in your editor and save it as a file first. After executing CHMOD X First

Make it executable and finally entered ./first executes the script.

This script will output:

A is:

Hello World

Sometimes the variable name is easy to confuse with other words, such as:

Num = 2

echo "this is the $ numnd"

This will not print "this is the 2nd", but only print "this is the", because the shell will go to search the variable Numnd value, but there is no value when this variable is. You can use the curly brackets to tell Shell We want to print NUM variables:

Num = 2

echo "this is the $ {num} nd"

This will print: this is the 2nd

Many variables are automatically set by the system, which will be discussed when these variables are used later.

If you need to process mathematical expressions, you need to use programs such as EXPR (see below).

In addition to the general SHELL variables only in the program, there are environment variables. Variables processed by the export keyword are called environment variables. We don't discuss environment variables because usually use environment variables only in the login script. Shell command and process control

The three types of commands can be used in the shell script:

1) UNIX command:

Although any UNIX command can be used in the shell script, it is still a relatively more common command. These commands are usually used for files and text operations.

Common command syntax and function

Echo "Some Text": Print the text content on the screen

Ls: file list

Wc -l filewc -w filewc -c file: calculates the number of characters in the number of words in the file calculation file

CP Sourcefile Destfile: File Copy

MV OldName NewName: Rename file or mobile file

RM File: Delete files

GREP 'PATTERN' FILE: Search strings in the file such as: grep 'searchString' file.txt

Cut -b colnum file: Specify the scope of the file you want to display, and output them to standard output devices such as: Output Each line of the 5th to nine characters cut -b5-9 file.txt must not confuse with the CAT command This is two completely different orders

CAT file.txt: Output file content to standard output device (screen)

File Somefile: get file type

Read var: prompt user input and assign the input to the variable

Sort file.txt: Sort by rows in file.txt files

UNIQ: Deleting the rows of text files such as Sort File.txt | Uniq

EXPR: Mathematical operations EXAMPLE: Add 2 and 3EXPR 2 " " 3

Find: Search files, such as: Search for Find. -name FileName -Print according to the file name

TEE: Output data to standard output devices (screens) and files such as: SomeCommand | Tee Outfile

BaseName File: Returns a file name that does not contain the path, such as: BaseName / Bin / Tux will return TUX

DirName File: Returns the path where the file is located, for example: dirname / bin / tux will return / bin

Head file: Print the text file a few lines

TAIL FILE: Print the end of the text file

Sed: SED is a basic lookup replacement. You can read text from standard inputs (such as command pipes), and output the result to the standard output (screen). This command is searched using a regular expression (see). Don't confuse the wildcards in the shell. For example: replace LinuxFocus for Linuxfocus: cat text.file | Sed 's / linuxfocus / linuxfocus /'> NewText.file

AWK: AWK is used to extract fields from a text file. By default, the field split is a space, you can use -f to specify other splites. Cat file.txt | awk -f, '{print $ 1, "$ 3}" We use, as a field split, and print one and the third field. If the file is as follows: Adam Bor, 34, Indiakerry Miller, 22, USA command output is: Adam Bor, Indiakerry Miller, USA

2) Concept: Pipe, Redirection and Backtick

These are not system commands, but they are really important. The pipe (|) uses the output of a command as the input of another command.

GREP "Hello" file.txt | wc -l

Searching in file.txt and contains rows with "Hello" and calculates the number of rows.

The output of the grep command here as the input of the WC command. Of course you can use multiple commands.

Redirection: Output the result of the command to the file instead of the standard output (screen).

> Write files and overwrites old files

>> Add to the end of the file, keep the old file content.

Short short slanting

Using the short-short slash can use the output of a command as a command line argument for another command.

command:

Find. -mtime -1 -Type F -PRINT

Used to find files that have been modified within the last 24 hours (-mtime -2 indicating the past 48 hours). If you want to make all the files you find a package, you can use the following script:

#! / bin / sh

# The Ticks Are Backts (`) Not Normal Quotes:

Tar -zcvf lastmod.tar.gz `Find. -mtime -1 -type f -print`

3) Process control

"IF" expression If the condition is true, the part is executed behind:

IF ....; then

....

Elif ....; then

....

Else

....

Fi

In most cases, the test command can be used to test the condition. For example, you can compare the string, determine if the file exists and whether it is readable, etc.

Usually "[]" is used to represent the condition test. Note that the space here is important. To ensure the space of square brackets.

[-f "somefile"]: Judgment is a file

[-x "/ bin / ls"]: Judging whether the / bin / ls exists and has executable permissions

[-n "$ var"]: Judging whether the $ var variable is worth

["$ a" = "$ b"]: Decades if $ A and $ B are equal

Performing Man Test You can view all test expressions that can be compared and determined.

Perform the following script directly:

#! / bin / sh

IF [$ shell "=" / bin / bash "]; then

Echo "Your login shell is the bash (bourne again shell)

Else

Echo "Your Login Shell Is Not Bash But $ Shell"

Fi

Variable $ shell contains the name of the login shell, and we compare it with / bin / bash.

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

New Post(0)