Linux Shell Learning Notes __linux

Source: Internet
Author: User
Tags chmod file permissions

Recently, writing shell scripts under Linux is almost as close to learning as writing. Record some learning experience here.

a) The shell's quotation marks.

the shell's quotes have single quotes ', double quotes ', and inverted quotes '.

A character characters, enclosed in single quotes, appears as a normal character. Special characters, which are enclosed in single quotes, also lose their original meaning and are interpreted only as ordinary characters.

Characters enclosed in double quotes, except that $, inverted quotes ('), and backslashes (\) still retain their special features, the remaining characters are treated as enclosed. "$" denotes a variable substitution, that is, the value of the variable that is subsequently specified instead of the $ and variable; inverted quotes indicate a command substitution, and only if the character after "\" is the following Fu Zhi, "\" is the escape character, which is: "$", "'", "", "\" or line breaks. The escape character tells the shell not to treat the character behind it specifically, just as a normal character.

The quoted string is interpreted by the shell as the command line, and when executed, the shell executes the command line first and replaces the entire quote section with its standard output result.

ii) directory permissions.

Access to a file or directory is divided into read-only (R), write-only (W), and executable (x) three.

When we use the Ls-l command, we will see the following


The first column we see is the-rw-r--r--character, which represents the permissions of the current file.

The horizontal line represents the empty license. Note that there are 10 locations here. The first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a horizontal line, it represents a file that is not a directory. If it is D, the representation is a directory. Except for the first position, each successive three represents a permission, and the first consecutive three characters represent the permissions of the file owner, for example, "rw-" here means that the file has permission to read and write. The second consecutive three-character "r--" means that the file has the permissions that the group has, where "r--" means read-only. The following three consecutive characters represent the permissions of all users and are read-only.

In the permission settings, in order to simplify this permission setting, we usually represent R, W, X in 4, 2, and 1, and then add them together to represent the permissions of the file. For example, the permissions of the two files above are 644. Another example is a file with a permission of 754, which means that the owner of the file has readable writable executable permissions, the group has read and execute permissions, and other users have Read permissions.

We can modify the permissions of a file through chmod. Here are two ways, the first is the number setting, we just need to run the following command


Or use the text setting method, as follows


The u+x,g-w-r,o-x-r in the text setting means setting permissions, where U,g,o represents the owner of the file, the group of users, the other users, and the +,-adds and deletes permissions.

Of course, using chmod to modify file permissions can only be superuser and the owner of the file.

In addition, we can also user chgrp modify the group to which the file belongs, chown Modify the user or group to which the file belongs.

Syntax: chgrp [options] User group file

Syntax: chown [options] User or group file


III) variables.

The variables in the shell do not need to be declared, the value of the assignment directly [variable name = value] is OK. Like what

Num=1
Me=fly

There are several things to note about variable references in the shell, such as when you use the following statement in a script

echo "I ' m ${me}baby"
It is not possible to use $mebaby at this time because the shell interprets it to find the Mebaby variable. And you only have me as a variable.

In the shell to implement the operation of variables, you need to pay attention to several places, look at the results of the following diagram


From the results above we can see that the use of the shell variable for the operation of the line, you can use the brackets, double parentheses, and the expr operation keyword.

In addition to the basic variables above, there are some special parameters in the shell. such as the following parameters:

 $0 = Shell name or shell script Name $ = First Shell parameter ...   $9 = nineth Shell parameter $# = number of positional parameters     "$*" = "$ $ $ .. $n "To display all parameters passed to the script in a single string.
Unlike positional variables, this option argument can be more than 9.   "$@" = "$" "$" "$" "$".
$n, #相同 with $, but with quotes and returns each parameter in quotation marks.
 $# the number of arguments passed to the script.
The  $-displays the current options used by the shell, as is the SET command function.  $?
= The exit status of the most recently executed command, 0 indicates no errors, and any other value indicates an error.
 $$ = pid for the current shell script.  $! = PID for the most recently started background job.      form           If set var   if not set var   ${var:-string}     $var            string   ${var:+string}     string       &NBS P  null   ${var:=string}     $var            string (and execute var = string)    ${var:?string}     $var             return string then exit     form &NBSP ;             result    ${var%suffix}     Delete minimum match mode at var end    ${vAr%%suffix}     Remove the maximum matching pattern at the end of Var    ${var#suffix}     Delete the minimum matching mode at the beginning of var    ${var#
 #suffix}     Remove the maximum matching mode at the beginning of Var

iv. conditions and cycles.

Like other programming languages, the shell contains if conditional statements, While,for,until loops, and case and select statements. First we introduce the IF conditional statement

If statement structure [IF/THEN/ELIF/ELSE/FI], where [] there is a space between the variables.

If [$y-eq 1];then
	y=$ (($y + 1))
else
	Y=1
fi
Shell commands, which can be separated by semicolons or by line breaks. If you want to write multiple commands on one line, you can pass "';" Segmentation.

num=3;if [$num-eq 1]; then echo 1; elif [$num-eq 2]; then echo 2;else echo 3;fi;
In an IF condition statement, you can size a numeric string, match comparisons, and judge the permissions, presence, or absence of a folder or file.

v) function.

six) time.

Seven) The use of GREP,SED,AWK,TR and other tools.

We start with the example, the following has a file in F of the domain name to take out and sort, the contents are as follows,

http://www.fly_sky520.org/index.html

http://www.fly_sky520.org/1.html

http://post.fly_sky520.org/ index.html

http://mp3.fly_sky520.org/index.html

http://www.fly_sky520.org/3.html

