shell之脚本片断

时间:2023-03-08 17:22:35

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.gz 1.免密码登录,就这两步
ssh-keygen -t rsa -P ''
scp .ssh/id_rsa.pub 192.168.1.242:/root/.ssh/authorized_keys [root@manage ~]# cat manager.sh
while 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;;
esac
done 2.安装jdk与tomcat
mkdir packages
cd packages/
curl -O ftp://192.168.1.100/03%BF%AA%B7%A2%B9%A4%BE%DF/Java/jdk/jdk-7u79-linux-x64.rpm
curl -O ftp://192.168.1.100/03%BF%AA%B7%A2%B9%A4%BE%DF/Tomcat/apache-tomcat-8.0.30.tar.gz rpm -ivh jdk-7u79-linux-x64.rpm
cat <<EOF > /etc/profile.d/java.sh
JAVA_HOME=/usr/java/jdk1.7.0_79
CLASSPATH=$CLASSPATH:$JAVA_HOME/lib
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH CLASSPATH
EOF
source /etc/profile tar zxvf apache-tomcat-8.0.30.tar.gz
./apache-tomcat-8.0.30/bin/startup.sh 3.安装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 mysqld
pass=$(grep "temporary password" /var/log/mysqld.log |awk -F ': ' '{print $2}')
mysql --connect-expired-password -p"$pass" <<EOF
SET PASSWORD FOR 'root'@'localhost'= "123456";
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
EOF
#cd
#umount /mnt
echo $PWD 4.安装memcached memcached 1.4.15
libevent 2.0.21-stable yum install memcached
vi /etc/sysconfig/memcached
grep mem /etc/passwd
systemctl status memcached
systemctl start memcached
systemctl enable memcached
systemctl status memcached
memcached-tool 127.0.0.1:11211 stats 5.安装zookeeper
tar zxvf zookeeper-3.4.8.tar.gz
cd zookeeper-3.4.8
cp conf/zoo_sample.cfg conf/zoo.cfg
mkdir /home/zookeeper
vi conf/zoo.cfg
./bin/zkServer.sh start/stop
ss -lnp|grep zoo
ss -lnp|grep 2181
./bin/zkCli.sh -server 127.0.0.1:2181 6.安装activemq
tar zxvf apache-activemq-5.9.1-bin.tar.gz
cd packages/apache-activemq-5.9.1
./bin/activemq start
ss -lnp|grep 8161
ss -lnp|grep 11211
ss -lnp|grep java

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 animal
echo -n "the $animal has "
case $animal in
horse|dog|cat)echo -n "four";;
man|kangaroo)echo -n "two";;
*)echo -n "an unknown number of";;
esac
echo " legs."

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

[Thu Mar  ::  /dev/pts/ 192.168.2.109 /usr/local/ps/log]#cat /var/prtg/scripts/pslog
#find /usr/local/ps/log -ctime +|xargs rm -rf
#>/usr/local/ps/log/debug*
a=debug`date +%Y%m%d`.log
for i in `ls *.log`
do
if [ "$i" != "$a" ]
then
rm -rf $i
else
>$i
fi
done

13.

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

12.

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

11.

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

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

###4种方式:全路径/path/to/conns|bash conns|source conns|. conns
###要有执行权限产生子shell
[root@-shiyan prog]# cat conns
#!/bin/bash
ip_conns=`ssh $ "netstat -ant| grep EST | wc -l"`
echo $ip_conns
[root@-shiyan prog]# chmod u+x conns
[root@-shiyan prog]# cp conns /usr/bin
[root@-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=sdd
ps=s
echo "$new" |grep -q "$ps"
if [ $? -eq ]
then
echo "yes"
else
echo "no"
fi 判断某年某天是星期几?
year=
end_year=
day_2=/
week_2=Fri while [ $year -lt $end_year ]
do
new=`date -d "$day_2 CST $year"`
echo "$new"
echo "$new" |grep -q "$week_2"
if [ $? -eq ]
then
# year=`expr $year + `
echo "this year $year include $week_2"
year=`expr $year + `
else
#echo "this year $year include $week_2"
year=`expr $year + `
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@-shiyan frag]# bash here.sh
USAGE: xtitlebar [-h] "string_for_titelbar"
OPTIONS: -h help text
EXAMPLE: xtitlebar "cvs"
[root@-shiyan frag]# cat here.sh
help()
{
cat <<HELP
USAGE: xtitlebar [-h] "string_for_titelbar"
OPTIONS: -h help text
EXAMPLE: xtitlebar "cvs"
HELP
exit
}
help

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

[root@-shiyan frag]# bash while2.sh
enter y/n :
y
===============================================
| unix script test |
| --- num |
| --- num |
| --- num |
| --- num |
===============================================
enter y/n :
n
Press <return> to proceed or type q to quit:
ef
Press <return> to proceed or type q to quit:
ef
Press <return> to proceed or type q to quit:
q
[root@-shiyan frag]# cat while2.sh
#!/bin/bash
banner()
{
cat <<echo1
===============================================
| unix script test |
| --- num |
| --- num |
| --- num |
| --- num |
===============================================
echo1
} getyn()
{
while echo "enter y/n :"
do
read yn
case $yn in
[Yy]) return ;;
yes) return ;;
YES) return ;;
[Nn]) return ;;
no) return ;;
NO) return ;;
* ) echo "only accept Y,y,N,n,YES,yes,NO,no";;
esac
done
} pause()
{
while echo "Press <return> to proceed or type q to quit:"
do
read cmd
case $cmd in
####break跳出while语句
[qQ]) exit ;;
####引号中间没有任何字符,表示是return
"") break;;
####continue继续下一循环
*) continue;;
esac
done
} ####pause or getyn to test
while getyn    
do
banner
done
pause

6.

[root@-shiyan frag]# bash while.sh
input num: input is
new num is
new num is
new num is
new num is
new num is
new num is
[root@-shiyan frag]# cat while.sh
#!/bin/bash
echo "input num:"
read num
echo "input is $num"
while test $num -lt
do
num=$(($num+))
echo "new num is $num"
sleep
done

5.

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

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

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

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

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

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

[root@-shiyan sh]# cat while
#!/bin/bash
w1 () {
min=
max=
while [ $min -le $max ]
do
echo $min
min=`expr $min + `
done
} w2 () {
i=
while(($i<))
do
if(($i%==))
then
echo $i
fi
i=$(($i+))
done
} if [ $@ = w1 ]
then
w1
else
w2
fi

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

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