Linux. sh file syntax

Source: Internet
Author: User
Linux sh file syntax introduction: 1 The program must start with the following line (must begin with the first line of the file ):! Binsh symbol! The parameter used to tell the system that the program is used to execute the file. In this example, we use binsh to execute the program. When the script is compiled, the. sh file syntax in linux is described as follows: 1. the program must start with the following line (the first line of the file must start ):#! /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 writing a script, you must make it executable if you want to execute the script. To make the script executable: compile chmod + x filename for use. /filename to run the 2 annotation. During shell programming, the sentence starting with # indicates the annotation 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 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 :#! /Bin/sh # assign a value to the variable: a = "hello world" # print the content of variable A now: echo "a is: "echo $ a 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 ", and only print "this is the", because shell will search for the value of the variable numnd, but this variable has no value. We can use curly braces to tell shell that we want to print the num variable: num = 2 echo "this is the $ {num} nd" which will print: this is the 2nd4 environment variable. the variable processed by the export keyword is called the environment variable. We will not discuss environment variables, because generally, only environment variables are used in the login script. 5. three types of commands can be used 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 functions echo "some text": print text on the screen ls: file list wc-l filewc-w filewc-c file: calculation of the number of file lines calculation of the number of words in the file calculation of the number of characters in the file cp sourcefile destfile: file copy mv oldname newname: rename the file or move the file rm file: delete the file grep 'pattern' file: search for a string in a file, such as 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 per line cut-b5-9 file.txt do not confuse with cat command, it is two completely different command cat file.txt: output file content to standard output device (screen) file somefile: get the file type read var: Prompt user input, and assign the input value to the variable sort file.txt: sort the rows in the file.txt file uniq: delete the columns that appear in the text file, for Example, sort file.txt | uniqexpr: perform mathematical operations Example: add 2 and 3 expr 2 "+" 3 find: search for a file, for example, find by file name. -name filename-printtee: outputs data to the standard output device (screen) and files such as: somecommand | tee outfilebasename file: returns a file name without a path such: basename/bin/tux will return tuxdirname file: the path of the returned file, for example, dirname/bin/tux, will return/binhead file: print the lines at the beginning of the text file: print the lines at the end of the text file sed: Sed is a basic search replacement Change the 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. fileawk: awk to extract fields from text files. By default, the field delimiter is a space. you can use-F to specify other separators. Catfile.txt | awk-F, '{print $1 "," $3}' 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, IndiaKerryMiller, 22, USA command output result: Adam Bor, IndiaKerry Miller, USA2) concept: Pipeline, redirection and backtick 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. A backslash can be used 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, you can use the following linux script :#! /Bin/sh # The ticks are backticks (') not normal quotes ('): tar-zcvf lastmod.tar.gz 'find. -mtime-1-type f-print '3) Process Control 1. if the "if" expression is true, the part after then is executed: if ....; Then .... Elif ....; Then .... Else .... In most cases, you can use the test command to test the conditions. For example, you can compare strings, determine whether a file exists, and whether the file is readable... "[]" Is usually used to represent a conditional test. Note that spaces are important. Make sure that the square brackets have spaces. [-F "somefile"]: determines whether it is a file [-x "/bin/ls"]: determine whether/bin/ls exists and has the executable permission [-n "$ var"]: determines whether the $ var variable has a value ["$ a" = "$ B"]: determine whether $ a and $ B are equal. execute man test to view all types of test expressions that can be compared and judged. Directly execute the following script :#! /Bin/shif ["$ SHELL" = "/bin/bash"]; thenecho "your login shell is the bash (bourne again shell) "elseecho" your login shell is not bash but $ SHELL "fi variable $ SHELL contains the name of the logon shell. we compared it with/bin/bash. If you are familiar with C, you may like the following expressions: [-f "/etc/shadow"] & echo "This computer uses shadow passwors" here & is a shortcut operator. if the expression on the left is true, execute the statement on the right. You can also think of it as a logical operation. In the above example, if the/etc/shadow file exists, print "This computer uses shadow passwors ". Similarly, operations (|) are also available in shell programming. Here is an example :#! /Bin/shmailfolder =/var/spool/mail/james [-r "$ mailfolder"] ''{echo" Can not read $ mailfolder "; exit 1 ;} echo "$ mailfolder has mail from:" grep "^ From" $ mailfolder the script first checks whether mailfolder is readable. If it is readable, the "From" line in the file is printed. If it is not readable or the operation takes effect, print the error message and exit the script. There is a problem here, that is, we must have two commands: ◆ print the error message ◆ exit the program we use curly brackets to put the two commands together as an anonymous function for use as a command. General functions will be mentioned below. We can use the if expression to do anything without the sum or operator, but it is much more convenient to use the sum or operator.
 
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.