http://post.fly_ Sky520.org/2.html
* Note that there are blank lines between the lines. Let's see what I'm doing here.

1 First use the grep and awk tools.

The method first uses GREP-V to fetch non-empty row data, the-v parameter is the meaning of reverse retrieval. The effect is as follows:


Then use the Awk tool to take the third parameter that is divided by the "/" delimiter, which is the domain name.


In the end, sort is used and the nuiq-c count


So the first complete command is

Cat F | Grep-v ' ^$ ' |awk-f/' {print $} ' |sort|uniq-c

2) using TR and grep tools

Take advantage of the substitution function of the TR tool first


Then use the grep tool to find the items that contain the domain name


Last sort Count


So the complete command is

Cat f |tr "\/" "\ n" | grep Fly | Sort | Uniq-c


3 use cut and TR tools

First use the Cut tool to remove the domain name

Cut-d/-f3 means that the contents of the file are delimited by "/" and the third part is displayed. -D is the meaning of the custom delimiter, the default delimiter is tab, and-F is the display area

We then use the TR tool to remove the empty rows


Last sort Count


So the final method is

cut-d/-f3 f|tr-s ' \ n ' |sort | Uniq-c

4 Using the SED tool

First Use SED tool to remove blank lines


The meaning of D for deletion in the SED command. A regular expression ^$ represents a blank line. And then use SED's ' s///g ' character replacement function


Last or the sort count



Sed is quite powerful editing, for example, I'm going to change the name of "name =" Fly "" in configuration file F to Sky. I just need to enter the following command


Where-I is the option to modify the contents of the file directly, the operation of this command is to find the name line by/name/first, and then modify the corresponding value.

Here are some examples of sed

6. Instance deletion: D command $ sed ' 2, $d ' example-----Delete the second line of example file to all rows at the end. 
$ sed ' $d ' example-----Delete the last line of example file. Replace: The s command $ Sed-n ' s/^test/mytest/p ' example-----(-N) option is used with the P flag to print only those occurrences that have been replaced.  
In other words, if test at the beginning of a line is replaced with mytest, it is printed. $ sed ' s/^192.168.0.1/&localhost/' example-----& symbol represents the part found in the replacement string.
All rows beginning with 192.168.0.1 are replaced with localhost, which becomes 192.168.0.1localhost. 
$ Sed-n ' s/\ (love\) able/\1rs/p ' Example-----Love is marked as 1, all loveable are replaced with lovers, and the replaced rows are printed. $ sed ' s#10#100#g ' example-----no matter what character, followed by the S command is considered a new delimiter, so, "#" Here is the separator, instead of the default "/" separator.  
means to replace all 10 with 100. 
Range of selected rows: Comma $ Sed-n '/test/,/check/p ' example-----all rows that are within the scope determined by the template test and check are printed.
$ Sed-n ' 5,/^test/p ' example-----print all rows from line fifth to the first line that contains the start of test.  
$ sed '/test/,/check/s/$/sed test/' example-----for the rows between the template test and West, the end of each line is replaced with the string sed test. Multi-point edit: E command $ Sed-e ' 1,5d '-e ' s/test/check/' example-----(-e) option allows multiple commands to be executed on the same line. As the example shows, the first command deletes 1 to 5 rows, and the second command replaces test with check. The order in which the commands are executed has an effect on the results.  
If all two commands are replacement commands, the first substitution command affects the result of the second replacement command. $ sed--expression= ' s/test/check/'--expression= '/love/d ' Example-----a better order than-E is--expression.  
It can assign a value to a SED expression.  
Read from File: R command $ The contents of sed '/test/r file ' example-----file are read in, displayed after the row that matches the test, and if multiple rows are matched, the contents of file are displayed under all matching rows.  
Write file: w command $ Sed-n '/test/w file ' example-----in example all rows containing test are written to file.   
Append command: A command $ sed '/^test/a\\--->this is a example ' example<-----' This is a example ' was appended to the line beginning with test, and SED requires command A to have a backslash behind it.  
Insert: I command $ sed '/test/i\\ new line-------------------------' example if test is matched, the text after the backslash is inserted in front of the matching row.  
Next: N command $ sed '/test/{n; s/aa/bb/} ' example-----if test is matched, move to the next line in the matching row, replace the AA of the line, change to BB, print the line, and then continue.  
Variants: Y command $ sed ' 1,10y/abcde/abcde/' example-----convert all ABCDE in 1--10 line to uppercase, note that regular expression metacharacters cannot use this command.  
Exit: Q command $ sed ' 10q ' example-----print out after line 10th, exit sed. Keep and get: H command and G command $ Sed-e '/test/h '-e ' $G example-----when the SED processes files, each row is saved in a temporary buffer called the pattern space, unless the row is deleted or the output is canceled, all rows processed will be Print on the screen. The mode space is then emptied and stored in a new line waiting to be processed. In this example, when the row that matches the test is found, it is stored in the mode space, and the H command copies it and stores it in a special buffer called the retention buffer. The second statement means that when the last row is reached, the G command takes out the line that holds the buffer and then puts it back in the pattern space and appends it to the end of the line that already exists in the pattern space. In this case, append to the last line.。  
In simple terms, any row containing test is copied and appended to the end of the file. Keep and swap: H command and x command $ Sed-e '/test/h '-e '/check/x ' example-----Interchange mode space and keep buffer contents.  
 That is, the line that contains test and check is swapped.
The above instance content is excerpted from Netizen Icyheart's post, address is http://www.iteye.com/topic/587673.

(viii) Timed tasks.

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.