=========命令=========
--echo
转义\n\t
echo -e '1\t2\n3'
-e enable interpretation of backslash escapes
带颜色输出:
echo -e '\e[1;31m red \e[0m'
\e[显示方式;前景色;背景色m
可以将所有控制参数都用上,也可以只使用前景色或背景色
显示方式 意义
0 终端默认设置
1 高亮显示
4 使用下划线
5 闪烁
7 反白显示
8 不可见
前景色 背景色 颜色
30 40 黑色
31 41 红色
32 42 绿色
33 43 黃色
34 44 蓝色
35 45 紫红色
36 46 青蓝色
37 47 白色
\e[0m 关闭颜色模式
取消命令交互式:
有些命令是在交互式命令行中接收标准输入,直接传空到标准输入中交给命令,大多是直接退出
# 获取证书
echo -n | openssl s_client -showcerts -connect docker-hub.tools.com:443 2>/dev/null| sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
-n do not output the trailing newline
# 执行python
echo "dic={'a':1,'b':2};print dic['a']"|python
或:
]echo "
> import json
> ss = {'a':1,'b':2}
> print json.dumps(ss)
> " | python
# oracle脚本执行完直接退出
echo -n|sqlplus test/[email protected]/test @test.sql
#类似:
echo -n|supervisorctl
echo -n|impala-shell
取消脚本交互:
echo xxx.war | ./tools/down_wars.sh
echo -e '111\n222\n' | ./tools/xxx.sh
echo -e "${arg2}\n"|xxx.sh ${arg1}
--xargs
xargs -i COMMAND {}
xargs -I [] COMMAND []
-0, --null Items are separated by a null, not whitespace.
Disables quote and backslash processing
-I R same as --replace=R (R must be specified)
-i,--replace=[R] Replace R in initial arguments with names
read from standard input. If R is
unspecified, assume {}
删除文件最好用第二种(防误删)
$ find . -type f -name "*.txt" -print | xargs rm -f
$ find . -type f -name "*.txt" -print0 | xargs -0 rm -f
-----read----------
read -n 2 var #-n 2 输入两个字符后自动结束(不用回车)
read -p 'input:' var # input:wahaha -p 提示信息
read -s var #不回显(不显示输入内容,可以用作密码)
read -t 1 var #-t 1 1秒后超时(可能需要配合空值判断与默认值)
read -d ':' var #定界结束符为:(回车不结束,只有输入指定的符号才结束,注:只能使用单个字符)
--tee
同时输出到文件和标准输出:
ls 666 2>&1|tee 3.txt
tee -a XXX 追加
2>&1,默认tee不接受错误输出,需要重定向到标准输出
----------expect---------------
脚本中例:
for i in `grep "^1" $1`;do
expect << EOF
spawn ssh-copy-id $i #要执行的命令
set timeout 5 #设置超时时间,-1为不超时
expect { #带分支 expect
"*yes/no" {
send "yes\n" #发送命令,也可以"yes\r"
exp_continue #匹配到后继续后面分支
}
"*password" {
send "password01\n"
}
}
expect "*password" {send "password02\n"} #重试密码
.... #这里可以多加几个密码
expect eof #结束expect
EOF
done
命令行中:
expect -c "set timeout -1;spawn ssh-copy-id 192.168.80.166;expect \"yes/no\" { send \"yes\r\" ; };expect eof;"
expect -c 'set timeout -1;spawn ssh-copy-id 192.168.80.166;expect "yes/no" { send "yes\n" ; };expect eof;'
expect -c 等于
expect <<EOF
......
EOF
cat ssh(-C 大C,可不写) 命令类似,EOF可用别的符号代替,比如 !
-----printf------------
printf "%-5s %-10s %-4s\n" No Name Mark;printf "%-5s %-10s %-4.2f\n" 1 James 90.9989
如%-4.2f
-表示左对齐,默认右对齐
4表示宽度
.2f表示保留两位小数
常用控制符:
%d int
%s string
%f float
%x 16进制 %#x 可以输出标准格式,如printf '%#x\n' 111 >>0x6f
----cat------
cat -n pull_tag_push.sh |tr -s '\n'
-n 显示行号
--------------正则--------------------
(pattern)
匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 '\(' 或 '\)'。
(?:pattern)
匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。
(?=pattern)
正向肯定预查(look ahead positive assert),在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如,"Windows(?=95|98|NT|2000)"能匹配"Windows2000"中的"Windows",但不能匹配"Windows3.1"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
(?!pattern)
正向否定预查(negative assert),在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如"Windows(?!95|98|NT|2000)"能匹配"Windows3.1"中的"Windows",但不能匹配"Windows2000"中的"Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之后开始。
(?<=pattern) 反向(look behind)肯定预查,与正向肯定预查类似,只是方向相反。例如,"(?<=95|98|NT|2000)Windows"能匹配"2000Windows"中的"Windows",但不能匹配"3.1Windows"中的"Windows"。
(?<!pattern) 反向否定预查,与正向否定预查类似,只是方向相反。例如"(?<!95|98|NT|2000)Windows"能匹配"3.1Windows"中的"Windows",但不能匹配"2000Windows"中的"Windows"。
例:
匹配空白字符但不匹配换行:
(?:(?!\n)\s)+
注:后置条件在内,前置条件在外
查找非127.0.0.1的ip
ip a|grep -P 'inet (?!127.0.0.1)'