文章目录
临时记录、未分类整理一、语法脚本参数条件判断用户输入循环判断数组
二、实用脚本配置bond
临时记录、未分类整理
按行读取文件
cat $FILENAME | while read LINE
do
echo $LINE
done
并行执行
参考:https://blog.csdn.net/yangshangwei/article/details/87691890
组合变量
prefix
=123
suffix
=abc
union
=eval
echo '$'$prefix$suffix
echo
echo -n
echo -e
日期指定为几天前
[root@simwor ~
]
2021-01-23
[root@simwor ~
]
2021-01-25
[root@simwor ~
]
2021-01-26
[root@simwor ~
]
计算
i
=$
[ $i + 1
]
一、语法
脚本参数
符号说明
$#参数的个数$0命令本身$n第N个参数$*整体参数字符串$@整体参数数组
[root@ansible ~
]
echo "\$# := $#"
echo "\$0 := $0"
echo "\$* := $*"
echo "\$@ := $@"
echo ""
echo "for i in \"\$*\""
for i
in "$*"; do echo $i; done
echo ""
echo "for i in \"\$@\""
for i
in "$@"; do echo $i; done
[root@ansible ~
]
[root@ansible ~
]
$
$0 :
= var.sh
$* :
= 1 2 3 4 5
$@ :
= 1 2 3 4 5
for i
in "$*"
1 2 3 4 5
for i
in "$@"
1
2
3
4
5
[root@ansible ~
]
条件判断
文件判断
判断说明
[ -a FILE ]如果 FILE 存在则为真。[ -e FILE ]如果 指定的文件或目录存在时返回为真。[ -d FILE ]如果 FILE 存在且是一个目录则返回为真。[ -f FILE ]如果 FILE 存在且是一个普通文件则返回为真。[ -s FILE ]如果 FILE 存在且大小非0时为真则返回为真。[ -r FILE ]如果 FILE 存在且是可读的则返回为真。[ -w FILE ]如果 FILE 存在且是可写的则返回为真。(一个目录为了它的内容被访问必然是可执行的)[ -x FILE ]如果 FILE 存在且是可执行的则返回为真。
字符串判断
判断说明
[ -z “STRING” ]如果STRING的长度为零则返回为真,即空是真[ -n “STRING” ]如果STRING的长度非零则返回为真,即非空是真[ STRING1 ]如果字符串不为空则返回为真,与-n类似[ STRING1 == STRING2 ]如果两个字符串相同则返回为真[ STRING1 != STRING2 ]如果字符串不相同则返回为真[ STRING1 < STRING2 ]如果 “STRING1”字典排序在“STRING2”前面则返回为真。[ STRING1 > STRING2 ]如果 “STRING1”字典排序在“STRING2”后面则返回为真。
数值判断
判断说明
[ INT1 -eq INT2 ]INT1和INT2两数相等返回为真 ,=[ INT1 -ne INT2 ]INT1和INT2两数不等返回为真 ,<>[ INT1 -gt INT2 ]INT1大于INT2返回为真 ,>[ INT1 -ge INT2 ]INT1大于等于INT2返回为真,>=[ INT1 -lt INT2 ]INT1小于INT2返回为真 ,<[ INT1 -le INT2 ]INT1小于等于INT2返回为真,<=
逻辑判断
判断说明
[ ! EXPR ]逻辑非,如果 EXPR 是false则返回为真。[ EXPR1 -a EXPR2 ]逻辑与,如果 EXPR1 and EXPR2 全真则返回为真。[ EXPR1 -o EXPR2 ]逻辑或,如果 EXPR1 或者 EXPR2 为真则返回为真。[ ] || [ ]用OR来合并两个条件[ ] && [ ]用AND来合并两个条件
用户输入
打印提示
[root@ansible ~
]
echo ""
read -p
"This is a prompt line by \"read\":" prompt
echo $prompt
echo ""
[root@ansible ~
]
This is a prompt line by
"read":ok
ok
[root@ansible ~
]
不对用户输入做转义
[root@ansible ~
]
echo ""
read -p
"Do not accept backslash : " prompt
echo $prompt
echo ""
read -r -p
"Backslash is ok : " backslash
echo $backslash
echo ""
[root@ansible ~
]
Do not accept backslash
: no\yes
noyes
Backslash is ok
: no\yes
no\yes
[root@ansible ~
]
设置超时时间
[root@ansible ~
]
echo ""
read -t 3 -p
"Watch your time : " watch
if [ -z
"$watch" ]
then
echo "Ops, you missed your chance."
else
echo $watch
fi
echo ""
[root@ansible ~
]
Watch your
time : Ops, you missed your chance.
[root@ansible ~
]
获取用户密码输入
参考:https://www.jianshu.com/p/52e16e9ce24e
[root@ansible ~
]
password
=''
echo -n
"Enter password: "
OLD_IFS
=$IFS
while IFS
= read -r -s -n1 char
do
if [ -z
$char ]
then
echo
break
fi
if [[ $char == $
'\x08' || $char == $
'\x7f' ]]
then
[[ -n
$password ]] && password
=${password:0:${#password}-1
}
printf '\b \b'
else
password+
=$char
printf '*'
fi
done
IFS
=$OLD_IFS
echo "Password is: $password"
[root@ansible ~
]
[root@ansible ~
]
Enter password: ********************
Password is: Today_is_a_good_day
!
[root@ansible ~
]
循环判断
case
case var
in
var-is-a
)
do-something
;;
var-is-b
| var-is-c
)
do-something
;;
*
)
report-something
esac
数组
基本使用
[root@master01 ~
]
declare -A log_levels
log_levels
[disaster
]="-------"
log_levels
[error
]="-----"
log_levels
[warn
]="---"
log_levels
[info
]="-"
echo ${log_levels[info]}
echo ${log_levels[*]}
echo ${log_levels[@]}
echo ${#log_levels[*]}
echo ${!log_levels[*]}
[root@master01 ~
]
-
--- ----- - -------
--- ----- - -------
4
warn error info disaster
[root@master01 ~
]
二、实用脚本
配置bond
脚本
#
!/bin
/bash
management_ip
="192.168.1.100/24"
transaction_ip
="192.168.1.200/24"
transaction_gw
="192.168.1.1"
# 网卡信息
enps
=`nmcli dev status
| awk
'{print $1}' | grep enp
| xargs`
# 筛选千兆和万兆网卡
enp1ks
=""
enp1ws
=""
for enp in $enps
do
if [ `ethtool $enp
| grep
-c
1000M`
== 1 ]
then
enp1ks
="$enp1ks $enp"
echo
"$enp speed is 1000Mb/s"
elif
[ `ethtool $enp
| grep
-c
10000M`
== 1 ]
then
enp1ws
="$enp1ws $enp"
echo
"$enp speed is 10000Mb/s"
fi
done
# 配置管理IP BOND
echo
"nmcli c add type bond ifname management-bond1 mode 1 ip4 $management_ip"
for enp1k in $enp1ks
do
echo
"nmcli c add type bond-slave ifname $enp1k master management-bond1"
done
# 配置业务IP BOND
echo
"nmcli c add type bond ifname transaction-bond4 mode 4 ip4 $transaction_ip gw4 $transaction_gw"
for enp1w in $enp1ws
do
echo
"nmcli c add type bond-slave ifname $enp1w master transaction-bond4"
done
运行
[root
@localhost ~]# sh extract
-eth
-info
.sh
enp0s3 speed is
1000Mb
/s
enp0s8 speed is
1000Mb
/s
enp0s10 speed is
1000Mb
/s
enp0s9 speed is
1000Mb
/s
nmcli c add type bond ifname management
-bond1 mode
1 ip4
192.168.1.100/24
nmcli c add type bond
-slave ifname enp0s3 master management
-bond1
nmcli c add type bond
-slave ifname enp0s8 master management
-bond1
nmcli c add type bond
-slave ifname enp0s10 master management
-bond1
nmcli c add type bond
-slave ifname enp0s9 master management
-bond1
nmcli c add type bond ifname transaction
-bond4 mode
4 ip4
192.168.1.200/24 gw4
192.168.1.1
[root
@localhost ~]#