shell常用功能
参考: https://github.com/spujadas/elk-docker/blob/master/elasticsearch-init
- 判断是否有root权限
if [ `id -u` -ne 0 ]; then
echo "You need root privileges to run this script"
exit 1
fi
- 判断配置文件是否存在
#CONF_FILE setting was removed
if [ ! -z "$CONF_FILE" ]; then
echo "CONF_FILE setting is no longer supported. elasticsearch.yml must be placed in the config directory and cannot be renamed."
exit 1
fi
- 判断启动脚本是否存在
if [ ! -x "$DAEMON" ]; then
echo "The elasticsearch startup script does not exists or it is not executable, tried: $DAEMON"
exit 1
fi
- 判断java是否安装
checkJava() {
if [ -x "$JAVA_HOME/bin/java" ]; then
JAVA="$JAVA_HOME/bin/java"
else
JAVA=`which java`
fi
if [ ! -x "$JAVA" ]; then
echo "Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME"
exit 1
fi
- 判断env是否存在
[ ! -z "$name" ] && echo 1
Shell数组的增删改查
参考: http://www.cnblogs.com/tangshengwei/p/5446315.html
http://c.biancheng.net/cpp/view/7002.html
http://www.runoob.com/linux/linux-shell-array.html
http://blog.csdn.net/redhat456/article/details/6068409
- 数组的定义,遍历
for((i=0;i<${#arr[*]};i++));do
echo ${arr[i]}
done
echo
echo '---------------'
for i in ${arr[@]};do
echo $i
done
- 取指定数组范围:
[root@bogon tmp]# a=(1 2 3 4 5)
#从下标1开始取,取3位。
[root@bogon tmp]# echo ${a[@]:1:3}
2 3 4
curl命令-网站如果3次不是200或301则报警
curl -o /dev/null -s -w "%{http_code}" baidu.com
-k/--insecure 允许不使用证书到SSL站点
-H/--header 自定义头信息传递给服务器
-I/--head 只显示请求头信息
-w/--write-out [format] 什么输出完成后
-s/--silent 静默模式。不输出任何东西
-o/--output 把输出写到该文件中
-L, --location
-o, --output
curl -L -o /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)"
curl -s "http://{ESIP}:9200/_cat/shards" | grep UNASSIGNED
curl -H 'Content-Type: application/json' -XPUT 'http://localhost:9200/_template/filebeat' -d@/etc/filebeat/filebeat.template.json
linux正则
参考: http://blog.csdn.net/Hello_Hwc/article/details/40017833
- 基本
. 匹配任何单个字符
* 前面出现0个或者多个
^ 以..开始
$ 以..结束
- 举个例子
china : 匹配此行中任意位置有china字符的行
^china : 匹配此以china开关的行
china$ : 匹配以china结尾的行
^china$ : 匹配仅有china五个字符的行
[Cc]hina : 匹配含有China或china的行
Ch.na : 匹配包含Ch两字母并且其后紧跟一个任意字符之后又有na两个字符的行
Ch.*na : 匹配一行中含Ch字符,并且其后跟0个或者多个字符,再继续跟na两字符
- 扩展正则
? : 匹配前面正则表达式的零个或一个扩展
+ : 匹配前面正则表达式的一个或多个扩展
{n,m}: 前面出现1个或2个或3个
| : 匹配|符号前或后的正则表达式
( ) : 匹配方括号括起来的正则表达式群
grep
- 参数
-n, --line-number
-i, --ignore-case 不区分大小写
-r, --recursive 按照目录
-o, --only-matching 只显示匹配行中匹配正则表达式的那部分
-v, --invert-match 排除
-c, --count 统计url出现次数
grep -nr
grep -oP
- 过滤ip
192.168.100.100
ifconfig|grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}"
- 过滤邮箱
cat >>tmp.txt<<EOF
iher-_@qq.com
hello
EOF
cat tmp.txt|grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+"
- 统计baidu关键字的url在这个大文件中出现的次数
$ cat >file.txt<<EOF
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
EOF
grep -cn ".*baidu.com.*" file.txt
3
过滤文件的空行
sed '/^$/d' file.txt
grep -v '^$' file.txt
sed过滤行,并替换这行
-n, --quiet, --silent sed -n '/my/p' datafile 这里要加-n
sed '/^$/d' file.txt 这里不用加-n
-i, --in-place[=SUFFIX] sed -i 's#lanny#jeffery#g' test.txt
-r, sed -r 's#(.*)#chkconfig \1 off#g' test.txt
ifconfig eth0|sed -n '2p'|sed -r 's#^.*addr:##g'|sed -r 's# B.*$##g'
chkconfig |awk -F' ' '{print $1}'|sed -r 's#(.*)#chkconfig \1 on#g'
- 过滤ip地址
$ ifconfig eth0|sed -n '2p'
inet addr:192.168.14.134 Bcast:192.168.14.255 Mask:255.255.255.0
$
$ ifconfig eth0|sed -n '2p'|sed -r 's#^.*addr:##g'
192.168.14.134 Bcast:192.168.14.255 Mask:255.255.255.0
$
$ ifconfig eth0|sed -n '2p'|sed -r 's#^.*addr:##g'|sed -r 's# B.*$##g'
192.168.14.134
- 替换两个字符位置
echo "oldboy oldgirl"|sed -r 's#(.*) (.*)#\2 \1#g'
- 打印行
# 打印第3行
$ sed -n '3p' test.txt
jeffery
# 打印第2-3行
$ sed -n '2,3p' test.txt
liyao
jeffery
- 删除行
#删除第二到第五行
sed '2,5d' datafile
# 删除包含"My"的行到包含"You"的行之间的行
sed '/My/,/You/d' datafile
一、条件选择、判断(if·、case)
二、四个循环(for、while、until、select)
三、循环里的一些命令与技巧(continue、break、shift…)
参数
$1 $2
$# 总个数
$? 执行结果(函数return获取)
$* 所有参数,(整体)
$@
clear
set -ue
#for i in "$*";do
# printf "\$i is %s\n" $i
#done
for i in "$@";do
echo $i
done
for i in "$*";do
echo $i
done
输入输出
- echo
- printf
tee
判断文件,目录
-d -f- 序列
{1..10}
seq 10
seq 10 |sed 's#^#$#g'|tr "\n" " "
seq -s " " 15|sed 's# # $#g'
数据类型
数值
比较 ((10<20))
相加
if((10<20));then
echo 123213123123213213213213
fi
字符串
比较[ 'maotai' == 'maotai1' ]
[[ $num =~ [^0-9] ]]
条件
if then fi
if
if else
if elif
case
case
case $ans in
'ok' | 'yes')
echo 'yes'
;;
case $ans in
[nN][oO][nN])
echo no
;;
for循环
for do doen
for i in 列表;do{}done
for((i=0;i<10;i++)) ## 注意这里是两个))
for((1,2,3))
- 写法: 举个例子
clear
- 第一种
for i in $(seq 10);do
echo $i
sleep 1
done
- 第二种
for((i=1;i<=10;i++));do
echo $i
done
while循环
while :;do
done
函数
function
local
shell技巧
- 1、生成随机字符 cat /dev/urandom
生成8个随机大小写字母或数字 cat /dev/urandom |tr -dc [:alnum:] |head -c 8
- 2、生成随机数 echo $RANDOM
确定范围 echo $[RANDOM%7] 随机7个数(0-6)
echo $[$[RANDOM%7]+31] 随机7个数(31-37)
- 3、echo打印颜色字
echo -e "\033[31malong\033[0m" 显示红色along
echo -e "\033[1;31malong\033[0m" 高亮显示红色along
echo -e "\033[41malong\033[0m" 显示背景色为红色的along
echo -e "\033[31;5malong\033[0m" 显示闪烁的红色along
color=$[$[RANDOM%7]+31]
echo -ne "\033[1;${color};5m*\033[0m" 显示闪烁的随机色along
print_green() {
printf '%b' "\033[92m$1\033[0m\n"
}
a约等于 b b是a的子字符串
参考:
http://blog.csdn.net/ithomer/article/details/6836382
https://unix.stackexchange.com/questions/249869/meaning-of-101/249870
a1="ithomer"
a2="ithomer.net"
a3="blog.ithomer.net"
if [[ "$a3" =~ "$a1" ]]; then
echo "$a1是$a3的子串!"
else
echo "$a1不是$a3的子串!"
fi
if [[ "$a3" =~ "$a2" ]];then
echo "$a2是$a3的子串!"
else
echo "$a2不是$a3的子串!"
fi