Linux Scripting Tutorial (i) Basic syntax

Source: Internet
Author: User

1, the beginning

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

#!/bin/sh

The symbolic #! is used to tell the system that the parameters behind it are the programs used to execute the file. In this example we use/BIN/SH to execute the program.

When you edit a good script, you must also make it executable if you want to execute the script.

To make the script executable:

Compile chmod +x filename so you can use it. /filename to run

2, Notes

In shell programming, comments are expressed in sentences that begin with # until the end of the line. We sincerely recommend that you use annotations in your programs.

If you use annotations, you can understand how the script works and work in a very short period of time, even if you haven't used it for a very long time.

3, variable

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

#!/bin/sh

#对变量赋值:

A= "Hello World"

# now print the contents of variable a:

echo "A is:"

Echo $a

Sometimes variable names can easily be confused with other words, such as:

num=2

echo "This is the $NUMND"

This does not print out "This is the 2nd" and only prints "This is the" because the shell searches for the value of the variable numnd, but the variable has no value. You can use curly braces to tell the shell what we want to print is the NUM variable:

num=2

echo "This is the ${num}nd"

This will print: This is the 2nd

4. Environment variable

Variables processed by the EXPORT keyword are called environment variables. We do not discuss environment variables because, in general, environment variables are used only in logon scripts.

5. Shell command and Process Control

There are three types of commands that can be used in a shell script:

(1) Unix command:

Although arbitrary UNIX commands can be used in shell scripts, there are some relatively more commonly used commands. These commands are usually used for file and text operations.

Common command syntax and functions:

echo "Some text": Print text content on screen

LS: File list

Wc–l file wc-w file Wc-c file:

Calculate the number of rows in a file count the number of characters in a file

CP sourcefile destfile: File copy

MV Oldname newname: Renaming a file or moving a file

RM file: Deleting files

grep ' pattern ' file: Search for strings within a file for example: grep ' searchstring ' file.txt

This paper url:http://www.bianceng.cn/os/linux/201410/45611.htm

Cut-b colnum File: Specifies the range of files to display and outputs them to standard output devices such as: Output the 5th to 9th characters per line cut-b5-9 file.txt do not confuse with cat commands, which are two completely different commands.

Cat file.txt: Output file contents to standard output device (screen)

File somefile: Getting files Type

Read Var: Prompts the user for input and assigns the input to the variable

Sort file.txt: Sort the rows in a file.txt file

Uniq: Delete the rows that appear in a text file for example: sort file.txt | Uniq

Expr: Mathematical operations Example:add 2 and 3expr 2 "+" 3

Find: Search for files such as: Search for Find by filename. -name Filename-print

Tee: Output data to standard output devices (screens) and files such as: Somecommand | Tee outfile

basename file: Returns a filename that does not contain a path such as: Basename/bin/tux will return Tux

DirName file: Returns the path where the files are located for example: Dirname/bin/tux will return/bin

Head file: Print the first few lines of a text file

Tail File: Print a few lines at the end of a text file

Sed:sed is a basic search and replace program. You can read text from standard input, such as a command pipe, and output the results to the standard output (screen). The command searches using a regular expression (see Reference). Do not confuse with wildcard characters in the shell. For example: Replace the Linuxfocus with the

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 separator is a space, and you can specify additional delimiters using-F.

Cat File.txt | Awk-f, ' {print $ ', ' $} ' here we use, as a field separator, to print both the first and third fields. If the contents of the file are as follows: Adam Bor, Indiakerry Miller, USA

The result of the command output is: Adam Bor, Indiakerry Miller, USA

(2) Concepts: Pipelines, redirects and Backtick

These are not system commands, but they are really important.

Pipe (|) The output of one command as input to another command.

grep "Hello" file.txt | Wc-l

Searches the file.txt for a row containing "Hello" and calculates its number of rows.

Here the output of the grep command is used as input to the WC command. Of course you can use more than one command.

Redirect: Outputs the results of a command to a file instead of the standard output (screen).

> Write files and overwrite old files

Add to the end of the file and keep the old file content.

Backslash: Using a backslash, you can use the output of one command as a command-line argument for another command.

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.