Linux Programming Mastering common commands

Source: Internet
Author: User
Tags create directory locale posix print format vars egrep

1) Compiling the application

Make-f Makefile_5_2 Clean
Make-f Makefile_5_2

2) About shared directories
This folder can be seen under the/mnt/hgfs of a Linux virtual machine

3) CD command, go to Folder


4) Copy CP command

Cp-ri a/b/* a1/b1/tip whether to overwrite

\CP-RF a/b/* a1/b1/do not prompt for direct overwrite

5) Linux shutdown

Shutdown-h now

6) Create directory mkdir

mkdir HPP

7) View file time

Stat HPP

8) Start the program under Linux./ccs

Program exit, first use Ps-ef|grep XXX to view its PID, and then return with the kill PID----------Force exit kill-9 PID

9) ll see the file read and write permissions, chmod 777 XXX give the file the highest permissions

The Dos2unix command is used to convert a DOS-formatted text file into UNIX format, and sometimes a conversion is required to upload a window file to Linux.

Format: Dos2unix file

Much thanks to Classic Shell Scripting

#chapter 2 Shell Basic

1. Parameters received by the Access script from the command line: $n
Use the first parameter in the script to access the second one, when more than 9 o'clock, with curly braces, such as ${10}.

2. When the shell script executes, use-X to open the script to perform the tracing function. such as: $ sh-x

3. List all languages supported by the system: LOCALE-A

#chapter 3 Search and substitution

4. Shell BRE (Basic RE) and ERE (Extended RE) Regular expression précis-writers:
\ Close or open the special meaning of subsequent characters
. Matches any single character, first nul outside
* Match any number of individual characters before it
+ 1 or more (ERE only)
? 0 or more (ERE only)
^ Indicates the beginning of a line, in [^ ...] The inside means to take the counter
$ represents the end of a line
[...] Match any single character within square brackets
{n} matches preceded by a single character n times (ERE, need to use escape \{n\} in Bre)
{n,m} appears at least n times, up to M times
() represents an instance (ERE only)
| Match a regular expression before or after (ERE only)

Note: Under the BRE, ^$ only has a special meaning at the beginning and end, and in other places, such as 39.8$killo, represents the $ itself.

5. Backward Reference: Backreferences
For example, \ (ab\) \ (cd\) [def]*\2\1 can match Abcdcdab, Abcdeeecdab, Abcdffcdab, ... Back references can have up to 9

6. POSIX character set: [: Alpha:]
[: Alnum:] Numeric characters, such as 123
[: Alpha:] alphabetic characters, such as ABCdef
[: Lower:] Lowercase alphabetic characters, such as ABC
[: Upper:] Uppercase characters, such as Def
[: Blank:] spaces space in the Position tab character
...... More
$ grep-e ^[[:alpha:]]\{3\} data.txt
Hello, world.

7. ERE (Extended RE)
There is no backward reference.
Interval expression does not require \{\}, directly using abc{3,5}, indicating that C appears 3 to 5 times
? Represents 0 or one predecessor re
+ 1 or more
* Same as BRE, 0 or more
| To match the sequence or the sequence or ... Read|write|listen
() grouping, (ABC) {3,5} means that ABC appears 3 to 5 times, but does not include parentheses () itself,

8. Additional GNU Regular expression operator: \w
\w matches any character that consists of words
\w matches any non-word-composed character, ^\w

9. Replace the text lookup: sed (steam editor)
Sed s/regexp/replacement/
$ sed ' s/:.*/:* *****/' data.txt
Hello, world.
Another password:******
The above command replaces all the contents of the colon (:)) with 6 asterisks (*), the/as a delimiter in sed s/regexp/replacement/, and any characters that can be displayed, such as
Sed s;regexp;replacement;
Sed s:regexp:replacement:
Sed s,regexp,replacement,
...... such as
$ find/home/owen/test/todelete/-type D-print |
Sed ' S;/home/owen/test/todelete;/home/owen/test/todel; ' |
Sed ' s/^/mkdir/' |
+ mkdir/home/owen/test/todel/
+ mkdir/home/owen/test/todel/xyz
The command first finds all directories in this directory, including the directory itself, and then replaces the Todelete with Todel, with the following result:/home/owen/test/todelete/
Then add the "mkdir" command to each line to create a new directory. The functions implemented are similar to CP.

