Linux script shell command

Source: Internet
Author: User

Http:// OS .51cto.com/art/200910/158857.htm

This article describes the basic knowledge of Linux script shell commands in detail, including: 1, 2, comment, 3, variable 4, environment variable, 5, Linux script shell commands, and process control.

Before giving you a detailed introduction to Linux compilation, let's first understand the Linux script shell command. The following explains how to use arbitrary Unix commands in shell scripts.

Basic syntax Introduction

1. Start

ProgramThe following line must start (the first line of the file must start ):
#! /Bin/sh
Symbol #! The parameter used to tell the system that the program is used to execute the file. In this example, we use/bin/sh to execute the program.
When editing a script, you must make it executable if you want to execute it.
To make the script executable:
Compile chmod + x filename to run it with./filename.

2. Notes

During shell programming, a sentence starting with # represents a comment until the end of this line. We sincerely recommend that you use annotations in your program. If you have used annotations, you can understand the role and working principle of the script in a short time even if the script is not used for a long time.

3. Variables

In otherProgramming LanguageYou must use the variable. In Linux script 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 as follows:
#! /Bin/sh
# Assign values to variables:
A = "Hello World"
# Print the content of variable:
Echo "A is :"
Echo $
Sometimes the variable name is easily confused with other words, such:
Num = 2
Echo "this is the $ numnd"
This does not print "this is the 2nd", but only prints "this is the", because shell will search for the value of the variable numnd, but there is no value for this variable. We can use curly braces to tell shell that we want to print the num variable:
Num = 2
Echo "this is the $ {num} Nd"
This will print: This is the 2nd

4. Environment Variables

Variables processed by the Export keyword are called environment variables. We will not discuss environment variables, because generally, only environment variables are used in the login script.

5 Linux script shell commands and Process Control

You can run the following command in a shell script:
Unix command:
Although any Unix command can be used in shell scripts, some more common commands are used. These commands are usually used for file and text operations. Common command syntax and functions
◆ Echo "some text": print the text on the screen.
◆ Ls: file list
◆ WC-l filewc-W filewc-C file: calculate the number of file lines. Calculate the number of words in the file. Calculate the number of characters in the file.
◆ CP sourcefile destfile: copy a file
◆ MV oldname newname: rename a file or move a file
◆ RM file: delete an object
◆ Grep 'pattern' file: search for a string in the file, for example, grep 'searchstring' file.txt.
◆ Cut-B colnum file: specify the content range of the file to be displayed, and output them to the standard output device, for example: output 5th to 9th characters in each line cut-b5-9 file.txt do not confuse with Cat command, this is two completely different commands
◆ Cat file.txt: output file content to the standard output device (screen)
◆ Somefile: Obtain the file type.
◆ Read var: prompt the user to input and assign the input value to the variable
◆ Sort file.txt: sorts the rows in the file.txt file.
◆ Uniq: Delete the columns in a text file, for example, sort file.txt | uniq
◆ Expr: perform mathematical operations example: Add 2 and 3 expr 2 "+" 3
◆ Find: search for a file. For example, search for find.-Name filename-print based on the file name.
◆ Tee: outputs data to the standard output device (screen) and files such as: somecommand | tee OUTFILE
◆ Basename file: the file name that does not contain the path is returned. For example, basename/bin/tux will return tux.
◆ Dirname file: Path of the returned file, for example, dirname/bin/tux,/bin is returned.
◆ Head file: print the beginning of a text file.
◆ Tail file: print the last few lines of a text file.
◆ Sed: SED is a basic search replacement program.

You can read text from standard input (such as command pipeline) and output the results to standard output (screen ). This command uses a regular expression (see references) for search. Do not confuse with wildcards in shell. For example, replace linuxfocus with linuxfocus: CAT text. File | SED's/linuxfocus/'> newtext. fileawk: awk to extract fields from text files. By default, the field delimiter is a space. You can use-F to specify other separators. Cat file.txt | awk-F, '{print $1 "," $3}', which is used here as a field delimiter and prints both the First and Third fields. If the file contains the following content: Adam Bor, 34, indiakerry Miller, 22, and USA, the output result is Adam Bor, indiakerry Miller, and USA. The preceding section describes the Linux script shell commands.

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.