I learned shell program records

Source: Internet
Author: User
1. Remove the file suffix ". Sh" from the current directory"
For I in 'ls .'
Do
MV-V $ I 'echo $ {I %. Sh }'
Done

2. When the column file directory is in the shell, the file name variable with spaces is not treated as two parameters.

Ls-1 directory path | while read line
Do
Echo $ line
Done

Explanation:-1 after LS is used to display only one row of the file, and then read the rows in sequence, so that the files in the specified directory can be assigned to the variable $ line in sequence, then use the variable $ line.
In other cases, the processing method with spaces is to add double quotation marks to the variables:
For example, Rm-R "$"
3. count the number of files in the directory
(1) view the number of files in the folder (number of files in the current directory)
Ls-L | grep "^-" | WC-l
Note:
Ls-l long list output file information under this directory (note that the files here, unlike general files, may be directories, links, device files, etc)
Grep ^-here, a part of the output information of the long list is filtered out, and only the general files are retained. If only the directory is retained, it is ^ d.
WC-l counts the number of rows of output information. Because only common files are available, the statistical result is the number of rows of general files, that is, the number of files.
(2) view the number of all files in the folder (number of files in the current directory and subfolders)

Find./-type F | WC-l
Or
Ls-LR | grep "^-" | WC-l

4. Search for a string in a directory and use the grep-R command, that is, grep-r string $ dirpath.
5. If you need to add a predefined Macro during compilation in GCC, run the command-D, that is, GCC-dmydebug-g-o A1 A. C.
6. We often encounter such a problem when using Linux, for example, executing a command
$ CP/etc/APT/sources. LIST/etc/APT/sources. List. Bak
There is a problem in it. The words/etc/APT/sources are the same. Why do I need to repeat them twice? This is a little short. If the directory is deeper, the effort will be great. Similar to the preceding example, you must perform similar operations to back up a file, rename a file, or create a link, and repeat the same path. It is usually wise to first CD to that directory, then operate under that directory, and then CD back. But it is still troublesome to do so. Is there any solution without leaving the house? The answer is yes.
Bash has a function called braces extension. parameters enclosed by braces, separated by commas, are extended to multiple independent parameters. Specifically, the example of the backup file above can be written:
$ CP/etc/APT/sources. {list, list. Bak}
Shell automatically expands the following parameters into two during interpretation, and then becomes the same complete command as above. In this way, the operation is performed without leaving the house, and the previous list of directories only needs to be typed once, and even the CD is skipped.
Further simplification is:
$ CP/etc/APT/sources. List {,. Bak}
In this way, there is nothing before the comma, so the parameter remains unchanged, and the extension after the comma remains unchanged.
7. built-in variables in awk

Number of records of the current input file for FNR
FS field delimiter (default :"")
Number of fields in the current NF record
Number of records of Nr in a job
OFS output field delimiter (default :"")
ORS output record delimiter (default: "\ n ")
RS input record delimiter (for gawk and mawk only)
Number of argc command line parameters
The index of the current file in argv in the argind command line
Convfmt digital conversion format, default: %. 6gd
Environ array containing the current shell environment variable
When filedwidths separates the list of fixed-width fields, use the blank field instead of the FS Field width list.
Filename name of the current input file
Ignorecase is case insensitive in regular expressions and string matching.
Output Format of ofmt numbers
The length of the string matched by the rlength match function.
Offset of the string matched by the rstart match Function
RT records the Terminator. for matching characters or using the regular expression specified by RS, gawk sets RT to the input text.
Subsep array subscript Separator

8. Differences between wildcards and regular expressions in Shell

Meanings of wildcards Regular Expressions
* Matches 0 or multiple characters .*
? Match any single character.
[A-Z] matches any single character in a-Z [A-Z]
[! A-Z] matches any single character that is not in a-Z ,! Note the difference ^ [^ A-Z]
{String1, string2,...} matches the string string1 of sring1 or string2 | string2

Wildcards are system-level, while regular expressions require the support of related tools: egrep, awk, Vi, Perl
9. Method for reading each row in the file:
(1) In the For Loop

#! /Bin/bash
Ifs = ""
N = 0
For line in 'cat/etc/passwd'
Do
N = 'expr $ n + 1'
Echo-e "$ N/T $ line"
Done

(2) While Loop

#! /Bin/bash
N = 0
While read line
Do
N = 'expr $ n + 1'
Echo-e "$ N/T $ line"
Done </etc/passwd

#! /Bin/bash
N = 0
CAT/etc/passwd | while read line
Do
N = 'expr $ n + 1'
Echo-e "$ N/T $ line"
Done

The while loop method is better than the for loop, because the for loop does not handle spaces in a row.
The difference between a while loop and a for loop is in loop control. While followed by a conditional statement, the for is generally in the form of a "variable in Name List ".
10. Method for passing parameters to SED or awk
Sed-n "$ rows "', '"$ Rowe" 'P' mv.txt | awk-V cols1 = $ cols1-V cols2 = $ cols2-V cols3 = $ cols3-V cols4 = $ cols4' {print $ cols1 $ cols2 $ cols3 $ cols4 }'
From the above, we can see that SED is different from awk. Sed is similar to a combination of strings to get the command, while awk uses the-V option to indicate that external variables are used to pass parameters.
11. Read and Write multiple files simultaneously in Shell
(1) One file is divided into multiple
Sometimes it is necessary to extract one or more column elements from the file to generate a new file, which is easily implemented in shell. For example, there is a data file with three columns of information: name, student ID, and class.
Redraiment 0612800134 0601
Christine 0612800136 0601
ZB 0612800229 0602
Now we need to store the first and second columns of this file in the F1 and F2 files respectively. We can use awk to extract the files, or we can use the following simple shell program:

#! /Bin/sh
While read F1 F2 F3
Do
Echo $ F1> F1
Echo $ F2> F2
Done

(2) Merge multiple files into one file
If you want to merge multiple files into one multi-column file, instead of appending them to the end of the file. For example, the F1s and f2s generated in the upper column are combined into join.txt. In this case, you need to operate multiple files at the same time, just like opening multiple files at the same time using fopen in C language, which is similar in shell. In shell, it is called "file descriptor" and expressed by "0-9. 0, 1, and 2 are the system's standard input, output, and error. "3-9" is used only by users. We can choose either of them to redirect the input. The script is as follows:

#! /Bin/sh
Exec 3 <F1
Exec 4 <F2
While read F1 <& 3 & read F2 <& 4
Do
Echo $ F1 $ F2> join.txt
Done

This actually tells us how to read multiple files at the same time.

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.