SHELL学习笔记3
来源:互联网 发布:大数据都要学什么 编辑:程序博客网 时间:2024/04/29 22:36
1.if条件句
单分支结构
语法:
if [条件]
then
指令
fi
或者:
if [条件]; then #分号相当于命令换行。
指令
fi
特殊写法:if [ -f "$file1" ]; then echo 1 ; fi 相当于 [ -f "$file1" ] && echo 1
简单判断两个数字的大小
#!/bin/bash
read -p "Pls input two number:" a b
if [ $a -gt $b ]
then
echo "$a > $b"
else
echo "$a < $b"
fi
另外还有一个例子:
#!/bin/bash
a="/uestc/file1"
if [ -f "$a" ]
then
echo "It exits"
cat "$a"
else
echo "There is no such file"
touch $a
fi
另一个例子:
#!/bin/bash
$FILEPATH="/tmp/uestc"
[ ! -d "$FILEPATH" ] && mkdir -p $FILEPATH
cd $PATH
if [ -f "$FILEPATH/file.sh" ]
then
echo "file.sh is exist"
cat "$FILEPATH/file.sh"
exit 0
else
touch "$FILEPATH/file.sh"
echo "$FILEPATH/file.sh has been created"
fi
思考题目:
判断系统内存大小,低于100MB就进行邮件报警
linux系统中查看系统内存的命令为:free -m 而Linux系统有个特性,只要是不用的内存会被缓存(caching),所以说真正可用的内存为buffers/cache中的free项。
我们这里解释复习一下awk的用法
awk指定分隔符号的选项为-F ":" ;
awk -F: '/root/ {print $7}' #这行命令为指定分隔符为:,截取含有root关键字的某一行,然后打印出这一行的第七个字符。
我们先截取出剩余内存数量:free -m | awk '/buffers\// {print $4}'
#!/bin/bash
cur_free=`free -m | awk '/buffers\// {print $4}'`
if[[ $cru_free -lt 4000 ]]
then
echo "Low Memory!! CURRENT MEMMORY=$cur_free"
echo "Low MEM" | mail -s "Low MEM current free mem is $cur_free" 639188185@qq.com
fi
如我我们想每分钟检查一次该如何做呢?
crontab -e
* * * * * /bin/sh /uestc/mem1.sh > /dev/null #一分钟检查一次
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令 放大法
2.双分支和多分支if语句
read -p "Plase input a integer: " a
if [ $a -gt 6 ]
then
echo "$a is larger than 6"
elif [ $a -lt 6 ]
then
echo "$a is litter than 6"
else
echo "$a equals 6"
fi
A="123k"
[ -n "$A" -a "$A" = "${A/[^0-9]/ /}" ] && echo "Integer" || echo "No"
3.监控MySQL服务的实战例子
PortNum=`netstat -lnt | grep 3306 | wc -l`
Process=`ps aux | grep mysqld | grep -v grep | wc -l`
LogPath="/tmp/mysql.log"
if [ $PortNum -eq 1 -a $Process -eq 2 ]; then
echo "MySql is running Now!"
else
service mysqld start>$LogPath
sleep 10
PortNum=`netstat -lnt | grep 3306 | wc -l`
MysqlProcessNum=`ps aux | grep mysqld | grep -v grep | wc -l`
if [ $PortNum -ne 1 -a $MysqlProcessNum -ne 2 ]; then
while true #while死循环确保mysqld进程被彻底杀死
do
killall -9 mysqld &>/dev/null
[ $? -ne 0 ] && break #$?不等于0说明没有进程可杀,代表进程已经被杀死了
sleep 1
done
service mysqld start>>$LogPath && status="Successful" || status="Failure"
mail -s "MySql startup status is $status" 639188185@qq.com <$LogPath
service mysqld start
fi
fi
4.MySQL的实例2
5.如何查看远端web服务是否开通tcp 80端口
7.case结构条件句
read -p "Pls input a integer: " ans
case "$ans" in
1)
echo "you have input 1"
;;
2)
echo "you have input 2"
;;
[3-9])
echo "you have input $ans"
;;
*)
echo "Pls input less than 9"
exit;
i;;
esac
8.启动apache的脚本
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible \
# server implementing the current HTTP standards.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd/httpd.pid
#
### BEGIN INIT INFO
# Provides: httpd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Should-Start: distcache
# Short-Description: start and stop Apache HTTP Server
# Description: The Apache HTTP Server is an extensible server
# implementing the current HTTP standards.
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/sbin/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure. So we just do it the way init scripts
# are expected to behave here.
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=6
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
RETVAL=2
esac
exit $RETVAL
9.while循环和until循环
for num in 5 4 3 2 1
do
echo $num
done
10
7
/sbin/nologin
- shell 学习笔记3
- SHELL学习笔记3
- linux shell 学习笔记 3
- SHELL学习笔记(3)
- shell script学习笔记3
- linux Shell学习笔记3
- shell脚本语言学习笔记-3
- Linux学习笔记-shell(3)
- Shell学习笔记(3)
- [Bash Shell] Shell学习笔记
- [Bash Shell] Shell学习笔记
- shell shell学习笔记001--什么是shell?
- Shell学习笔记(3)-一个复杂一点的Shell程序
- Linux Shell学习笔记3:更多的bash shell命令
- Linux Shell 《abs_guide》 学习笔记(3)
- linux shell学习笔记3 (引用、引号)
- Linux shell 脚本攻略 学习笔记-3
- Linux Shell编程学习笔记(3)
- 格林威尔北向接口在电信中的应用
- shell编程和python有什么不同
- 用MapReduce实现矩阵乘法
- Java.lang.UnsatisfiedLinkError
- django模板放置的位置
- SHELL学习笔记3
- C# Java间进行RSA加密解密交互(二)
- 有关socket AF_UNIX的一点心得
- 微信企业号开发之获取AccessToken
- Activity切换效果(overridePendingTransition)
- 程序设计基本概念——运算符
- Spring3.0的新特性及其分析
- ejabberd tls cert file
- C# Java间进行RSA加密解密交互(三)