3_ 自动化运维(CentOS 7) ---学习笔记

自动化运维(CentOS 7) —学习笔记

3.2.2 自定义之指令

alias
alias h5=`head -5`
unalias h5 

3.2.3 管道与重定向

rmp -qa |grep gcc
echo "pass" | passwd --stdin tom 
ls
 
ls > list.txt
hostname >> list.txt 
mail -s test [email protected] < list.txt
ls -l abc install.log 

ls -l abc install.log 2> error.txt
ls- l abc install.log >> all 2>&1 
ls -l abc install.log &> all 

3.3.1 技巧

echo "pass" | passwd --stdin root >/dev/null
id tom >> user2 2>> error // 2>> 错误信息重定向追加

3.3.2 后台任务

firefox 
firefox & 
ls /tmp ; ls /root ; ls /home 
ls test.txt && cat test.txt 
gedit || vim 
id tom &>/dev/null && echo "Hi,tom" || echo "No such user"

firefox & 
jobs 
fg 1 #将后台进程调回前台执行

3.3.4

echo {a,b,c}
echo user{1,5,8}
echo {0..10}
echo {0..10..2}
echo a{2..-1}
mkdir /tmp/{dir1,dir2,dir3}
ls -ld /tmp/dir{1,2,3}
chmod 777 /tmp/dir{1,2}

3.4.1

NAME=tomcat
echo $NAME    //显示变量的值

typeset -r NAME  #添加只读属性 
NAME=jerry

declare INT_NUMBER # 定义一个变量,不赋值
typeset -i INT_NUMBER # 设置整数变量
INT_NUMBER=test 
echo $INT_NUMBER
0

INT_NUMBER=200
echo $INT_NUMBER
200

read P_NUMBER  #回车后输入要赋值给P_NUMBER的值
echo $P_NUMBER

read -p "Please input a number:" P_NUMBER
echo $P_NUMBER

set # 查看当前系统中设置的所有变量及值
unset P_NUMBER # 删除变量

3.4.2 变量的适用范围

TEST=pass 
echo $TEST 
bash  # 在当前Shell下开启新进程bash
ehco $TEST 
exit 
export TEST 
export NAME=tom 

3.4.3

echo $PATH 
ls 
PATH=$PATH:/root 

3.4.4 位置变量

vim /tmp/test.sh 
#!/bin/bash 
#This is test script for parameter!
echo "This is the file name: $0"
echo "This is first parameter: $1"
echo "This is second parameter: $2"
echo "This is the number of all all parameter: $#"
echo "This is the all parameter: $*"
echo "This is the all parameter: $@"
echo "THis is PID $$"

bash /tmp/test.sh a b c 

3.4.5 变量的展开替换

