Linux Shell Learning

Source: Internet
Author: User
Tags aliases

Shell Features:

History is saved in ~/.bash_hostory and retains 1000 records: defined in

Echo $HISTSIZE

!: Previous Command

!$: Last parameter of previous command

! 950: No. 950 order

!c: The most recent command in history that starts with C

TAB: Command completion

Alias: Aliases Unalias

*,?: Wildcard Regular expression

|: Pipe character

>,>>: Redirect and append

2>: Error redirect 2&1>: Neat and bad redirect

Add a numeric ID to the back of the SLEEP,FG,BG,JOBS:FG

Shell variables:

ENV: View System variables

Set: System and custom variables

Variable with special characters, need single quote: b= ' ls/tmp '

Variables in the reference command, using the anti-quote: myvim= ' which vim '

The connection between variables, in double quotes: c= "$a" 12

Export: Variable Global declaration

Local: Variable partial definition

unset d: Delete the value of a variable

Shell Environment variables:

System level:

/etc/profile:/etc/profile.d/*.sh

/ETC/BASHRC:

1)/etc/profile.d/*.sh

2) ps1= ' [\[email protected]\h-\t \w (w)]\$ '

3) Umask Modification

Umask.sh:umask 0012

User level:

. Bash_history: Record History commands

. bash_logout: What to do when you exit

. Bash_profile: User's own environment variable configuration file

. bashrc:root aliases used by users

Shell Basic usage Cut:

cat/etc/passwd | Cut-d ': '-F 1 | Head-n5 (to: first 5 pieces of data of the split)

head-n2/etc/passwd | CUT-C2 (interception of the second character)

head-n2/etc/passwd | Cut-c1-5 (intercept 第1-5个 characters)

Shell basic usage of sort:

head-n5/etc/passwd | Sort: Ascending

head-n5/etc/passwd | SORT-T:-k3-n: Sort with a third field using pure numbers

head-n5/etc/passwd | SORT-T:-k3,5-r: Reverse-sorts strings from third to fifth fields

Cut-d:-f1/etc/passwd | Sort-n-u:-nu will treat the string as 0 and go back to

WC for basic Shell usage:

Wc-l 1.txt 2.txt: Lists a few lines

Wc-w (Word): Outputs how many words are separated by a space

echo "12345" | Wc-m: How many characters are output

Shell basic usage of uniq and tee:

Uniq + files: de-weight

Sort + Files | Uniq-c: Only the two rows are identical to each other, and C is the number of rows that can be counted.

echo "1213" | Tee + Files: Redirect and display the results on the screen

The Shell's basic usage tr and split:

head-n2/etc/passwd | TR ' [A-z] ' [A-z] ': Convert lowercase to uppercase

-D: Remove a character;-S: Remove duplicate characters

split-b500 passwd: The file is divided into 500bytes as a file, the default does not specify the target file, it will produce xaa,xab,xac ... File to display

Split-l 10 + file: Split the file in 10 behavioral units

Split-b 500 + file 123: Specify the target file name, the 123aa,123ab,123ac ... To display

The Shell basic usage of grep:

Grep:

1). -C: Print the number of lines that meet the requirements

2). -N: Output line number

3). --color: Display Color

4). -V: Print rows that do not meet the requirements

5). -a+ numbers: Displays rows that meet the requirements and the following n rows

6). -b+ Number: Displays the line that meets the requirements and the top n rows

7). -c+ Number: Displays the line that meets the requirements and the top and bottom n rows

8). -RH: Access directory Grep-rh "Iptables"/etc/*

9). --include: Include a file

Filter all the lines in the *.php document that contain eval in a directory

Grep-r--include= "*.php" ' eval '/data/

Egrep = Grep-e

SED basic usage of Shell:

Sed ' p-n 1.txt: Plus-n prints out rows that match the rules

Sed-n '/root/' p 1.txt: Print lines that contain special characters

-r:sed does not recognize +| Symbols such as {} () need to be de-defined characters \ or-R

Sed-n-R '/ro+/' P 1.txt

Sed-n '/ro\+/' P 1.txt

-E: Implementation of multiple tasks, can also be used; To achieve

Sed-e '/root/p '-e '/body/p '-n 1.txt

D: Delete the specified line

Sed '/root/d ' 1.txt; Sed ' 1d ' 1.txt; Sed ' 1,10d ' 1.txt

S: replace

Sed ' 1,2s/ot/to/g ' 1.txt

Replaces the position of two strings, where \3\2\1 represents three () of the contents

HEAD-N2 1.txt | Sed-r ' s/(Root) (. *) (bash)/\3\2\1/'

A: Add after the specified line

Sed ' 2,4a abcd ' xx.txt add ABCD in 2 to 4 rows

-I: Modifying files directly

Sed-i ' s/ot/to/g ' 1.txt

The basic Shell usage of awk:

1. Awk-f ': ' {print $} ' 1.txt

2. Awk-f ': ' {ofs= ' # '} {print $1,$2,$3,$4} ' 1.txt is

Awk-f ': ' {print $1#$2#$3#$4} ' 1.txt

3. awk '/oo/' 1.txt: Match oo

4. Awk-f ': ' $ ~/oo/' 1.txt: Match oo in the first paragraph

5. Conditional operator ==,>,<,!=,>=,<=

The third paragraph is 0:awk-f ': ' $3== ' 0 "' 1.txt

The third paragraph is greater than or equal to 500:awk-f ': ' $3>=500 ' 1.txt

The seventh paragraph is not '/sbin/nologin ': awk-f ': ' $7!= '/sbin/nologin ' 1.txt

6. Awk built-in variable NF (number of segments) NR (number of lines)

Head-n3 1.txt | Awk-f ': ' {print NF} '

7. Mathematical calculations:

Awk-f ': ' {ofs= ': '}{$7=$3+$4; Print $} ' 1.txt

8. Awk-f ': ' {(tot=tot+$3)}; END {print tot} ' 1.txt

9. Awk-f ': ' {if ($1== "root") print $} ' 1.txt

Shell Basic Syntax:

1) #!/bin/bash specified to run with bash

2) If statement

if condition; Then

Command

Fi

if condition; Then

Command

Else

Command

Fi

if condition; Then

Command

Elif condition; Then

Command

Else

Command

Fi

3) [Conditions]

-D: Whether it is a directory

-F: is normal file

-L: Is the connection file

-E: whether there is

-N: Not 0

-Z: Whether it is 0

-R (-w-x): whether there is permission

4) for And while

For i in ' SEQ 1 5 ' Do

Echo $i

Done

While condition; Do

Command

Done

Dead Loop:

While:; Do

Command

Sleep 3

Done

Continue Break Exit

5) Date Command

1) 2015-08-25:date +%y-%m-%d

2) 15-08-25:date +%y-%m-%d

3) Date +%f

4) 21:45:15:date +%h:%m:%s

5) 21:45:15:date +%t

6) Timestamp: Date +%s

7) date-d @1440510413

8) After one day: date-d "+1day"

9) One day ago: date-d " -1day"

10) January ago: date-d " -1month"

11) One minute ago: date-d " -1min"

12) Week: Date +%w (%w) lowercase for weeks, uppercase for week of the year

6) Mathematical operations

sum = $[$a + $b] (+-*/)

Decimal: Echo "SCALE=2;10/3" | BC Output 3.33

7) Interaction

Read-p "Please input a number:" X

8) Sh-x xx.sh View the execution process

9) Case Syntax

Case variable in

value1)

Command

;;

value2)

Command

;;

*)

Command

;;

Esac

) Shell Array

A= (1,2,3,4)

Number of array elements: Echo ${#a [@]}

Read single element: Echo ${a[2]}

Print the entire array: Echo ${a[@]} or echo ${a[*]}

Delete array: unset a unset a[1]

Array shard: Echo ${a[@]:0:3} from table 0 below 3

Array substitution: Echo ${a[@]/3/100} change the array to a third 100


Linux Shell Learning

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.