10. Check the System password information:/etc/passwd
$ more/etc/passwd
Each row is separated by: 7 fields, respectively, representing
Owen User Name
X password after encryption
1000 User ID number
1000 User Group ID number
Owen,,, User name, additional information, such as contact info
/home/owen User's root directory
/bin/bash the shell type of login

11. Clip content from text: Cut
$ cut-d:-F 1,5/etc/passwd | Grep-e ^m
Mysql:mysql Server,,,
-D represents the delimiter,-f denotes field

12. Connect 2 files, based on fields: Join
Join Quotas.sorted sales.sorted
Connect using the first field in two files, such as
A b
After connection, for a B C
Of course you can specify the connection key,-1 2-2 5, refer to Manual

13. Rearrange fields: awk
$ ls-l | awk ' {print $8, $ $} ' | Sort
Data.txt 67-rw-r--r--
Finduser 88-rwxr-xr-x 363-rwxr-xr-x 36-rwxr-xr-x
Nusers 60-rwxr-xr-x
Quotas 58-rw-r--r--
Sales 71-rw-r--r--
This first lists the files in the current directory, and then uses awk to display the file name, size, permissions, and finally sort the display.
Awk uses whitespace as the delimiter character by default.
$ ls-l | awk ' {printf '%s%s\t%s\n ', $ $, $ $8} ' | Sort
The basic pattern is as follows:

#chapter 4 Text Process tools

14. Text Sort: Sort
$ sort-t:-K 3,3/etc/passwd
-t Specifies the delimiter,-k specifies from which field to which field to sort as key

15. Remove Duplicates: Uniq
$ Sort Uniq-data | Uniq-c
2 Duo
3 Tres
1 Unus
Eliminate duplicates and control the display of duplicate or duplicate records

16. Simple text formatting commands: FMT
$ More Data.txt | FMT-W 50
Hello, world. ABCDEFDEFABC password:123456
Another password:666888

String sort\nbased on lines delimilated by new
Line Sign
Format to a maximum of 50 characters per line

17. Count the number of rows, words and characters: WC
/usr/share/dict$ More Words | grep ^herb | Wc-lwc
17 17 165

18. View the top N records of the standard input, or the first n in the file list, or the last N: Head, tail
Head-n 5/etc/passwd
Sed-e 5q/etc/passwd
Displays the reciprocal n, typically used to view recent log records
Tail-n 5/etc/passwd

#chapter 5 The Magic power of the pipe

19. Word Puzzle Good helper File: Example
Files= "
Pattern= "$"
Egrep-h-i "$pattern" $FILES 2>/dev/null | Sort-u-F
Use this script to find a 10-letter word, start with B, and the 7th digit is either x or y:
$ sh ' ^b.{5}[xy]. {3}$ ' | Fmt
beatifying Birdseye ' s blarneying Brooklyn ' s Bulawayo ' s
Equivalent to using commands:
/usr/share/dict$ More Words | Egrep-i ' ^b.{5}[xy]. {3}$ ' | Sort
Birdseye ' s
Brooklyn ' s
Bulawayo ' s

20. Convert or delete characters: TR
TR [Options] source-char-list replace-char-list
-C takes source-char-list, that is, to convert or delete characters that do not appear in the Source-char-list
-D Delete the characters that appear in the source-char-list, such as deleting all vowels:
echo Hello World | tr-d [Aeiou]
HLL Wrld
-S condensed repeating characters, such as:
echo Hello World | Tr-s L
Helo World
Typically combined to use, such as convert all to lowercase, all non-alphabetic characters are converted to line-break symbols
echo Hello World 123 End | TR A-Z | Tr-cs a-za-z ' \ n '
No numbers are included here, and if necessary, add a-za-z0-9.

