随手记
随手记
bash shell脚本
export :查看所有变量、
[root@centos7 ~]#pstree -p 查看进程树
systemd(1)─┬─ModemManager(618)─┬─{ModemManager}(646)
│ └─{ModemManager}(651)
├─NetworkManager(612)─┬─dhclient(743)
│ ├─{NetworkManager}(675)
│ └─{NetworkManager}(683)
├─VGAuthService(625)
PS1不是环境变量 在vim脚本中不现实
$PSAT是环境变量 在子进程中加以显示、
有些是不可变的只读变量 有些可以更改
自定义的变量 exit退出再进来就没了
[root@centos7 ~]#echo $SHLVL嵌套深度
1
[root@centos7 ~]#echo $_ $_ 显示前一个变量的最后一个参数 一个字符串
hostname.sh
[root@centos7 ~]#man bash查看所有变量的信息
[root@centos7 ~]#echo $BASHPID 当前进程编程号
2246
[root@centos7 ~]#echo $$
2246
[root@centos7 ~]#echo $PPID M上级进程编号
2241
[root@centos7 ~]#echo $PI
3.14
[root@centos7 ~]#readonly 查看常量 -p也一样
[root@centos7 ~]#readonly PI readonly设置变量只读
[root@centos7 ~]#PI=11
-bash: PI: readonly variable
echo "1st arg is $1"
echo "10st arg is ${10}" 1-9可以 10要加花括号
#********************************************************************
echo "1st arg is $1"
echo "10st arg is ${10}"
echo "All arg are $*"所有参数有哪些
echo "ALL tast_agr are $@" 所有参数
echo "the scriptname is `basename $0`"脚本名字 如果显示文件路径加红色`basename`后面加空格
echo "the arg number is $#" 个数
[root@centos7 ~]#bash tast_agr.sh {a..z} 生成文件
1st arg is a
10st arg is j
All arg are a b c d e f g h i j k l m n o p q r s t u v w x y z
ALL tast_agr are a b c d e f g h i j k l m n o p q r s t u v w x y z
the scriptname is tast_agr.sh
the arg number is 26
#********************************************************************
echo "1st arg is $1"
echo "10st arg is ${10}"
echo "All arg are $*"
echo "ALL tast_agr are $@"
set -- 加上这个 往下的变量就失效了
echo "the scriptname is `basename $0`"
echo "the arg number is $#"
#!bin/bash
Color="\e[1;5;$[RANDOM%7+31]m"
End="\e[0m"
echo -e "$Color hi 梁连城 回头是岸 放下屠刀 $End"
|| $$
cmd1 && cmd2 若cmd1执行正确 ($?=0),则开始执行后面的cmd2
若cmd1执行错误 ($?=1),则不执行cmd2
cmd1 || cmd2 若cmd1执行正确 $?=0 ,则不执行cmd2
若cmd1执行错误 $?=1, 则执行cmd 2
命令执行 返回码 1 0
[root@centos7 ~]#name=cai xiaoyang
bash: xiaoyang: command not found...
[root@centos7 ~]#name="cai xiaoyang"
[root@centos7 ~]#[ "$name" = "cai xiaoyang" ];echo $?
0
[root@centos7 ~]#[ $name = "cai xiaoyang" ];echo $? $name 不加双引号不行
-bash: [: too many arguments
2
[root@centos7 ~]#[ "$name" = "cai xiaoyang" ];echo $? 加上就对了
0
磁盘报警脚本
warning=11
disk=`df | grep "^/dev/sd" | tr -s " " "%" | cut -d"%" -f5 | sort -nr | head -n1`
[ $disk -gt $warning ] && echo "mane"
磁盘报警
wawa=11
haha=`df|grep "/dev/sd" |grep -Eo "([0-9]+)%"|cut -d "%" -f1|sort -nr|head -1`
[ "$haha" -gt "$wawa" ] && echo "硬盘满了"
检测ip通不通脚本
[ $# -eq 0 ] && echo "please input a canshu" && exit 1|| ping -c1 -w2 $1 &>/dev/null && echo "the host is up" ||echo "the host is down"
~
[root@centos7 ~]#str=abc;[[ "$str" = "abc" ]] && echo true || echo false
true [[]]]里面若想等 就打印true
运算
let
let计算后不会显示在屏幕上 echo $n取得结果
[root@centos7 ~]#let 1+2
[root@centos7 ~]#let n=1+2
[root@centos7 ~]#echo $n
3
[root@centos7 ~]#let n=1+2
[root@centos7 ~]#echo $n
3
[root@centos7 ~]#n=$[3*5] 也可以这么算
[root@centos7 ~]#echo $n
15
[root@centos7 ~]#n=$((2*7))
[root@centos7 ~]#echo $n
14
expi计算
[root@centos7 ~]#expr 3 + 4 3和4不能连着写 用空格隔开才能够
7
[root@centos7 ~]#expr 2 \* 7 ✖要转义才行
14
[root@centos7 ~]#declare -i n=3+3 计算
[root@centos7 ~]#echo $n
6
let
[root@centos7 ~]#i=2 赋予i等于2
[root@centos7 ~]#let i++ i++=i+1
[root@centos7 ~]#echo $i
3
[root@centos7 ~]#let i++ 每+一边就会加2
[root@centos7 ~]#let i++
[root@centos7 ~]#echo $i
5
[root@centos7 ~]#i=10;let h=i++;echo h=$h加号在前先赋值再加
h=10
[root@centos7 ~]#i=10;let h=i++;echo h=$h
h=10
[root@centos7 ~]#i=10;let h=++i;echo h=$h 把i++变成11 再赋予给h 加号在后先加再赋值
h=11
[root@centos7 ~]#echo $[RANDOM%63+1]
11
[root@centos7 ~]#echo $[-RANDOM%63+1]
-19
[root@centos7 ~]#echo $[-RANDOM%63+1]
-47
[root@centos7 ~]#echo $[-RANDOM%63+1]
1
[root@centos7 ~]#echo $[-RANDOM%63+1]
-19
对63取模 取随机数
#!bin/bash
Color="\e[1;5;$[RANDOM%7+31]m"
End="\e[0m"
echo -e "$Color hi 梁连城 回头是岸 放下屠刀 $End"
[root@centos7 ~]#i=100;let i+=10;echo $i
110
#!/bin/bash
usera=$(cat -n /etc/passwd | head |tail -n1|cut -d":" -f3)
echo "id为10的uid:$usera"
userb=$(cat -n /etc/passwd | head -n20|tail -n1|cut -d":" -f3)
echo "id为20的uid:$userb"
# userall=$usera+$userb
let usera=$usera+$userb
echo "id和为: $usera"
exit 0
与 &
[root@centos7 ~]#echo $[3&2] 3的二进制 和2的二进制相与 与零相与等于零 1和一相与等于一
2
[root@centos7 ~]#echo $[9&2] 二进制相与的结果
0
或计算 |
[root@centos7 ~]#echo $[9|2]
11
01001计算过程
00010
01011=11
false 为真
[root@centos7 ~]#false
[root@centos7 ~]#echo $?
1
[root@centos7 ~]#true 为假
[root@centos7 ~]#echo $?
0
异或算法^: 相同为0 不同为1 1 ^ 1 =0 1 ^ 0 =1
[root@centos7 ~]#echo $[12^24]
20 异或结果20 二进制对比
01100
11000
10100
同或算法 :相同为 1 不同为0
[root@centos7 ~]#str1=magedu
[root@centos7 ~]#echo $str1
magedu
[root@centos7 ~]#str=mageedu
[root@centos7 ~]#test $str1 = $str
[root@centos7 ~]#echo $? 表示他俩不相等 1
1
[root@centos7 ~]#str=magedu
[root@centos7 ~]#test $str1 = $str
[root@centos7 ~]#echo $?
0相等是零 为真
[root@centos7 ~]#test "$str1" != "$str" !=为不相等 问不相等吗?
[root@centos7 ~]#echo $?
1 结果是相等的 是假的 1
[root@centos7 ~]#[ "$str1" != "$str" ]
[root@centos7 ~]#echo $?
1 跟上个做法一样 []写法代替echo
-z 为空吗 为空就是真0 不为空就是1
[root@centos7 ~]#var='' ;[ -z $var ];echo $?
0
[root@centos7 ~]#var='f' ;[ -z $var ];echo $?
1
[root@centos7 ~]#[ $a = $b ] a有东西 b没东西的时候
-bash: [: hah: unary operator expected 要加双引导才行
[root@centos7 ~]#[ "$a" = "$b" ]
[root@centos7 ~]#[ "$a" = "$b" ]|echo $?
1
[root@centos7 ~]#[ "$a" = "$b" ];echo $?
1
[root@centos7 ~]#unset a b 清空变量内容
[root@centos7 ~]#[ "$a" = "$b" ];echo $?
0
删除变量的时候 一定要判断该变量有没有值
变量
[root@centos7 bin]#test.sh
centos7.com
test.sh
/root/bin/test.sh: line 3: name: command not found
[root@centos7 bin]#echo $? 显示上一条命令执行结果争取与非 0是正确 非零错误
127
[root@centos7 bin]#vim test.sh
hostname
ls
hostname
exit 100 状态码 可以更改 echo $? 显示更改的状态码
0 代表成功,1-255代表失败 • $? 变量保存最近的命令退出状态
~
脚本里有exit的时候如果用 .执行终端会退出 点相当于当前shell 执行exit
readonly 查看常量 read:查看人键入的变量
[root@centos7 ~]#read -p$ t10 -p定义变量符号$
$pin=10101
[root@centos7 ~]#read -p$ -t2 等待两秒
$[root@centos7 ~]#
declare -r 是常量 -x是环境变量
[root@centos7 ~]#readonly 查看常量 -p也一样
[root@centos7 ~]#readonly PI readonly设置变量只读
[root@centos7 ~]#PI=11
-bash: PI: readonly variable
网络
[root@centos7 ~]#systemctl stop NetworkManager停用网络管理器
[root@centos7 ~]#service network restart重启网络
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=ens33
DEVICE=ens33
ONBOOT=yes
取消mc地址 v4地址没用的