Linux_Shell programming Learning

Source: Internet
Author: User

The following is a summary of linux_Shell programming. If you need it, you can check it out. Why is shell programming in Linux? Although there are various graphical interface tools, kernel is still a very flexible tool. Shell is not only a collection of commands, but also a very good programming language. You can use shell to automate a large number of tasks. shell is especially good at system management tasks, especially for tasks that are easier to use, maintainability, and portability than efficiency. Next, let's take a look at how shell works: There are a lot of different shells in a script Linux, but usually we use bash (bourne again shell) for shell programming, bash is free and easy to use. So in this article, all the scripts provided by the author use bash (but in most cases, these scripts can also be run in bash's big sister, bourne shell ). Like other languages, we can use any text editor, such as nedit, kedit, emacs, and vi to compile our shell program. The program must start with the following line (the first line of the file must be located ):#! /Bin/sh #! 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: chmod + x filename, then you can run your script by entering:./filename. 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. Variables must be used 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: variable name = value to retrieve the variable value. You can add a dollar sign ($) before the variable :#! /Bin/sh # assign a value to the variable: a = "hello world" # print the content of variable A now: echo "a is: "echo $ a input the above content in your editor and save it as a file first. Then run chmod + x first. Make it executable, and enter./first to execute the script. This script will output: A is: hello world. Sometimes the variable name is easily confused with other words, for example: num = 2 echo "this is the $ numnd" which does not print "this is the 2nd", but only prints "this is ", because shell will search for the value of the variable numnd, but this variable does not have a value. We can use curly braces to tell shell that we want to print the num variable: num = 2 echo "this is the $ nd" which will print: this is the 2nd has many variables automatically set by the system, which will be discussed later when using these variables. If you need to process mathematical expressions, you need to use programs such as expr (see below ). In addition to the shell variables that are generally valid in the program, there are also 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. Shell commands and flow control can use three types of commands in shell scripts: 1) Unix Commands: although any unix commands can be used in shell scripts, however, some commands are more commonly used. These commands are usually used for file and text operations. Common command syntax and function: echo "some text": print the text on the screen. Ls: file list. Wc-l file wc-w file wc-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: searches for strings in a file, for example, grep 'searchstring' file.txt cut-B colnum file: specifies 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-B 5-9 file.txt. Do not confuse them with the cat command. This is a completely different command. Cat file.txt: output file content to the standard output device (screen. File somefile: Obtain the file type. Read var: prompts the user to enter the input and assigns 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: returns a file name that does not contain a path. For example, basename/bin/tux returns tux. Dirname file: the path of the returned file, for example, dirname/bin/tux, returns/bin. Head file: prints the first few lines of a text file. Tail file: number of rows at the end of the 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/LinuxFocus/'> newtext. file. Awk: awk is used 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 ","}', which is used here as a field delimiter and prints the first and third fields at the same time. If the file contains the following content: Adam Bor, 34, IndiaKerry Miller, 22, USA command output result: Adam Bor, IndiaKerry Miller.2) concept: Pipeline, redirection, and backtick, these are not system commands, but they are really important. The pipeline (|) uses the output of a command as the input of another command. Grep "hello" file.txt | wc -lsearches for rows containing "hello" in file.txt and calculates the number of rows. Here, the grep command output serves as the wc command input. Of course, you can use multiple commands. Redirection: output the command result to a file instead of a standard output (screen).> Write the file and overwrite the old file.> Add it to the end of the file to keep the content of the old file. Use a backslash to output a command as a command line parameter of another command. Command: find.-mtime-1-type f-print is used to find the files modified in the past 24 hours (-mtime-2 indicates the past 48 hours. If you want to pack all the searched files, use the following Script :#! /Bin/sh # The ticks are backticks (') 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 after then is executed: if ....; then

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.