21. Count the occurrences of a word in an article frequency: WF
Tr-cs a-za-z0-9 ' \ n ' | Convert non-alphabetic characters to newline symbols,-cs reference 20th note
TR A-Z | Convert all to lowercase letters
Sort | Sort
uniq-c | Statistical frequency, Results:
Sort-k1,1nr-k2 | First, only the first field is the number, sorted by the numeric order-n Inverse-R, and then the words are sorted in dictionary order
Sed ${1:-25}q ${1} Gets the first argument of the command line, if there is no default of 25, the back Q means exiting the program

${1:-25} is a parameter expansion form inside the shell, as follows:
The expansion method is: First find ${var}, if found, the value is ${var}, if not found, the value is equal to Default-var

Use (Requires chmod +x WF, then put WF under $path Path), using the highest frequency:
Man awk | WF | Pr-c4-t-w80
292 the 0 Mawk
169 and, a string of ~ s, n
168 is 1 to expr
155 A (IF)
124 of for a or a file
118 to-A
PR command, print format, page column for printing
-C4 represents COLUMN4, which is the equivalent of Word's column action, which is set to 4 columns
-T means that the page header and footer are not displayed, and if this is not the case, it is a printer paper size
-w80 set the page width, here is 80 characters

The lowest:
$ Man awk | WF 99999 | Tail-n 25 | Pr-c4-t-w80
1 typically 1 under 1 values 1 WC
1 u 1 underscores 1 variations 1 we
1 unaltered 1 Unlike 1 variety 1 Whidbey
1 unambiguous 1 unnecessary 1 Vdiesp 1 writing
1 unbuffered 1 unsafe 1 Vertical 1 XAXBXCX
1 unchanged 1 usually 1 via 1 xhh
1 undefined
My path.
Echo $PATH

This program is interesting, we can figure out how many non-repeating words are used in the AWK Handbook, only 1014, 99999 this number is meaningless, just a big one.
$ Man awk | WF 99999 | Wc-l

High-frequency vocabulary (frequency of more than 5 times, including 5 times) number, altogether is so much less!
$ Man awk | WF 99999 | awk ' $ >= 5 ' | Wc-l

22. Apply: Tagging and Automatic catalog generation
$ more shell note. txt | Grep-e ' # '

#chapter 2 Shell Basic
#chapter 3 Search and substitution
#chapter 4 Text Process tools
#chapter 5 The Magic power of the pipe

More shell note. txt | Grep-e ' ^[0-9]+\. ' | Sed ' s/^/@-/'
@-1. Parameters that the access script receives from the command line: $n
@-2. When the shell script executes, use-X to open the script to perform the tracing function. such as: $ sh-x
@-3. List all languages supported by the system: LOCALE-A
@-4. Shell BRE (Basic RE) and ERE (Extended RE) Regular expression précis-writers:
@-5. Backward References: backreferences
@-6. POSIX character set: [: Alpha:]
@-7. ERE (Extended RE)
@-8. Additional GNU Regular expression operators: \w
@-9. To replace a text lookup: sed (steam editor)
@-10. To view the password information for the system:/etc/passwd
@-11. Clip content from Text: Cut
@-12. Connect 2 files, field-based: Join
@-13. Re-orchestration Field: awk
@-14. Text sort: Sort
@-15. Remove Duplicates: Uniq
@-16. Simple text formatting commands: FMT
@-17. Count the number of rows, words, and characters: WC
@-18. View the top n records of a standard input, or the top N in a file list, or the last N: Head, tail
@-19. Word Puzzle Good helper File: Example
@-20. Convert or delete characters: TR
@-21. Count the occurrences of a word in an article frequency: WF
@-22. What to do: Tagging and automatic catalog generation

Use the SED command to add a @-prefix to each piece of data to confuse it with a real directory.

#chapter 6 variables, repetitions

