Shell scripting language Basic grammar Learning

Source: Internet
Author: User
Tags shebang egrep

Qiniu Seven cow problem solution

Getting started with the Shell scripting language

Problem solving Shell Daily learning
Cd/go to the root directory PWD current directory structure tab blur find complete command IFCONFG view current network information *? [] wildcard character convenient query star flower * match multiple ls *.tar? Matches a single LS m??? [] LS m* [od] lists the directory with the opening end of M for od fire file ls x* [0-9] Ls G *[m-p] Directory operation command: Current working directory: PWD Go to home directory: cd ~ list file types: Ls-f list hidden files: ls-a or LS-AF displays the various properties of the file: Ls-l display information for files in directory: Ls-af/et C or display directory: Dir or display the detailed properties of the file: VDir Displays the file line number: Cat-n Filenamecat is defective. Scroll to show more +filename next page, split screen display by space scrolling or pipe: cat-n filename |more View the first five elements of a file: Head-n5FileName to view the contents of the last five elements: tail-n5FileName more powerful less use the arrow keys to scroll up and down, enter Q to exit the Find file information: grep content file name in the current directory to find file names containing Bash:find./-name *bash* View File typestype+ filename to find a directory under the name INIT.D file type is directory: Find/etc-name INIT.D-typeD-PrintType (c D f) C for link D for directory f for normal file specified time in -Commands not used within days: find/usr/bin/-typeF-atime + --PrintSpecify a normal file with the last modification time within one day: find./-typeF-mtime-1-PrintQuick Find: Locate filename updatedb open Firefox: Firefoxfirefox & run Firefox www.baidu.com finder file in the background: Whereis find find The location of the command, and the document location to view the directory where the command exists: which LS to see which users are present in the current system: who view version information for the current system: UNAME-A help command: Man find simple help command: Whatis ls regular expression: Nl + filename : View the file in line, instead of the Cat list in the file with a beginning with a, the text ending with T: the point represents any character, * represents any number of times, ¥ represents the end, ... ^ stands for the beginning. Egrep "^a.*t$" filename to see how many of the output you just had: add a | wc-w Example: Egrep ". At" filename This command shows that at the front of at least one word may not meet our needs, if we want to have only one word in front then we can only do so Egrep "\<.at\>" The concept of the filename character set: A file name that starts with ABC Egrep "[abc]at" filename listed in the file, all beginning with a capital letter and ending with a lowercase letter t: Egrep "^[[: Upper:]]t$ "filename
Shell programming

Vi helloword.sh

!/bin/bash This is an instance file

Echo "Helloword"

Chmod u+x helloword.sh

./helloword.sh

Another example:

!/bin/bash

Log = "Monday"
Echo "This is Monday"
Echo Log E Cho$E Cho"Log Is (log) "

Enclosed in single quotes, all without explanation.
Double quotation marks. Except $, and so does not explain

! /bin/bash

Log=saturday

Echo "Today is $log"

Echo ' Today is $log '

Echo "Today is date "

Output results

Shell execution commands and loop execution and control statements

If Judgment command

Case:

If

A statement about repeating loops.

! /bin/bash

Sum=0
Number =1
While test NumbeR?Le -DoS um= [ s u m + Number]
Let nmber= NumbeR+1DoNeE Cho"ThesummRRy Is Sum

-le
-GT Greater than

Cut command

The main function, from the input line, extracts the specified part

Source file:

Beijing 010
Shanghai 021
Tianjin 022
Hangzhou 0571

Command: cut-c 3-6 City

Command: CUT-F1 City extract the first column of the input row

Sort command:

Sort City: Sorting the first column

Sort-r City: Order the first column in reverse order

Sort-k 2 City: Sort the second column

If there are duplicate rows:

First you need to sort it out before you can use the Uniq command

Sort City |uniq
Convert several characters to lowercase, enter in city
Tr "BHS" "BHS"

Code Practice One
#!/bin/bashfunctionGotologfile(){ Case    $  inch"579")Echo  $, $;CD~/logs/ $/req/up; Timepoint= 'Echo  $|sed' s/.$/0/'`;Echo "${timepoint}. Log"; Cat"${timepoint}. Log"| Awk-f' \ t '  ' {if ($8 = = 579) {print substr ($, index ($, "UID") + 5, +)} '| Sort-rn | Uniq-c |sort-rn | Head- -;;"4xx")Echo  "404";;"6xx")Echo  $, $    CD~/logs/ $/req/rs; Timepoint1= 'Echo  $|sed' s/.$/0/'`;Echo "${timepoint1}. Log"; Cat"${timepoint1}. Log"| Awk-f' \ t 'Cat"${timepoint}. Log"| Awk-f' \ t '  ' {if ($8 = = 579) {print substr ($, index ($, "UID") + 5, +)} '| Sort-rn | Uniq-c |sort Cat -- +. log | Awk-f' \ t ' ' {if ($8 ~/^4/) {print $8, substr ($, index ($, "UID") + 5, +)} '| Awk' {if (= = = 404) {a[$2] +=1} else if ($ = =) {b[$2 +=1]} else{}} end{for (i in a) print A[i] "==>" I; for (I I n b) Print b[i] "==>" i} ';;Esac}if[$# -ne 3]; ThenEcho "Usage:  $ < error code > < date > < day time > For example $  519 2014-12-28 11-29";Exit  1;fiGotologfile $   $   $
Code Practice Two
#!/bin/bashNumber=1Filenumber=1Length= $CD/home/liuhanlin/test/qiushi/totalfile= ' ls- L|wc- L`Echo "$totalfile"Center=$totalfileflag=-1Bound=$length whileTest$flag-le0 DoMkdir"Qiubai$filenumber"            whileTest$number-le$bound                DoMv"$number" "Qiubai$filenumber"                      LetNumber=$number+1                Done            Letcenter=$[$center-$length]Echo "$flag">config.txtEcho "$center">config.txt LetFilenumber=$filenumber+1           Echo "$filenumber">config.txt Letbound=$[$length*$filenumber]Echo "$bound">config.txtif[$center-le$length]; Then               Letflag=1           fi DoneMkdir"Qiubai$filenumber" whileTest$number-le$totalfile DoMv"$number" "Qiubai$filenumber"               LetNumber=$number+1 Done

Shell scripting language Basic grammar 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.