Linux notes--file test, special variables, sed command, string test and for loop

Source: Internet
Author: User

  • File Test (Monocular):

  • -E File: Test whether the files exist with the-F file: Test file is a normal file-D: Test file is a directory-R file: Tests whether the current user has Read permission to the specified file-W file: Tests whether the current user has write access to the specified file-X file: Tests whether the current user has permission to execute on the specified file

  • Multi-Branched if statement:

  • if judgment condition; Then

  • Statement1

  • Elif judgment Condition 2; Then

  • Statement2

  • Elif judgment Condition 3; Then

  • Statement3

  • Else

  • Statement4

  • Fi

  • Test the script for syntax errors:

  • Bash-n Script Bash-x script (Single Step)

  • Position variable: $ $ $

  • Example: Bash W.sh/etc/passwd/etc/fstab script $: is represented as/ETC/PASSWD $: is represented as/etc/fstab

  • Shift: Rotation (position variable is used once and then kicked out) shift N (number): Can Jump N positions

  • echo "$"

  • Shift

  • echo "$"

  • Shift 2

  • echo "$"

  • Special variables: $? $# (number of display parameters) $* (display parameter list) [email protected] (show parameter list)

  • Example: if [$#-lt 0]; Then

  • Note: Exit return code (Error): Exits the script, defines the script exit status code, if the script has a well-defined, exit status code then the last execution of a command exit code, that is, the script's exit status code.

  • SED basic usage: Stream editor,

  • -N: Silent mode (the contents of the mode space are no longer displayed by default)-I: Modify the source file (not normally used)-e script-e script. : Multiple scripts can be executed at the same time-f/path/to/sed_script: using regular expressions

  • sed processes files in a memory space (pattern space), so the source file is not edited directly, however, after processing finishes, the contents of the pattern space are printed to the screen (so sometimes it appears as two lines of repetition)

  • SED address command file ...

  • Address:

  • 1.startline,endline

  • 2./pattern (regexp)/

  • 3./pattern1/,/pattern2/(the line that was first matched by 1 to the end of the line that was matched to the first time by 2, all the rows in the middle)

  • 4.linenumber: Specified line, $: Last line $-1: Penultimate line

  • 5.startline,+n: Starting from Startline, n rows backward;

  • Commad:

  • D: Delete Qualifying rows (example: sed ' 1,2d ' (double quotes as variable references)/etc/fstab) p: Displays rows that match the criteria a\string: Append a new line after the specified line (example: '/^\//a \# ... '/etc/passwd or/^\//a \#  ... \ n ...) I \string: Append a new row before the specified line R file: Adds the contents of the specified file to the qualifying line (example: sed ' 2r/etc/fstab '/etc/passwd) W file: Saves the specified range of content to the specified file (example: '/roo t/w/etc/passwd '/etc/fstab)

  • S: Find and Replace s/pattern/string/(example: s/^\//#//etc/passwd) By default replaces only the first matching string in each line, with the modifier: g (full screen replacement s///g) I: Ignore case.  (s) In addition to//can also use # # # @@@ 等) &: The reference pattern matches the entire string (SED ' s#/r. T/#&r#g '/etc/passwd) also supports a back reference (SED ' s#\ (r. t\) #\1r#g '/etc/passwd) (only change single initials: SED ' s#r (. t\) #R \1#g '/etc/passwd) add eee:sed ' 4, $s/^.*/eee&/' * * * at the front of each line in 4 to the last row (if &eee add Eee after each line)

  • Character test:

  • = = (or =): Compare, equal to true, unequal to false: [$A = $B]! =: Unequal to True, equal to False > < >= <=-N string: Tests whether the specified string is empty, null true, not empty false-Z string: Test specified character String is empty, empty is false, not empty is true

  • For loop: Enter condition, exit condition

  • for variable in list;

  • Loop body

  • Done

  • Generate list; {1..100}

  • Seq 10:1--10 seq 5 10:5--10 seq2 2 10:2 4 6 8 seq 1 2 10:1 3 5 7 9

  • SEQ: Start number step length end number

  • Declare-n (declaring variable type) NUM=0-X: Environment variable

  • Example: Line= ' wc-l/etc/passwd | Cut-d '-f1 '

  • For I in ' seq | $LINE '; Do

  • echo "# # #, ' Head-n $I/etc/passwd | Tail-1 |cut-d:-f1 ' ";d One


This article is from the "stupid Bird first-FAI" blog, please be sure to keep this source http://benniaoxianhui.blog.51cto.com/10463960/1679200

Linux notes--file test, special variables, sed command, string test and for loop

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.