": # % /"  
${: # % /}  
存在 删除头 删除尾 替换  


${varname:-word}    //如varname 存在且非null ,则返回其值,否则返回备用的word
${varname:=word}    //如varname 存在且非null ,则返回其值,否则设置为word
${varname:?message} //如varname 存在且非null ,则返回其值,否则显示varmessage:word
${varname:+word}   //如varname 存在且非null ,则返回备用的word,否则返回原值null

NAME=Jacob
echo ${NAME:-no user}; echo ${NAME}  //NAME存在且非空,输出$NAME,否则输出 no user 
echo ${NAME:=no user}; echo ${NAME}  //NAME存在且非空,输出$NAME,否则设置为 no user
echo ${NAME:?no defined}; echo ${NAME}//NAME存在且非空,输出$NAME,否则显示 no user
echo ${NAME:+OK}; echo ${NAME} //NAME存在且非空,返回输出OK

${variable#key}  //从头开始删除关键词,执行最短匹配
${variable##key}  //执行最长匹配
${variable%key}  //从尾开始
${variable%%key}  
${variable/old/new}  //将old替换为new 仅替换第一个
${variable//old/new} //替换所有

USR=$(head -1 /etc/passwd) //
echo $USR 
echo ${USR#*:}    //
echo ${USR##*:}
echo ${USR%*:}
echo ${USR%%*:}
echo ${USR/root/admin}
echo ${USR//root/admin}

3.4.6 数组

A[1]=11
A[2]=22
A[3]=33
echo ${A[1]},${A[2]},${A[3]}
echo ${A[1]}:${A[2]}:${A[3]}

echo ${A[*]}
echo ${A[@]}

B=(aa bbb cccc)
echo ${B[0]}:${B[1]}:${B[2]}

echo "length of B_0 is ${#B[0]}"
echo "length of B_1 is ${#B[1]}"
echo "length of B_2 is ${#B[2]}"
echo ${#B[@]}  # 返回数组中元素的个数  

3.4.7 算术运算与测试

test -d /etc/ && echo "Y" || echo "N"    //-d -directory 是否存在且为目录
test -d /etc/passwd && echo "Y" || echo "N"
[ -d /etc ] && echo "Y" || echo "N"      // test = [ ]
[ -e /etc/passwd ] && echo "Y" || echo "N"      //-e -exist 是否存在
[ -f /etc/passwd ] && echo "Y" || echo "N"     //-f -file 是否存在且为普通目录
[ -h /etc/grub.conf ] && echo "Y" || echo "N"     //-h -chain 是否为链接文件  
[ -n $PATH ] && echo "Y" || echo "N"  // -n -nonzero 字符串长度为非0

[ -z $tt ] && echo "Y" || echo "N"  // -z -zero  字符串长度为0
[ -z $PATH ] && echo "Y" || echo "N"

[ 22 -eq 22 ] && echo "Y" || echo "N"   //-eq  --equal
[ 10 -ne 22 ] && echo "Y" || echo "N"	//-ne  --no equal
[ 32 -gt 22 ] && echo "Y" || echo "N"	//-gt  --great    -ge  --great and equal
[ 10 -lt 22 ] && echo "Y" || echo "N"	//-lt  --litter   -le  --littre and equal 

3.5 Shell

3.5.1 \

echo *
echo \*  #把*作为字面意义的普通字符
echo \>
echo / \ 
> -name "test.txt" \
> -type f \
> -size +5M 

3.5.2

echo '$HOME' #还原字面意义 
echo 'test\'

3.5.3

echo "This is a book."
echo "$HOME" #不会屏蔽 ` \ $
echo "\$HOME" # \ 屏蔽 

3.5.4

echo "Today is `date +%D`."
echo "Today is $(date +%D)."

date "+%Y-%m-%d %H:%M:%S"

3.6 正则表达式

grep root /tmp/passwd    //grep what where
grep --color :..0: /tmp/passwd # 查找:与0:之间包含任意两个字符的字串
grep --color 00* /tmp/passwd # *匹配前一个字符出现零次或多次,查找包含至少一个0的行
grep --color o[os]t /tmp/passwd # 查找包含oot或ost的行
grep --color [0-9] /tmp/passwd  # 查找包含数字0-9的行 
grep --color [f-q] /tmp/passwd  # 查找包含字母f-q的行 
grep --color ^root /tmp/passwd  # 查找以root开头的行
grep --color bash$ /tmp/passwd  # 查找以bash结尾的行 
grep --color sbin/[^n] /tmp/passwd # 查找sbin/后面不跟n的行
grep --color '0\{1,2\}' /tmp/passwd # \{n,m\} # 查找数字0出现一次两次的行
grep --color "\(root\).*\1" /tmp/passwd # 查找包含两个root的行
grep --color "\(root\)\(:\).*\2\1" /tmp/passwd  # 查找包含一root:开头并以:root结尾的字符串
grep ^$ /tmp/passwd # 过滤文件的空白行
grep -v ^$ /tmp/passwd  # 过滤文件的非空白行

3.6.2 扩展正则表达式

egrep --color '0{1,2}' /tmp/passwd # 查找数字0出现1次和2次的行
egrep --color '0+' /tmp/passwd  # 查找包含至少一个0的行
egrep --color '(root|admin)' /tmp/passwd  # 查找包含root或者admin 的行

3.6.3 POSIX 规范

grep --color [[:digit:]] /tmp/passwd # 数字字符
grep --color [[:alpha:]] /tmp/passwd # 字母字符
grep --color [[:punct:]] /tmp/passwd # 标点字符
grep --color [[:space:]] /tmp/passwd # 任何产生空白的字符

[[:alpha:]]  字母字符
[[:alnum:]]  字母和数字字符
[[:cntrl:]]  控制字符
[[:digit:]]  数字字符
[[:xdigit:]]  十六进制数字字符
[[:punct:]]  标点字符
[[:graph:]]  非空格字符
[[:print:]]  任何可以显示的字符
[[:space:]]  任何产生空白的字符
[[:blank:]]  空格与Tab键字符
[[:lower:]]  小写字符
[[:upper:]]  大写字符

3.6.4 GUN规范

grep --color "i\b" /tmp/passwd # 匹配i结尾的单词 

你可能感兴趣的:(CentOS,7,学习笔记,Linux运维之道)