博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shell之脚本片断
阅读量:6464 次
发布时间:2019-06-23

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

16.

 

以下是平台信息CentOS Linux release 7.1.1503 (Core)Linux mysql-dev1 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux   25  curl -O http://memcached.org/files/memcached-1.4.25.tar.gz   26  curl -O http://mirrors.cnnic.cn/apache/zookeeper/stable/zookeeper-3.4.8.tar.gz   27  curl -O http://www.apache.org/dyn/closer.cgi?path=/activemq/5.9.1/apache-activemq-5.9.1-bin.tar.gz 官网提供的这个链接无效   28  curl -O http://archive.apache.org/dist/activemq/5.9.1/apache-activemq-5.9.1-bin.tar.gz   31  curl -O https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz1.免密码登录,就这两步ssh-keygen -t rsa -P ''scp .ssh/id_rsa.pub 192.168.1.242:/root/.ssh/authorized_keys[root@manage ~]# cat manager.shwhile echo "please input host which you want connect: "do select host in 240 241 242 238-db 247-db 248-db   do   break   done        case $host in         "240") ssh 192.168.1.240;;         "241") ssh 192.168.1.241;;         "242") ssh 192.168.1.242;;      "238-db") ssh 192.168.1.238;;      "247-db") ssh 192.168.1.247;;      "248-db") ssh 192.168.1.248;;           *)  break;;        esacdone2.安装jdk与tomcatmkdir packagescd packages/curl -O ftp://192.168.1.100/03%BF%AA%B7%A2%B9%A4%BE%DF/Java/jdk/jdk-7u79-linux-x64.rpmcurl -O ftp://192.168.1.100/03%BF%AA%B7%A2%B9%A4%BE%DF/Tomcat/apache-tomcat-8.0.30.tar.gzrpm -ivh jdk-7u79-linux-x64.rpmcat <
/etc/profile.d/java.shJAVA_HOME=/usr/java/jdk1.7.0_79CLASSPATH=$CLASSPATH:$JAVA_HOME/libPATH=$JAVA_HOME/bin:$PATHexport JAVA_HOME PATH CLASSPATHEOFsource /etc/profiletar zxvf apache-tomcat-8.0.30.tar.gz./apache-tomcat-8.0.30/bin/startup.sh3.安装mysql#hostnamectl set-hostname mysql-test-cn#systemctl stop firewalld#cd /root/packages/mysql#yum -y install perl net-tools nfs-utils#systemctl start rpcbind#rpm -e --nodeps mariadb-libs#mount 192.168.1.250:/nfs/html /mnt#cd /mnt#rpm -ivh mysql-commercial-client-5.7.10-1.1.el7.x86_64.rpm mysql-commercial-common-5.7.10-1.1.el7.x86_64.rpm mysql-commercial-libs-5.7.10-1.1.el7.x86_64.rpm mysql-commercial-server-5.7.10-1.1.el7.x86_64.rpm#systemctl start mysqldpass=$(grep "temporary password" /var/log/mysqld.log |awk -F ': ' '{print $2}')mysql --connect-expired-password -p"$pass" <

 

 

 

 

15.

 

[Tue Jan 19 11:23:47 1101 /dev/pts/0 192.168.2.250 ~/sh]#bash legs please enter name of an animal: ii the ii has an unknown number of legs. [Tue Jan 19 11:24:28 1101 /dev/pts/0 192.168.2.250 ~/sh]#bash legs please enter name of an animal: dog the dog has four legs. [Tue Jan 19 11:25:20 1102 /dev/pts/0 192.168.2.250 ~/sh]#cat legs echo -n "please enter name of an animal: "read animalecho -n "the $animal has "case $animal in  horse|dog|cat)echo -n "four";;  man|kangaroo)echo -n "two";;  *)echo -n "an unknown number of";;esacecho " legs."

 

 

 

14.删除程序产生的日志,只保留一个当天日期的文件,并且在每天20点时清空此文件,感觉有点重复,要么只保留一个文件,要么清空都可以达到减少占用磁盘的目的。

[Thu Mar 26 10:01:40 1071 /dev/pts/1 192.168.2.109 /usr/local/ps/log]#cat /var/prtg/scripts/pslog#find /usr/local/ps/log  -ctime +0|xargs rm -rf#>/usr/local/ps/log/debug*a=debug`date +%Y%m%d`.logfor i in `ls *.log`doif [ "$i" != "$a" ] then rm -rf $i else >$ifidone

 

13.

