Linux Shell 小本子经典收藏

## ====================================================##
## auth:wonter                                                                                       ##
## date:2014-06-12                                                                                 ##
## to  :trust.sh                                                                                       ##
## ====================================================##
#!/bin/sh
trust(){
{
echo -e "\n"
} | ssh-keygen -t rsa -P ''

#参数
echo "请输入信任机ip:(提示:需输入三次密码!)"
read ip

#建立信任关系
cd ~/.ssh

scp -r id_rsa.pub $ip:/root/.ssh/$ip

ssh $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访问

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" ]
then
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" 
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 

 

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.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Sun21    4days  0.00s  0.00s -bash
root     pts/0    192.168.1.2    09:11    0.00s  0.07s  0.00s w
root     pts/2    192.168.1.2    09:45   30:53   0.07s  0.07s -bash
root     pts/3    192.168.1.8    10:20   22:05   0.02s  0.00s vi operation.log
[root@tradx24 logs]# skill -KILL -v pts/3
pts/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.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                21Mar14  4days  0.09s  0.09s -bash

3、通过ssh远程批量实施MySQL脚本

#ip
list1=(`cat ../tools/dblist | grep -v \# | awk '{print $1}' | sed "1,1d"`)
#user
list2=(`cat ../tools/dblist | grep -v \# | awk '{print $2}' | sed "1,1d"`)
#password
list3=(`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 rsa
cd ~/.ssh
scp -r id_rsa.pub $ip:/root/.ssh/$ip
ssh $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
        fi
done
}

10、引用其他shell脚本方法

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

11、获取脚本自身名称

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

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

bak_mk="../bak/$serve"
new_mk="../new/$serve"
if [ ! -d $bak_mk ]; then
mkdir $bak_mk
fi
if [ ! -d $new_mk ]; then
mkdir $new_mk
fi
判断文件是否为空
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定义集合

#name
list1=(`cat ../tools/list | grep -v \# | awk '{print $1}' | sed "1,1d"`)
#ip
list2=(`cat ../tools/list | grep -v \# | awk '{print $2}' | sed "1,1d"`)
#serve
list3=(`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]}.yw
done

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

#Perform operation
echo "operator:"
read me
a=`tty | awk -F "/dev/" '{print $2}'`
date "+%Y-%m-%d %H:%M:%S" >> ../logs/operation.log
echo `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/sh
echo "请输入定时阀值(格式:`date +"%Y-%m-%d %H:%M"` )"
        read timing
echo "定时任务设置完毕,请安心等待..."
ti(){
while true
do
        time=`date +"%Y-%m-%d %H:%M"`

        if [[ "${time}" == "${timing}" ]]
        then
                echo "定时任务:"
                #定时清理日志
                rm -rf ../logs/*
                #定时备份日志
                #定时执行脚本
                break
        fi
done
}
ti&

18、通过参数方式实施脚本 tools.sh

#!/bin/sh
trust(){
#建立信任
echo "请输入信任机ip:(提示:需输入三次密码!)"
read ip
trusts
}
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 spk
echo $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 -l
ps 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 <PID>
手册中说: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++));
do
sh ./${list1[i]}.wy
done

7.判断是否有

if [ ! -d $bak_mk ]; then
mkdir $bak_mk
fi

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 8657198
ls -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/home
ln -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/root
echo -n "6 " >> /var/spool/cron/root
echo -n "* " >> /var/spool/cron/root
echo -n "* " >> /var/spool/cron/root
echo -n "* " >> /var/spool/cron/root
echo $cmd >> /var/spool/cron/root

32.组网信任

{
echo -e "\n"
} | ssh-keygen -t rsa -P ''
cp -r ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
cd ~/.ssh
scp -r id_rsa.pub $ip:/root/.ssh/$ip
ssh $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 2
echo 1:2:3 | awk -F ':' {'print $2'}

 35.egrep扩展

cat 1.sh | grep -v 5 | grep -v 8
cat 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 no
test $a != $b && echo yes || echo no
-eq 相等
-ne 不相等
test "d" = "d" -o "a" = "d" && echo yes || echo no
-o 并
-a 且

38.进程树

ps -axjf

 

 

 

注:转载请添加出处
网站地图xml地图