Last active
October 20, 2018 07:45
-
-
Save whatsrtos/18ca2f3eb749b0b2903f5e2221191e7a to your computer and use it in GitHub Desktop.
Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 数组 | |
ip_array=(10.10.80.158 10.10.80.159 10.10.80.160) | |
echo ${ip_array[*]} # 以一个字符串打印所有元素 | |
echo ${ip_array[@]} # 每个元素作为一个字符串 | |
echo ${#ip_array[*]} # 数组元素个数 | |
# 字符串 | |
len=${#str} #长度 | |
# if语句 | |
if [ -d /root/etc ]; then # 目录存在 | |
do_something | |
if [ -e /root/fff ]; then # 文件存在 | |
do_something1 | |
elif [ "$timeofday" = "no" ]; then # 字符串相等 | |
do_something2 | |
else | |
do_something3 | |
fi | |
# for循环 | |
for var in 1 2 3; do | |
echo $var | |
done | |
## 也可以写 | |
for var in 1 2 3 | |
do | |
echo $var | |
done | |
## 遍历数组 | |
array=(1 2 3) | |
for var in ${array[@]}; do | |
{ | |
echo $var | |
} & # 并行执行 | |
done | |
# switch语句 | |
case $var in | |
$condition1) | |
# do_something | |
;; | |
$condition2) | |
# do_something | |
;; | |
*) | |
exit 1 | |
;; | |
esac | |
## 参数个数判断, 参数从$0开始.. | |
if [ $# -lt 2 ] ; then | |
usage | |
exit 1 | |
fi | |
## 把变量名为$arr_name的数组赋值给$array | |
eval array=( \"\${${arr_name}[@]}\" ) | |
for host in "${array[@]}" | |
do | |
{ | |
echo -e "\n -- run cmd[$cmd] @${host} --" | |
ssh root@$host $cmd | |
} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment