博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux Shell 小脚本经典收藏
阅读量:4843 次
发布时间:2019-06-11

本文共 15667 字,大约阅读时间需要 52 分钟。

## ====================================================#### auth:wonter                                                                                       #### date:2014-06-12                                                                                 #### to  :trust.sh                                                                                       #### ====================================================###!/bin/shtrust(){{echo -e "\n"} | ssh-keygen -t rsa -P ''#参数echo "请输入信任机ip:(提示:需输入三次密码!)"read ip#建立信任关系cd ~/.sshscp -r id_rsa.pub $ip:/root/.ssh/$ipssh $ip 'touch ~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys'ssh $ip "cat ~/.ssh/$ip >> ~/.ssh/authorized_keys"}trust

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 更新时间

echo 3 > /proc/sys/vm/drop_caches 清理缓存

python -m SimpleHTTPServer 启动python页面cgi访问

导出数据库

time=`date "+%Y-%m-%d"`

mysqldump -uroot -p123456 javame > /usr/local/mysql/data/bak/javame-${time}.sql

导入数据库(sql文件)

mysql -u abc -p abc < abc.sql

注意sql文件必须在当前目录下,如果不在当前目录下需要在< 之后加上具体sql文件路径

jenkins项目发布脚本

预安装:yum instal sshpass -y

ip="192.168.20.200"jenkinsurl="http://ip:port/job/portal"port="22"passwd="123456"tomcatpath="/root/tomcat-8.0"war_name="web-portal"get_war="${jenkinsurl}/ws/target/${war_name}.war"pid=`sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "ps -ef | grep $tomcatpath | grep -v grep | awk '{print \$2}'"`if [ "X${pid}" = "X" ]thensshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "rm -rf $tomcatpath/webapps/*"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "curl $get_war -o $tomcatpath/webapps/${war_name}.war"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "unzip $tomcatpath/webapps/${war_name}.war -d $tomcatpath/webapps/ROOT"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "rm -rf $tomcatpath/webapps/${war_name}.war"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "rm -rf $tomcatpath/webapps/ROOT/WEB-INF/classes/properties/*"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "$tomcatpath/bin/startup.sh" else sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "kill -9 \$(ps -ef | grep $tomcatpath | grep -v grep | awk '{print \$2}')"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "rm -rf $tomcatpath/webapps/*"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "curl $get_war -o $tomcatpath/webapps/${war_name}.war"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "unzip $tomcatpath/webapps/${war_name}.war -d $tomcatpath/webapps/ROOT"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "rm -rf $tomcatpath/webapps/${war_name}.war"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "rm -rf $tomcatpath/webapps/ROOT/WEB-INF/classes/properties/*"sshpass -p $passwd ssh $ip -p ${port} -o StrictHostKeychecking=no "$tomcatpath/bin/startup.sh"fi

 

删除空行、 删除第四行和第二行、删除第三行到尾行

sed -e '/^$/d'sed -e '4d' -e '2d'sed -e '3,$d' (sed '/info/,$d') #匹配 输出模式匹配1和模式匹配行2  $sed -n -e '/Storage/p' -e /Software/p thegeekstuff.txt  5. Storage  9. Software Development  清除linux缓存
 echo 3 > /proc/sys/vm/drop_caches 
 

匹配文件某一行下加一行内容

sed  '/123456/a\654321' bak.sql

提取两段之间的内容

sed -n '/kaishi/,/jieshu/{//!p}' bak.sql

sed里面加变量

'"$i"'

1、在两个文件中找出相同的号码

diff -y xx.txt oo.txt | egrep -v "<|>" | awk '{print $2}'

2、打印第几行到第几行之间

cat 1 | awk 'NR==2,NR==4{print}'

 

1、删除Linux远程用户连接会话

[root@tradx24 logs]# w 10:45:28 up 15 days, 16:23,  4 users,  load average: 0.00, 0.00, 0.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHATroot     tty1     -                Sun21    4days  0.00s  0.00s -bashroot     pts/0    192.168.1.2    09:11    0.00s  0.07s  0.00s wroot     pts/2    192.168.1.2    09:45   30:53   0.07s  0.07s -bashroot     pts/3    192.168.1.8    10:20   22:05   0.02s  0.00s vi operation.log[root@tradx24 logs]# skill -KILL -v pts/3pts/3    root     21217 vi              pts/3    root     24331 bash

