Shell-related knowledge (1)

Source: Internet
Author: User


The Shell's own abilities.
Writing scripts is OK, at least now I'm running a script on the Internet
Program-Log scheduled backup and delete, NFS Shanghai volume file Delete and send mail.



"Data"
Export Path=/mall/jdk/jdk1.7/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Export LANG=ZH_CN. GB18030

start= ' Date +%s-d ' 2011-11-28 15:55:37 "'
end= ' Date +%s-d ' 2011-11-28 15:55:52 "'
echo $ (($end-$start))
Date "+%y-%m-%d%h:%m:%s"
Date-d ' +1 day ' + '%d ' Today +1
Date-s 20100405
Date-s 14:31:00

1. Late last month
Date-d ' Date +%y%m01 ' "-1 day" +%d
2. Forward
Date-d-' Date +%d ' Day +%d
3. Conditions
If [' Date +%d-d tomorrow ' = = 01];
4. Last month
Date-d Last-month
Date-d -1month
Date-d '-1 month ' + "%m"
5, next month
Date-d Next-month
Date-d 1month
6. Early this month
Date-d 1 ' Date +%b '
Early last month
Date-d 1 ' date-d last-month +%b '
Specify month
Day-d 1May

echo ' Date +%d ' | xargs-i{} date-d '-{} day ' +%y%m%d How Linux gets last month's end date


Sed-i "3s/.*/$table/g" conf/run.sql
Table=t_sms_mt_log_ ' date + "%m" ' this month, then the beginning of the month 1th is still counted last month
Date-d '-1 day ' + '%m ' this month, real this month
Subject= "Daily verification code issued quantity statistics _ ' date-d '-1 day ' +"%m%d ""

SELECT * from T_sys_user where user_code= ' duanzz ';

Update t_sys_user set terminal_id= ' 13730000000 ' where user_code= ' duanzz ';


Set Fileformat=unix

Intercept-Replace of string
Echo ${stringa:1} from the second place has been all
Echo ${stringa:2:5} intercepts 5 bits from the third position.

echo ${stringz: (-4)} Intercept from right to left
echo ${stringz:-4} Intercept from right to left, same as above
A=${STRINGA%%F} Remove the variable Stringa suffix F.
echo ${stringz/abc/xyz} variable in string ABC replaced by XYZ
echo ${stringz/#abc/xyz} with ' XYZ ' to replace the beginning ' abc '.
Echo ${STRINGZ/%ABC/XYZ} Replaces the end of ' abc ' with ' XYZ '.

You_id=${user}-on-${hostname}
you_id= $USER \-on-$HOSTNAME
echo $you _id root-on-db1 effect. Mixed output of variable and string
val=${1##+ (0)} # uses local variables to match the longest consecutive one or more 0.


Reference: ${backup_dir}/$DATE
#备份的变量:
Backup_dir=/home/mysql/mysql_bak
Date= ' date-d "+%y%m%d%h:%m:%d" '
Yes_time= ' date-d Yesterday "+%y%m%d" '
Mkdir-p/opt/chen$date\nailong Chen20160608nailong
Date-d "2 day Ago" + "%y-%m-%d" Yesterday date
date-d "1 week Ago" + "%y-%m-%d" last Sunday
Date +%y%m%d%h%m

The single quotation mark (') operation is basically the same as double quotes, but it is not allowed to reference a variable because the special meaning of $ is closed. In single quotation marks,
Any special characters are interpreted literally, except '. So the single quotation mark ("full reference") is a more than double quotation mark ("partial citation").
") more restrictive reference methods.


' Single quote mask all special characters
"" Double quotation marks mask some special characters

"Eval"

Two scans

Test.txt content: Hello Shell world!

Myfile= "Cat Test.txt"

(1) Echo $myfile #result: Cat test.txt

(2) eval echo $myfile #result: Hello Shell world!

"Echo"
ECHO-E handles special characters and interprets special characters. Effective special characters
Echo-n Non-newline output
Echo-e "A\NDDDD"//Automatic line wrapping
echo $ (ls-al) do not wrap echo "$ (ls-al)" Line wrap

bash$ Echo hello\!
Hello!
bash$ echo "hello\!"
hello\!
bash$ echo-e X\ty
Xty
bash$ echo-e "X\ty"
X y

echo "Chen" >> file: Append Chen content to file, without overwriting
echo "Chen" > file: Covered!!!


[A-za-z0-9]
Intercept Hello string: # var= ' Hello world! ' # echo ${var:0:5} Hello
Intercept World string: # Echo ${var:5:-1} World
Intercept the last character: # echo ${var: (-1)}!


########################  (( ... )) Structures can be used to calculate and test the results of an arithmetic expression.
#!/bin/bash
((5 > 4))
echo $?
echo "Exit Status of \" ((5 > 4)) \ "is $?."

"Input from keyboard-more than read"
Command << Word
Any input
Word


Read-p "Your command:" CMD
until [$CMD = = ' Q ']; Do
! Which $CMD && echo "wrong command" && read-p "Input again:" CMD && continue
Command= ' which $CMD | Grep-v "^alias" | Grep-o "[^[:space:]]\{1,\}" '
BINCP $COMMAND
echo "Copy $COMMAND finished."
Read-p "Continue:" CMD
Done

"Output to Screen"
Cat << Word
-----------
Name ID
Word
"Call in script" source _function.sh
"Shell function"
Hello ()
{
Echo ' Date '
}
Hello # # #这儿直接写hello
' Anti-quote allows you to assign the output of a shell command to a variable
today= ' Date +%y%m%d '
echo "Today is ' date +%y%m%d '"

Sleep 5 #过五秒再执行


"BaseName"
$ basename/tmp/test/file.txt
File.txt
$ basename/tmp/test/file.txt. txt
File

"DirName"
Dirname/var/log/messages get the path to a file or folder (previous level)
/var/log

Array
Var= ("123" "456" "789")
Var[1]=123
var[2]=456
echo "$var [1], $var [2]"
#

echo $? Condition is true, output 0, condition no, Output 1
CP 1.txt/opt/apps/2.txt >/dev/null 2>&1
Status=$?
echo $? #如果cp这个命令成功的话, output 0, unsuccessful, output 1

: The colon is here a placeholder, {str:=expr} if the variable str is not empty, ${str:=expr} equals the value of STR, and if Str is empty, the value of expr is assigned to STR
: >> target_file If this file does not exist before, create it
if [$name = ""] If this variable is a null value
Then: # doing nothing, branching out.
If [!-S 22.txt] If 22.txt this file is empty
or if [-Z 22.txt]

[$I-eq 1] && min= $MYRAND

If [-s $name] If this variable is empty
If [-D $path] If the directory exists
If [-s $path] if the directory exists
If [-N ""] # Tests whether there are command-line arguments (NOT null).
-A file exists YES!
-F This is a generic file
-D This is a directory
-B This is a fast device (floppy disk, hard drive, optical drive, etc.)
-C This is a character device (keyboard, sound card, etc.)
-P This file is a conduit
-H OR-l this is a symbolic link
-R file is readable
-W file is writable
-X file is executable
-O Whether you are a file owner
-G file Group-id is the same as you
F1-nt F2 file f1 than file F2 new
F1-ot F2 file F1 f2 older than file


Shell-related knowledge (1)

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.