1.非root账户免密
ssh-copy-id -i ~/.ssh/id_rsa.pub 目标主机非root账户名@目标主机IP -p 目标主机端口
2.通常ls列出的文件,想直接管道通过rm -rf删除是无效的.这时就要配合命令xargs使用了:
例如: ls -t|tail -10|xargs rm -rf //按时间排序,删除最后的10个文件
3.使用ssh执行多段命令
ssh 目标主机非root账户名@目标主机IP -p 目标主机端口 'cd tmp/tree && cat tree.log'
4.ansible使用指南
https://blog.csdn.net/zfw_666666/article/details/124691877
5.shell获取文件md5
md5=`md5sum aaa.txt | awk '{ print $1 }'`
echo zipMd5: $md5
6.expect脚本结束需要用expect eof
#!/usr/bin/expect
set PROJECT [lindex $argv 0]
set DOWNLOAD_PATH [lindex $argv 1]
set timeout -1
root@13.xx.xx.60:/srv/obs/build/ /data1/obsrpms/
spawn rsync -v -a -e "ssh -p 10000" secure@13.50.63.60:/srv/obs/build/$PROJECT $DOWNLOAD_PATH
expect "Password:"
send "passwd123\r"
expect eof
7.Linux中sed命令功能强大
命令格式1:sed 's/原字符串/新字符串/' 文件
命令格式2:sed 's/原字符串/新字符串/g' 文件
外部应用变量使用双引号
8.k8s中 Fatal error: can't check configuration file '/etc/privoxy/config': Permission denied
subPath权限不够 加777权限
9.在shell中变量的赋值有五种 :使用 read 命令,直接赋值,使用命令行参数,使用命令行的输出结果,从文件读取
1. 直接赋值,格式为:变量名=变量值
2. Read 命令是系统内置命令,语法格式为: read 变量1 变量2
3. 使用命令行参数赋值:$命令 参数1 参数2 参数3
4. 利用命令的输出结果赋值:
currentdir=`pwd`
echo $currentdir
5. 从文件中读入数据:
#!/bin/bash
ls *.sh > execfile
while read LINE
do
chmod a+x $LINE
done < execfile
10.Shell判断字符串包含关系的几种方法
1)通过grep来判断:
str1="abcdefgh"
str2="def"
result=$(echo $str1 | grep "${str2}")
if [[ "$result" != "" ]]
then
echo "包含"
else
echo "不包含"
fi
先打印长字符串,然后在长字符串中 grep 查找要搜索的字符串,用变量result记录结果,如果结果不为空,说明str1包含str2。如果结果为空,说明不包含。这个方法充分利用了grep 的特性,最为简洁。
2)字符串运算符
str1="abcdefgh"
str2="def"
if [[ $str1 =~ $str2 ]]
then
echo "包含"
else
echo "不包含"
fi
利用字符串运算符 =~ 直接判断str1是否包含str2。
3)利用通配符
str1="abcdefgh"
str2="def"
if [[ $str1 == *$str2* ]]
then
echo "包含"
else
echo "不包含"
fi
用通配符*号代理str1中非str2的部分,如果结果相等说明包含,反之不包含。
4)利用case in 语句
str1="abcdefgh"
str2="def"
case $str1 in
*"$str2"*) echo Enemy Spot ;;
*) echo nope ;;
esa
5)利用替换
str1="abcdefgh"
str2="def"
if [[ ${str1/${str2}//} == $str1 ]]
then
echo "不包含"
else
echo "包含"
fi
11.Shell 入门教程(十六):如何进行文件判断
#!/bin/bash
# author:陈树义
# site:http://www.shuyi.me
file="/User/chenshuyi/hello.sh"
if [ -r $file ]
then
echo "文件可读"
else
echo "文件不可读"
fi
if [ -w $file ]
then
echo "文件可写"
else
echo "文件不可写"
fi
if [ -x $file ]
then
echo "文件可执行"
else
echo "文件不可执行"
fi
if [ -f $file ]
then
echo "文件为普通文件"
else
echo "文件为特殊文件"
fi
if [ -d $file ]
then
echo "文件是个目录"
else
echo "文件不是个目录"
fi
if [ -s $file ]
then
echo "文件不为空"
else
echo "文件为空"
fi
if [ -e $file ]
then
echo "文件存在"
else
echo "文件不存在"
fi
12.SHELL判断文件是否包含某个字串
比如说,判断/etc/ld.so.config是否包含指定目录:
grep "/opt/quantum6/bin" /etc/ld.so.conf >> /dev/null
if [ $? -ne 0 ]; then
echo not found
fi