2、通过ssh远程执行命令

[root@tradx24 bin]# iip="192.168.1.10"[root@tradx24 bin]# ssh $iip w 10:54:13 up 125 days, 20:17,  1 user,  load average: 0.00, 0.00, 0.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHATroot     tty1     -                21Mar14  4days  0.09s  0.09s -bash

3、通过ssh远程批量执行MySQL脚本

#iplist1=(`cat ../tools/dblist | grep -v \# | awk '{print $1}' | sed "1,1d"`)#userlist2=(`cat ../tools/dblist | grep -v \# | awk '{print $2}' | sed "1,1d"`)#passwordlist3=(`cat ../tools/dblist | grep -v \# | awk '{print $3}' | sed "1,1d"`)for((i=0;i<=${#list1[@]};i++));do        iip=`echo ${list1[i]} | awk -F "." '{print $4}'`        ssh ${list1[i]}  mkdir /home/sqlScrip/        ssh ${list1[i]}  mkdir /home/sqlScrip/$iip        clear        scp ../sqlScrip/$iip/* ${list1[i]}:/home/sqlScrip/$iip        clear        #path        list4=(`ls ../sqlScrip/$iip`)        for((w=0;w<=${#list4[@]};w++));        do                if [ "117" == "$iip" ]                then                        ssh ${list1[i]}  "mysql -h ${list1[i]} -u ${list2[i]} -p${list3[i]} < /home/sqlScrip/${iip}/${list4[w]};"                        clear                fi        done        ssh ${list1[i]} 'rm -rf /home/sqlScrip'done

4、通过svn下载war包

#list第五行为下载地址list=(`cat ../tools/list  | awk '{print $5}' | sed "1,1d"`)for((i=0;i<${#list[@]};i++));do        curl -u wangyong:Abcd1234 -O ${list[i]}        war=`echo ${list[i]} | awk -F "/" '{print $NF}'`        mv $war ../workapp/done

5、解压war包到指定目录

ssh ${ip} unzip ${tomcat目录}/webapps/${war包名称}.war -d  ${tomcat目录}/webapps/${指定目录}

6、多Linux服务器建立信任关系

ssh-keygen -t rsacd ~/.sshscp -r id_rsa.pub $ip:/root/.ssh/$ipssh $ip 'touch ~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys'ssh $ip "cat ~/.ssh/$ip >> ~/.ssh/authorized_keys"

7、删除某目录下空文件

find ../new/$serve/properties -type f -size 0 -exec rm -f {} \;

8、删除某目录下小于10b的文件

#Delete the files smaller than 10B        for i in `ls -l ../new/$serve/properties/ | sed -e '1d' | awk '$5 < 10 {print$NF}'`        do                rm -rf ../new/$serve/properties/$i        done

9、Linux通过alias设置快捷命令

