1.1 Shell some small summary
1, 0 wide assertion 2, Print menu 3, getopts option Introduction 4, script query IP geolocation
1.1.1 grep zero-width Assertions (0 wide assertion)
It means taking out what you want and getting rid of things you don't care about.
-O: Indicates exact match-P: Indicates a regular expression using Pcre to match
1. Antecedent assertion: Represents the position preceding the match expression
[Email protected] ~]# echo "Cooking Sing" | Grep-op ' [a-z]* (? =ing) ' Cooks
The above example: (? =ing) This is the assertion, meaning that when the assertion exists, the judgment match, match to the
The object is the string in front of it. Of course, the preceding string you also need to match the regular expression, for example: ([A-z]) *, because
The regular is greedy, so the assertion will always end up from the right side to the mismatched one.
2. Post assertion: Indicates the position following the match expression
[Email protected] ~]# echo "ABCDEFG ABCA" | Grep-op ' (? <=abc). * ' DEFG ABCA
The above example: (? <=abc) is the assertion, as opposed to above, it is to start from the left side of the match
Small combat
(1) Take IP address
[[email protected] learn]# ifconfig eth0 link encap:ethernet hwaddr 06:66:91:e7:9a:74 inet addr:10.10.11.15 Bcast:10.10.11.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST mtu:1500 metric:1 rx packets :1234086 errors:0 dropped:0 overruns:0 frame:0 TX packets:829131 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1182619038 (1.1 gib) tx bytes:88948632 (84.8 MiB) Interrupt:246 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:62861848 errors:0 dropped:0 overruns:0 frame:0 tx packets:62861848 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:10133085846 (9.4 gib) TX bytes:10133085846 (9.4 gib)
Remove the IP address of the EM1
[Email protected] ~]# Ifconfig | Grep-e ' (eth0|em1) '-A 1|grep-op ' (? <=ADDR:) [\d.] + ' 10.10.10.180
(2) Time to take the log
If the Nginx backup log format file is Access_20150408_nginx.log
[Email protected] ~]# echo "Access_20150408_nginx.log" | Grep-op ' (? <=access_). * (? =_nginx) ' 20150408
1.1.2 Print Menu
echo command
-N: Do not wrap-e: Turn on the backslash ESC escape. If the following character appears in the string, it is handled in particular, not as a general text output
\a a warning sound; \b Delete the previous character; \c ends with a newline symbol, \f wraps but the cursor remains in its original position; \ n Wraps and the cursor moves to the beginning of the line; \ r The cursor moves to the beginning of the line, but does not wrap; \ t inserts tab; \v is the same as \f; \ \ insert \ character; \nnn inserts ASCII characters represented by NNN (octal)
Read command
-P: Interactive display prompt information-T: Timeout time-N: Set number of characters-s: not visible (useful when entering a password)-D: Delimiter
Implement a simple menu
#!/bin/bashmydate= ' date +%d/%m/%y ' this_host= ' hostname ' user= ' whoami ' while true;do # clearcat <<eof _____________________________________ _______________ User: $USER Host: $THIS _host date: $MYDATE ____________________________________________________ 1: list files 2: See memory Total 3: See who is on the systeme 4: help screen q: exit menu ____ ________________________________________________eofecho -e -n "\t You Choice[ 1,2,3,4,q]> "read choiceecho $CHOICEcase $CHOICE in 1) ls ;; &NBSP;2) free -m ;; &NBSP;&NBSP;3) who ;; 4) echo "This is help screen , nothing here yet to help you ! " ;; Q|Q) exit 0 ;; *) echo -e "\t \007 unknown user reponse" ;; esac #echo -e -n "\t hit the return key to continue " read -p " Hit the return key to continue " Godone
1.1.3 Getopts Introduction
Different hints or different executions depending on the parameters
Let's look at a column first.
#!/bin/bash while getopts "H:n:a" argdo case $arg in &NBSP;&NBSP;&NBSP;H) echo "Welcome to beijing arg: $OPTARG ";; n) echo "your name is $OPTARG";; a) echo "your age is $OPTARG " ;; :) echo "The -h arg must have one args";; ?) echo "Please input your select args" ;; esac done
Execution results
[[Email protected] learn]# sh get.sh-h "Hello World"-n budongshu-a Welcome to Beijing Arg:hello worldyour name is BU Dongshuyour Age is
How to use getopts scripts
Script. SH option_string variable
The first parameter uses the-H option. The second parameter is the Hello World string followed by-H
Parse the above example
Define options:
You can see the phrase "h:n:a" arg in the while getopts, so the definition of the option "H:N:A" is defined here.
Colon meaning
1, the letter followed by a colon to indicate that this option must have parameters, i.e.-H and-n must provide a parameter,
The value of the parameter is stored in the variable $optarg
2, there is no colon after the letter for this option can not follow the parameters, follow up and do not assign value. You can see that-a has no value
3, a colon at the top of the letter indicates that the error message is provided by ignoring the command itself. Here's a demonstration
Script
#!/bin/bash while getopts "H:n:a" argdo case $arg in &NBSP;&NBSP;&NBSP;H) echo "Welcome to beijing arg: $OPTARG ";; n) echo "your name is $OPTARG";; a) echo "your age is $OPTARG " ;; ?) echo "Please input your select args" ;; esac done
Execution result: The command internally provided an error in the middle of the
[[Email protected] learn]# sh get.sh-h hello-n Welcome to Beijing arg:helloget.sh:option requires an argument--Nplea Se input your Select args
Plus a colon to see later
Script
#!/bin/bash while getopts ": H:n:a" argdo case $arg in &NBSP;&NBSP;&NBSP;H) echo "Welcome to beijing arg: $OPTARG ";; n) echo "your name is $OPTARG";; a) echo "Your age is $OPTARG " ;; ?) echo "Please input your select args" ;; esac done
Execution result: You can see a missing error.
[[Email protected] learn]# sh get.sh-h hello-n Welcome to Beijing arg:helloplease input your select args
Error message prompt
Script
#!/bin/bash while getopts ": H:n:a" argdo case $arg in &NBSP;&NBSP;H) echo "Welcome to beijing arg: $OPTARG";; n) echo "your name is $OPTARG";; a) echo "Your age is $OPTARG " ;; :) echo "The -h arg must have one args";; ?) echo "Please input your select args" ;; esacdone
: Colon in case, when you ignore the system command error (that is, add: number), there is a parameter-H followed by a colon, the representative must follow an option parameter, if not followed by the option parameter, it will prompt for your custom error message,-A is not required to provide parameter options ( Because there is no colon appended to the letter, the script is executed without a parameter (or when the-a option is not followed), and no error is given.
? Case question mark, when the following parameters such as-e does not exist when the error
All of the above error $optarg variables are not available
1.1.4 Script Query IP geolocation
Script
#!/bin/bashgetip=$ (curl-s ip.cn?ip=$1) iparea=$ (Echo $Getip |awk-f ":" ' {print $} ' |awk ' {print $} ') ipisp=$ (Echo $Getip |awk-f ":" ' {print $} ' |awk ' {print $} ') if [!] $];thenip=$ (echo $Getip |awk-f ": ' {print $} ' |awk ' {print $} ') echo $ IP $IParea $IPispelseecho $IParea $IPISPFI
Execution results
[[email protected] script]#./getip.sh (current extranet IP address) 117 ... Beijing.... [Email protected] script]#/getip.sh 8.8.8.88.8.8.8 US google[[email protected] script]#./getip.sh 114.114.114.114114.114.114.114 Jiangsu province Nanjing Wind Network
2.0 Expect Introduction
Installation
Yum Install Expect-y
Script
#!/usr/bin/expect-f set IP [lindex $argv 0] Set password "111111" spawn ssh [email protected] $ipset timeout expect { "Yes/no" {send "yes\r"; exp_continue} "password:" {send "$password \ r"}}expect "]#" send "free-m\r" int Eract#expect EOF #exit
Execution results
[[email protected] learn]# hostname bj-idc-15[[email protected] learn]# ./ Test1.exp 10.10.10.14 spawn ssh [email protected][email protected] ' s password: last login: mon apr 11 16:24:49 2016 from 10.10.11.15[[ email protected] ~]# free -m total used free shared buffers cachedmem: 7870 2782 5088 0 341 2219-/+ buffers/cache: 220 7650swap: 3999 57 3942[[email protected] ~]# hostname bs01
This article is from the "Hu San DAO" blog, please be sure to keep this source http://bdstravel.blog.51cto.com/8870307/1762647
Introduction to Linux shell and expect dead