Linux --- shell basics

 

shell: shell to achieve using Linux batch management, rather than developing applications

shell can be associated with a better third-party libraries

1, bash's basic features:

       ctrl+l

       The key

       history

2, IO and redirection pipe character

(1) IO redirection: change the default location

1, > standard output redirection:  

ls> / dev / pts / 5 ls command to redirect the output window. 5
  ls> /data/ls.out ls command to redirect output to file /data/ls.out
  Note: If the file already has the content ls.out then after redirect the output file will overwrite any original content

2 >> : append redirection, the new content will be added to the target end of the file  

ls >> /data/ls.out

3, () : standard merged output a plurality of programs

(cal 2007;cal 2008) > all.txt

4, 2> standard error of the output redirection:

2 cmd> /data/err.log cmd (cmd command itself is no, the input command will display an error results) redirect error result to display data / err.log in

Note : history 2> /data/err.log because of their history command is correct, so the default output device will normally display the contents of the command history, and not output to /data/err.log in. Original data / err.log files will be overwritten empty file.
cmd 2 >> /data/err.log This will display the correct information on the default output device, and an error message each time input to err.log file for easy research questions behind

5,  <  standard input redirection

cat <File   File contents of the input file to the cat command

(2) pipe symbol

Pipe (using "|" symbol shown) for connecting the command
command 1 | command 2 | command. 3 | ...

  • The standard output of a command sent to the command STDIN 2, 2 standard command output command is sent to the STDIN. 3
  • The default forwarding STDERR not pipe, can use 2> & 1 | & implemented

 

ls /data /err 2>&1 | tr 'a-z' 'A-Z'
ls /data /err |& tr 'a-z' 'A-Z' 

 

3, Programming Basics

       Principles of Programming

Program: implementation of a code of a function

= Program comprising instructions (commands) + + (logic) Data (variable)

4, grep and regular expressions:

       Linux Three Musketeers: grep, sed, awk

       patten "mode"

5 logic, statement

Branch:

if

case: * use the startup script writing implement case of Linux *

cycle:

       for: Add to the list for the cycle

       while: do satisfy the judgment condition cycle is True --- Do not enter an infinite loop

       until: satisfies the determination condition do loop is False ---- /

 

break: out of the loop

continue: to interrupt this cycle, the next cycle continues

exit: Exit the current program, the return value of the specified program

read: Input

echo: Output

 

Linux Three Musketeers:

  sed: stream editor

    Switch between the two modes

       awk:

              The main job of the report generator, is an independent programming language

       grep: filter

 

function:

       A fixed function block

Array:

      

String:

       String Processing

 Exercise

1, it is determined / etc / inittab file is greater than 100 lines, if yes, display the "/ etc / inittab / is a big file." Otherwise Display "/ etc / inittab is amall file."

#!/bin/bash

#

Line = `wc -l /etc/inittab | cut -d’ ‘ -f1

 

if [ $Line -gt 100];then

       echo “/etc/inittab/ is a big file.”

else

       echo“/etc/inittab/ is a smal file.”

be

1, bash's basic features:   

a) command to expand:

The date command - Create command based on the date command

date: View the system clock

Check the hardware clock: clock, hwclock often synchronize the system clock with the hardware clock

cal View Calendar

date +% F: change the format of the output date 2019-08-05  

date +% F-% H-% M-% S output: = 2019-08-05-14-29-15 years - when - - May - Day minutes - seconds

%F = CCYY-mm-dd

%H = hh

%M = MM

%S = ss

%T = hh:MM:ss

%D = mm/dd/YY

       Exercise: Create a log file for a month's statement cycle (2019-08-06.http.log)

              # 或者touch `date +%F`.http.log

for i in {1..30};do

                     touch `date +%Y-%m-$i`.http.log;done

              Linux directory rule to follow: FHS directory rules

              {}: Extended e.g. cp / etc / inittab /etc/inittab.bak == cp /etc/{etc/inittab,/etc/inittab.bak}

             

              tar: Archive tool - can be operated directory

                     Compression tools: gzip, bzip2, xz compression can only manipulate files, directories can not operate

                     zcat view compressed files without decompression

Common parameters of tar:

                            -j corresponding to the compression tools bzip2

                            -J the corresponding compression tool xz

                            -z gzip compression tool correspondence

                            -c compression

                            -x decompression can not specify an extraction tool

                            -f specify the file name

                            -v Show Details

                            -C specifies the extracted directory

                            -t not view the contents of decompression, the same zcat

              Examples: tar cjvf 2018-08.http.log.bz2 2018-08 * .http.log compression

                            tar xvf 2018-08.http.log.bz2 -C / abc / decompression

              Exercise: write a script, all files backed up daily 02:20 / etc directory, the name of today's date; and save the file as a compressed file;

                     crontab  ----  020 2 * * *  bash /root/xxx.sh

                     vim xxx.sh

                            #!/bin/bash

                            #

                            tar  cJvf /var/`date +%F`.xz  /etc

b) Command History:

Role: use the View command between

About command history file: .bash_history - at root, the specialized storage command history, it will automatically write (history -a command can take advantage of the command stored in the memory in .bash_history) once at the time of shutdown

Variables on command history (environment variables): PATH is an environment variable

How --- Environment Variables View: env

--- view a more detailed environmental variable: set

              history of common operations:

                     1, the exclamation mark + number (n!): View command history

                     2, exclamation + String (String!): A string of recent matches

                     3, exclamation + exclamation point (!): A command

                     4, + $ exclamation mark ($!): Same as the last parameter ESC + shortcut key on a command function.

              Common options:

                     -a: Add

                     -d: delete

                     -c: Empty

                     #: # Display the most recent command line

c) command completion: tab key in the development environment \ t represents the four spaces

  1. prompt
  2. Autocomplete

d) directory completion: tab key

  1. prompt
  2. Autocomplete

Execution status e) command

In Linux, after the execution of each command will have two results:

       Replace the contents of the command itself

       Secondly, the command execution result status

Variable $? Variable is used to store the state of command execution

       0 indicates a successful state (and python opposite)

       1 represents a failure status to 255

f) command shortcuts

ctrl + l clear screen

ctrl + c End Process

ctrl + u to delete the character before the cursor

ctrl + k character after cursor is deleted

ctrl + a cursor jumps to the head

The cursor jumps to the end ctrl + e

ctrl + w a space-delimited files to delete

ctrl + r search again recently entered the command interface used (for input string)

g) alias alias

System boot sequence is read some special files

alias cdnet = "cd / etc / sysconfig / network-scripts /" can only be executed in the current shell

              Global configuration file

/ Etc / profile define environment variables (for all users) the first to read the file

/ Etc / bashrc define local variables, local variables typically used to set (for all users)

Personal profiles (written in the user's home directory)

~ / .Bash_profile define environment variables (user specified)

~ / .Bashrc --- Alias ​​definition of local variables (user specified)

              sources / etc / profile or. / etc / profile file so that the entry into force

h) file wildcard globbing

Not a regular expression wildcard file

? Any single character

              * Any character of any length

[] Any one character in the specified range

Any one character ^ [^] outside the specified range [^ 0-9A-Za-z]

Specify character classes: When calling in to add a []

       [: Alnum:] letters + numbers

       [: Alpha:] letter

       [: Upper:] uppercase letters

       [: Lower:] lowercase

       [: Digit:] Digital

       [: Punct:] special characters

       [: Space:] space ^ [[: space:]] * $ spacebar have blank lines

 

Guess you like

Origin www.cnblogs.com/gxnihao/p/11310739.html