黑白棋盘 其实是个双循环, 外层执行第一层循环时 内层共执行8次,并判断是单或双, 是单的话,total是双,tmp余数是0,输出背景为灰的两个空格 是双的话,total是单,tmp余数是1,输出背景为黑的两个空格 外层第一层最后一个echo ""是换行 然后执行第二层外循环 [Wed Mar 25|15:40:23 ~ 1081 /bin/bash ]# cat black#!/bin/bashfor((i=1;i<=8;i++)) do    for((j=1;j<=8;j++))    do       total=$(($i+$j))       tmp=$(($total%2))       if [ $tmp -eq 0 ]       then           echo -e -n "\033[47m  \033[0m"       else           echo -e -n "\033[40m  \033[0m"       fi    done    echo "" done [Wed Mar 25|15:40:46 ~ 1082 /bin/bash ]# cat .bashrc# .bashrc# User specific aliases and functionsalias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then        . /etc/bashrcfi#PS1='[\u@\h \W]\$ 'export PS1='[\d|\t \w \! $SHELL ]\$ '

 

12.

因为有好多的safe进程,所以把每一个列出来并杀死[root@aster3 ~]# cat aa=`ps -ef|grep safe|grep -v grep|awk '{print $2}'`for i in $a do kill -9 $i done清空每一个日志文件[root@84-monitor monitorlog]# cat afor i in `ls *.log`do#echo $i> $idone

 

11.

日常工作中,处理数据难免会遇到遍历,for循环可能是我们用的比较多的了。本节就来探讨下for语句嵌套循环的性能,猜想下面两个语句的性能。外层循环越少效率越高[root@250-shiyan sh]# cat inlineforfor (( i=1; i<100; i++ ))do for (( c=1; c<1000; c++ ))  do  echo "$c-----inline"  doneecho "$i-----outline"done[root@250-shiyan sh]# cat outlineforfor (( i=1; i<1000; i++ ))do for (( c=1; c<100; c++ ))  do  echo "$c-----inline"  doneecho "$i-----outline"done[root@250-shiyan sh]# time (bash inlinefor) |tail -3998-----inline999-----inline99-----outlinereal    0m1.754suser    0m1.433ssys     0m0.319s[root@250-shiyan sh]# time (bash outlinefor) |tail -398-----inline99-----inline999-----outlinereal    0m1.731suser    0m1.392ssys     0m0.338s[root@250-shiyan sh]# time (bash inlinefor) |tail -39998-----inline9999-----inline999-----outlinereal    3m8.818suser    2m25.615ssys     0m43.021s[root@250-shiyan sh]# time (bash outlinefor) |tail -3998-----inline999-----inline9999-----outlinereal    3m5.233suser    2m21.791ssys     0m43.278s

 

10.脚本执行方式及子进程关系

###4种方式:全路径/path/to/conns|bash conns|source conns|. conns ###要有执行权限产生子shell [root@250-shiyan prog]# cat conns#!/bin/baship_conns=`ssh $1 "netstat -ant| grep EST | wc -l"`echo $ip_conns[root@250-shiyan prog]# chmod u+x conns[root@250-shiyan prog]# cp conns /usr/bin[root@250-shiyan prog]# conns 192.168.2.109 [root@250-shiyan prog]# ./conns root@192.168.2.109's password: 851 ###无需执行权限,产生子shell [root@250-shiyan prog]# bash conns root@192.168.2.109's password: 855 [root@84-monitor ~]# cat a a=192.168.2.109 ip_conns=`ssh $a "netstat -ant| grep EST | wc -l"` echo $ip_conns ###source命令与点命令是在当前shell中执行,并不产生子shell,也不需要脚本有执行权限。 [root@84-monitor ~]# source a 860 [root@84-monitor ~]# . a 860

 

9.

shell 判断字符串中是否含有指定字符new=sddps=secho "$new" |grep -q "$ps"if [ $? -eq 0 ]thenecho "yes"elseecho "no"fi 判断某年某天是星期几?year=1980end_year=2010day_2=10/22week_2=Fri while [ $year -lt $end_year ]donew=`date -d "$day_2 CST $year"`echo "$new"echo "$new" |grep -q "$week_2"if [ $? -eq 0 ]then            #  year=`expr $year + 1`echo "this year $year include $week_2"    year=`expr $year + 1`else    #echo "this year $year include $week_2"    year=`expr $year + 1`fi  done [root@250-shiyan prog]# expr length "$HOME" 5 [root@250-shiyan prog]# echo "$HOME"|wc -c 6 [root@250-shiyan prog]# echo “$HOME”|awk '{print length($0)}' 7 判断字符串为空的方法有三种:if [ "$str" =  "" ]if [ x"$str" = x ]if [ -z "$str" ] (-n 为非空)注意:都要用双引号,否则有些命令会报错,养成好习惯吧!

 

8.

[root@250-shiyan frag]# bash here.shUSAGE: xtitlebar [-h] "string_for_titelbar"OPTIONS: -h help textEXAMPLE: xtitlebar "cvs"[root@250-shiyan frag]# cat here.shhelp(){cat <

 

7.还有点问题,2015/5/25的时候才搞明白下面的问题所在。才理解了下面的这段脚本含义。