command(){list1=(`cat ~/.bashrc | grep custom`)for((i=0;i<=${#list1[@]};i++));do        if [[ "${list1[i]}" = "#custom" ]]        then                echo "[warn] The command has already been initialized,there's no need to repeat the operation, thank you!"                break        else                path=`pwd`                #初始命令                #Initialization commands.                echo "#custom" >> ~/.bashrc                echo "alias l='ll -al'" >> ~/.bashrc                echo "alias conf='cd `echo $path`/../conf'" >> ~/.bashrc                echo "alias bin='cd `echo $path`/../bin'" >> ~/.bashrc                echo "alias new='cd `echo $path`/../new'" >> ~/.bashrc                echo "alias too='cd `echo $path`/../tools'" >> ~/.bashrc                echo "alias hostlist='cat `echo $path`/../tools/.hostlist'" >> ~/.bashrc                echo "alias un='cd `echo $path`/../'" >> ~/.bashrc                echo "alias bak='cd `echo $path`/../bak'" >> ~/.bashrc                echo "alias app='cd `echo $path`/../workapp'" >> ~/.bashrc                echo "alias ..='cd ../'" >> ~/.bashrc                source ~/.bashrc        fidone}

10、引用其他shell脚本方法

## ====================================================#### auth:wonter                                         #### date:2014-06-12                                     #### path:javame.cnblogs.com                             #### ====================================================###!/bin/sh#引用tool.sh脚本中方法. ../tools/tool.sh#Parameterspar#Capture ip#Capture pathconfig

11、获取脚本自身名称

#Automatically obtain configuration filesname=`echo $0`uname=`echo $name | awk -F "/" '{print $2}' | awk -F "." '{print $1}'`#Uniform configurationserve="$uname"

12、检查是否存在文件夹,没有则新建文件夹

bak_mk="../bak/$serve"new_mk="../new/$serve"if [ ! -d $bak_mk ]; thenmkdir $bak_mkfiif [ ! -d $new_mk ]; thenmkdir $new_mkfi 判断文件是否为空 if [[ -s $file ]]; then
    echo "not empty"
  fi

13、统计脚本执行时间

     st=`date +"%Y-%m-%d %H:%M:%S"`       #执行方法        start        en=`date +"%Y-%m-%d %H:%M:%S"`        st_c=`date -d  "$st" +%s`        en_c=`date -d  "$en" +%s`        interval=`expr $en_c - $st_c`        echo "Start start-up time   :${st}"        echo "Start end time        :${en}"        echo "total consuming time  :${interval} 秒"

14、shell定义集合

#namelist1=(`cat ../tools/list | grep -v \# | awk '{print $1}' | sed "1,1d"`)#iplist2=(`cat ../tools/list | grep -v \# | awk '{print $2}' | sed "1,1d"`)#servelist3=(`cat ../tools/list | grep -v \# | awk '{print $3}' | sed "1,1d"`)for((i=0;i<=${#list1[@]};i++));do        cp ../conf/.model ../conf/${list1[i]}.conf        cp ../bin/.model.wy ../bin/${list1[i]}.wy        cp ../bin/.model.yw ../bin/${list1[i]}.ywdone

15、记录脚本执行者用户与IP

#Perform operationecho "operator:"read mea=`tty | awk -F "/dev/" '{print $2}'`date "+%Y-%m-%d %H:%M:%S" >> ../logs/operation.logecho `w | grep $a | awk '{print $3}'` "${me} > Perform operation: one" >> ../logs/operation.log

16、shell中的正则语句

for i in `cat ../conf/$confs  | grep start | awk -F ">>" '{print $2}' | awk -F "_" '{print $1}'`do        if [[ "${i}" == "shiro" ]]        then                sed -n -e '/>>shiro/,/>>shiro/p'  $confs |grep -v '>>shiro' > $pro_shi                cat $pro_shi | col -b > ../temp/m1                cat ../temp/m1 > $pro_shi        elif [[ "${i}" == "jdbc" ]]        then                sed -n -e '/>>jdbc/,/>>jdbc/p'  $confs |grep -v '>>jdbc' > $pro_jdbc                cat $pro_jdbc | col -b > ../temp/m1                cat ../temp/m1 > $pro_jdbc        fi done

17、shell写个定时进程(不推荐我这个写法,应该有更好!)

## ====================================================#### auth:wonter                                         #### date:2014-06-12                                     #### to  :timing.sh                                      #### ====================================================###!/bin/shecho "请输入定时阀值(格式:`date +"%Y-%m-%d %H:%M"` )"        read timingecho "定时任务设置完毕,请安心等待..."ti(){while truedo        time=`date +"%Y-%m-%d %H:%M"`        if [[ "${time}" == "${timing}" ]]        then                echo "定时任务:"                #定时清理日志                rm -rf ../logs/*                #定时备份日志                #定时执行脚本                break        fidone}ti&

18、通过参数方式执行脚本 tools.sh

#!/bin/shtrust(){#建立信任echo "请输入信任机ip:(提示:需输入三次密码!)"read iptrusts}command(){#初始化命令command}case "$*" in        trust)        trust        ;;        command)        command        ;;        *)        echo "----------------------------------------"        echo "pls : http://javame.cnblogs.com  welcome"        echo "----------------------------------------"        echo "  主机信任     | or | 初始化命令(初始)  "        echo "tools.sh trust | or | tools.sh command"        echo "----------------------------------------"        ;;esac

19、终端发送回会话给其他用户 限root用户

echo "输入您想说的话吧:" read spkecho $spk >/dev/stdin >/dev/pts/2

20、某目录下所有文件统一批量修改ip

sed -i  "s/10.0.0.1/10.0.1.2/g" `grep "10.0.0.1" -rl /opt/uer/wy`

21、链接数与进程数

#链接数netstat -n | awk '/^tcp/ {++y[$NF]} END {for(w in y) print w, y[w]}'#统计httpd协议连接数进程数ps -ef|grep httpd|wc -lps aux|grep httpd|wc -l#句柄数lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr|more #查看进程占用内存ps aux |awk '($1 ~apache) && ($6>50) {print $0}'

22、Linux查看线程的三种方法

1、top -H手册中说:-H : Threads toggle加上这个选项启动top,top一行显示一个线程。否则,它一行显示一个进程。2、ps xH手册中说:H Show threads as if they were processes这样可以查看所有存在的线程。3、ps -mp 
手册中说:m Show threads after processes这样可以查看一个进程起的线程数。

23、查看连接某服务端口最多的的IP地址

netstat -nat | grep "192.168.1.20:8443" |awk '{print $5}'|awk -F: '{print $4}'|sort|uniq -c|sort -nr|head -20

24、常见压缩解压命名

.tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!).gz解压1:gunzip FileName.gz解压2:gzip -d FileName.gz压缩:gzip FileName.tar.gz 和 .tgz解压:tar zxvf FileName.tar.gz压缩:tar zcvf FileName.tar.gz DirName.bz2解压1:bzip2 -d FileName.bz2解压2:bunzip2 FileName.bz2压缩: bzip2 -z FileName.tar.bz2解压:tar jxvf FileName.tar.bz2压缩:tar jcvf FileName.tar.bz2 DirName.bz解压1:bzip2 -d FileName.bz解压2:bunzip2 FileName.bz压缩:未知.tar.bz解压:tar jxvf FileName.tar.bz压缩:未知.Z解压:uncompress FileName.Z压缩:compress FileName.tar.Z解压:tar Zxvf FileName.tar.Z压缩:tar Zcvf FileName.tar.Z DirName.zip解压:unzip FileName.zip压缩:zip FileName.zip DirName.rar解压:rar x FileName.rar压缩:rar a FileName.rar DirName

25、厚积薄发,持续更新积累,请关注javame.cnblogs.com

1.gzexe -d 解密

2.Address 192.168.3.202 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
解决方法:vim /etc/ssh/ssh_config 修改GSSAPIAuthentication 值为 no
3.打印指定行一下

sed -n -i '/PID/,+600000000 p' file

4.sed用法

在第一行插入数据sed -i 1"i\\$icare_ip" file只打印第三行sed -n '3p' datafile只查看文件的第100行到第200行sed -n '100,200p' file删除第二到第五行sed '2,5d' datafile删除包含"2014-10-19"的行到包含"2014-10-29"的行之间的行sed '/2014-10-19/,/2014-10-29/d' datafile删除包含"My"的行到第十行的内容sed '/My/,10d' datafile sed选项-e用于进行多重编辑 sed -e '1,10d' -e 's/My/Your/g' datafile

5.交互式输入

{echo "who"} | ssh 192.168.48.73

6.shell中的集合

list1=(`cat ../tools/list | grep -v \# | awk '{print $1}' | sed "1,1d"`)for((i=0;i<${#list1[@]};i++));dosh ./${list1[i]}.wydone

7.判断是否存在

if [ ! -d $bak_mk ]; thenmkdir $bak_mkfi

8.针对^M

dos2unix

9.获取最后登录系统的ip

last -n 5

10.AWK统计某个文件夹下的文件占用的字节数

ls -l |awk 'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ", size}'[end]size is 8657198ls -l |awk 'BEGIN {size=0;} {size=size+$5;} END{print "[end]size is ", size/1024/1024,"M"}' [end]size is 8.25889 M

关于find搜索定位:

11.文件名 搜索文件

locate one.sh

12.查找后执行

find ./ -name 192.168.48.154 -exec cat > 1 {} \;

13.删除空文件

find ./ -type f -size 0 -exec rm -rf {} \;

14.查找大小为0的文件或空目录

find ./ -empty -type f / d

15.查大于512k的文件

find /home -size +512k

16.将多个文件打印到一个文件中

find ./ -name "[1-9].sh" -exec cat > 3 {} \;

17.目录下过滤文件夹

find ./ ! -type f

18.批量修改

find ./ -name "[1-9].sh" -exec sed -i s/0/9/g {} \;

19.-o 相当于且

find ./ -name "1.sh" -o -name "2.sh"

20.两天内改动

find /home -mtime -2

21.两分钟内改动

find /home -mmin -2

22.10分钟内访问的文件

find /home -amin -10

23.2小时内访问的文件

find /home -atime -2

24.将find出来的东西拷到另一个地方

find *.c -exec cp '{}' /tmp ';'

 25.检查/root目录挂载在那个磁盘分区上

df -h /root

26.软连接、硬链接

ln -s /home /root/homeln -b /home/run.sh /root/run.sh

27.解压到指定目录

tar zxvf /filename.tar.zip -C /home

28.vi文书 

gg第一行G最后一行d1G删除光标所在行到第一行dG删除光标所在行到最后一行yy复制光标所在一行A在光标所在最后字符处开始插入ZZ保存退出

29.^M 乱码

yum install dos2unix

30.修改linux登录后信息

vi /etc/motd

31.定时任务脚本

cmd="cd /root/polling/bin/ && /root/polling/bin/run.sh"echo -n "* " >> /var/spool/cron/rootecho -n "6 " >> /var/spool/cron/rootecho -n "* " >> /var/spool/cron/rootecho -n "* " >> /var/spool/cron/rootecho -n "* " >> /var/spool/cron/rootecho $cmd >> /var/spool/cron/root

32.组网信任

{echo -e "\n"} | ssh-keygen -t rsa -P ''cp -r ~/.ssh/id_rsa.pub ~/.ssh/authorized_keyscd ~/.sshscp -r id_rsa.pub $ip:/root/.ssh/$ipssh $ip 'touch ~/.ssh/authorized_keys && chmod 644 ~/.ssh/authorized_keys'ssh $ip "cat ~/.ssh/$ip >> ~/.ssh/authorized_keys"

33.关于shell入参处理

$0 脚本名字 $1 位置参数 #1 $2 - $9 位置参数 #2 - #9 ${10} 位置参数 #10 $# 位置参数的个数 "$*" 所有的位置参数(作为单个字符串) * "$@" 所有的位置参数(每个都作为独立的字符串) ${
#*} 传递到脚本中的命令行参数的个数 ${
#@} 传递到脚本中的命令行参数的个数 $? 返回值 $$ 脚本的进程ID(PID) $- 传递到脚本中的标志(使用set) $_ 之前命令的最后一个参数 $! 运行在后台的最后一个作业的进程ID(PID)

34.使用cut切分

echo 1:2:3 | cut -d ':' -f 2echo 1:2:3 | awk -F ':' {
'print $2'}

 35.egrep扩展

cat 1.sh | grep -v 5 | grep -v 8cat 1.sh | egrep -v '5|8'ls | egrep 'lsit1.sh|tools.sh' #可以同时查找多个egrep "go?d" tools.sh #?一个字符 * 多个字符

36.read命令带描述

read -p "uname:" name

37.test命令

#判断是否存在目录/文件test -e /home && echo ok || echo no[ "a" == "ad" ] && echo yes || echo no#判断参数是否相等test $a -eq $b && echo yes || echo notest $a != $b && echo yes || echo no-eq 相等-ne 不相等test "d" = "d" -o "a" = "d" && echo yes || echo no-o 并-a 且

38.进程树

ps -axjf

 

 

 

注:转载请添加出处

转载于:https://www.cnblogs.com/Javame/p/3867686.html

你可能感兴趣的文章
衣带渐宽终不悔
查看>>
偏函数+高阶函数
查看>>
C# .NET UDP 形式调用 graylog,gelf
查看>>
POST信息模拟登录获取页面内容
查看>>
云端同步的架构实践和协议细节
查看>>
进程和线程、协程的区别
查看>>
Test on 11/09/2016
查看>>
自己定义控件-LinearListView
查看>>
软件杯布匹纺织瑕疵检测系统——需求结构化
查看>>
[BZOJ4552][Tjoi2016&Heoi2016]排序 二分+线段树
查看>>
hotmail收邮件-源码联系我~
查看>>
mac攻略(三) -- apache站点配置
查看>>
java中web service的几种实现方式(自用)
查看>>
第二周
查看>>
软件测试工程师工作总结
查看>>
Oracle12c 64bit install on CentOS7
查看>>
根据url下载图片
查看>>
C# 接口练习
查看>>
JavaScript 字符串处理详解
查看>>
迷茫时期的我
查看>>