23. Setting or modifying Environment variables: Export
Path= $PATH:/home/owen/bin
Export PATH
Print environment variable Export-p

24. Remove the variable from the shell from the function: unset
$ foo=123
$ echo $foo
$ unset foo
$ echo $foo
Delete a function by using unset-f function_name, which defaults to-V to delete the variable

25. Parameter expansion: ${varname:-word}
Substitution operation:
${varname:-word} If the varname exists and is not NULL, the value is returned, otherwise word is returned. Purpose: If the variable is not defined, the default value is used

${varname:=word} If varname exists and is not NULL, its value is returned, otherwise it is set to Word and its value is returned. Purpose: If the variable is undefined, set the variable to the default value

${varname:+word} returns Word if varname exists and is not NULL, otherwise NULL is returned. Purpose: To test the existence of variables.

${varname:?message} If varname exists and is not NULL, its value is returned, otherwise varname:message is displayed and the current command or script is exited. Purpose: Captures errors caused by undefined variables
$ echo ${vars:? " Undefined, pls check it "}
bash:vars:undefined, pls check it

Note that the ${varname:-word} each colon (:) is optional, and if there is no colon, the condition becomes "if varname exists", which can be null

More Pattern matching
$ p=/home/jwu/cases/
$ echo $p

${variable#pattern} If the pattern matches at the beginning of the variable, delete the shortest part of the match and return the remainder
$ echo ${p#/*/}

${variable# #pattern} If the pattern matches at the beginning of the variable, delete the longest part of the match and return the remainder
$ echo ${p##/*/}

${variable%pattern} If the pattern matches the end of the variable, delete the shortest part of the match and return to the remaining part
$ echo ${p%.*}

${variable%%pattern} If the pattern matches the end of the variable, delete the longest matching part and return the remainder
$ echo ${p%%.*}

Note that the pattern used here, as well as other places in the shell, such as case statements, is different from the schema match of the preceding regular expression. As above, here * represents any one of the symbols, and. Represents the dot number itself.

The POSIX normalized character from the length operator: ${#variable} returns the length of the $variable value
$ d=diversification
$ echo $d
$ echo ${#d}

Shell special variables, access parameters: $#, [email protected], $*
Set parameters: $ set--Hello "Hi there" greeting
$# the total number of arguments passed to the shell script or function
[email protected] passed in the command line arguments, placed in double quotation marks (""), will be expanded to individual parameters
$ for i in [email protected]
> Do echo I are $i
> Done
I am Hello
I is hi
I is there
I is greeting
Note that the space between the hi there above is missing.
$ for I in "[email protected]"
> Do echo I are $i
> Done
I am Hello
I is hi there
I is greeting
Plus "" will get each parameter
$* the command line arguments passed in, enclosed in double quotation marks (""), are expanded to a single argument

The shell operator is similar to the C language: +-*/
Operation is placed in $ ((...)) Within, note is round brackets
Specific reference tables
$ ((3 && 2))
$ echo $ ((3 > 2))
$ echo $ ((3 > 4))
$ echo $ (((3 > 2) | | (3 > 4)))
The same as C and its derived languages C + +, Java, and awk, and non-0 values represent true.

29. Exit Status: $?
$ echo Hello
$ echo $?
$ e S
E:command not found
$ echo $?
End state of POSIX
0 Command successfully exited
All other states are failed exits, such as
127 Command not found

You can pass an exit value to its caller in a shell script, such as Exit 42

30. Judgment statement: If-elif-else-fi
If pipeline
Then ...
Elif Pipeline
Then ...
else ...

31. Logical judgment: not, and, or
Not if! (...)
and (...) && (...)
OR (...) | | (...)

The. Test command: If ...
If [$#-ne 1]
echo usage:finduser username >&2
Exit 1
The main figures are compared with
-eq equal
-ne Not Equal
-lt less than
-GT great than
-le Less or equal
-ge Great or Equal

#chapter 7 input/output, file, and commands execute

To be continue ...

Linux Programming Mastering common 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: 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.