[root@250-shiyan frag]# bash while2.shenter y/n :y===============================================| unix script test || 1 --- num 1 || 2 --- num 2 || 3 --- num 3 || 4 --- num 4 |===============================================enter y/n :nPress 
to proceed or type q to quit:efPress
to proceed or type q to quit:efPress
to proceed or type q to quit:q[root@250-shiyan frag]# cat while2.sh#!/bin/bashbanner(){cat <
to proceed or type q to quit:"doread cmdcase $cmd in####break跳出while语句[qQ]) exit 1;;####引号中间没有任何字符,表示是return"") break;;####continue继续下一循环*) continue;;esacdone}####pause or getyn to testwhile getyn    dobannerdonepause

 

6.

[root@250-shiyan frag]# bash while.shinput num:4input is 4new num is 5new num is 6new num is 7new num is 8new num is 9new num is 10[root@250-shiyan frag]# cat while.sh#!/bin/bashecho "input num:"read numecho "input is $num"while test $num -lt 10donum=$(($num+1))echo "new num is $num"sleep 2done

 

5.

[root@localhost script]# cat >if-1#!/bin/bashecho -e "are you ok(y/n or maybe)? \c"read answerif [[ $answer == [yY]* || $answer = maybe ]]then echo "glad to hear it"fi[root@localhost script]# bash if-1are you ok(y/n or maybe)? dk[root@localhost script]# bash if-1are you ok(y/n or maybe)? yglad to hear it[root@localhost script]# bash if-1are you ok(y/n or maybe)? Yglad to hear it[root@localhost script]# bash if-1are you ok(y/n or maybe)? maybeglad to hear it

 

4.||表示只要其中之一个,满足即可,&&表示两个,以至n个条件都要满足才可以。

####也可以一试 if [[ "$s" -gt "0" ||  "$r" -gt "0" ]] [root@localhost script]# cat >if-2#!/bin/bashecho -e "how old are you? \c"read ageif (( age < 0 || age > 120 ))then echo "you are so old"####两个同时满足,即上限与下限elif (( age >=0 && age <= 12 ))then echo "you are child"elif (( age >=13 && age <=19 ))then echo "you are 13-19 years old"elif (( age >=20 && age <=29 ))then echo "you are 20-29 years old"elif (( age >=30 && age <=39 ))then echo "you are 30-39 years old"else echo "you are above 40"fi[root@localhost script]# bash if-2how old are you? 30you are 30-39 years old[root@localhost script]# bash if-2how old are you? 28you are 20-29 years old[root@localhost script]# bash if-2how old are you? 400you are so old[root@localhost script]# bash if-2how old are you? 0you are child

 

3.检查根分区,循环做两件事,a.输出当前值,b.如果大于10%,则输出信息

[root@250-shiyan sh]# cat check-root.sh#!/bin/bashwhile sleep 5  do    for i in `df -h |sed -n '/\/$/p'|awk '{print $5}'|sed 's/\%//g'`         do echo $i         if [ $i -ge 10 ]         then         echo " more than 10$ linux of disk space."         fi         done  done[root@250-shiyan sh]# bash check-root.sh11 more than 10$ linux of disk space.11 more than 10$ linux of disk space.

 

2.自编有while循环,有函数,有if,还有脚本参数,执行时  ./while w1或./while w2

[root@250-shiyan sh]# cat while#!/bin/bashw1 () {min=1max=100while [ $min -le $max ]doecho $minmin=`expr $min + 1`done}w2 () {i=1while(($i<100))doif(($i%4==0))thenecho $ifii=$(($i+1))done}if [ $@ = w1 ]thenw1elsew2fi

 

1.自编让其以后台进程形式存在,不用crontab去定期执行

[root@250-shiyan sh]# cat eth#!/bin/bashwhile [ 1 -gt 0 ]doeth=`ifconfig eth0|grep "TX bytes"|gawk '{print $6}'|cut -d ":" -f2`echo $eth >> /root/sh/jjsleep 2done[root@250-shiyan sh]# bash eth &[root@250-shiyan sh]# tail -f jj

 

转载地址:http://oohzo.baihongyu.com/

你可能感兴趣的文章
jQuery.extend 函数详解
查看>>
[转]Windows的批处理脚本
查看>>
lnmp高人笔记
查看>>
[转载] OpenCV2.4.3 CheatSheet学习(三)
查看>>
C#中跨窗体操作(2)--消息机制
查看>>
子程序框架
查看>>
多维数组元素的地址
查看>>
数据库运维体系_SZMSD
查看>>
aspose 模板输出
查看>>
福大软工1816 · 第三次作业 - 结对项目1
查看>>
selenium多个窗口切换
查看>>
《单页面应用》所获知识点
查看>>
静态库 调试版本 和发布版本
查看>>
读书笔记四
查看>>
JAVA中的finalize()方法
查看>>
慕课网学习手记--炫丽的倒计时效果Canvas绘图与动画基础
查看>>
==与equals()的区别
查看>>
基本分类方法——KNN(K近邻)算法
查看>>
在XenCenter6.2中构建CentOS7虚拟机的启动错误
查看>>
.NET Framework3.0/3.5/4.0/4.5新增功能摘要
查看>>