Linux流程控制

来源:互联网 发布:ubuntu terminal 主题 编辑:程序博客网 时间:2024/06/03 05:18

一、回顾
软件包的管理:对.rpm软件包的管理:查询,安装,卸载,升级
1.rpm 方式:
rpm -qa | grep [软件名]
rpm -e [软件名]  --nodeps
rpm -ivh [软件名.rpm]

2.yum 方式
yum list | more  //查询yum源上拥有的所有的软件包
yum  list  installed | gerp [软件名]
yum -y install [软件名]
yum -y remove [软件名]

3.通过解压缩方式的安装  .tar.gz
tar -zxf xxx.tar.gz -C  [解压到的目录]
rm -rf  [解压到的目录]编程

4.shell编程
shell脚本的规范
1.第一行:#!/bin/sh开头 或 #!/bin/bash开头
2.注释:#
3.shell脚本以.sh结尾
4.执行shell基本的两种方式:
1)sh hello.sh    #执行shell脚本
2)./hello.sh #这种方式要求这个脚本拥有执行权限

变量,表达式,测试表达式
1)变量
环境变量
用户变量   /home/[username]/.bash_profile
系统变量   /etc/profile

位置变量:$1 ... $9

预定义变量:
$0:执行的脚本的名称
$?:执行成功返回0,失败返回非0
$*:执行脚本输入的全部参数
$$:执行进程的ID号
$#:输入的参数个数

自定义变量:
区分大小写
等号左右不能有空格
调用变量需要使用$符号

2)表达式:--》运算表达式
$(())
$[]
expr

算术运算:+ - \* /
逻辑运算: &&    ||

3)内置测试表达式
第一种 test
if test $1 -gt $2
then
command...
fi

第二种:[ ]
a=6
b=9
if [ $a -lt $b ]
then
echo smaller
fi

数值比较
-eq #equal 等于
-ne #not equal 不等于
-gt #greater than 大于
-ge #greater equal 大于等于
-lt #little than 小于
-le #little equal 小于等于

字符串的比较
= 等于
!= 不等于
-z 长度为0则为true
-n 长度不为0则为true
$string 字符串不为空则为true

文件测试
-d 如果是目录则为true
-f 如果是文件则为true
-r 如果是可读则为true
-w 如果是可写则为true
-x 如果是可执行则为tru

二、流程控制
1.if
第一种语法格式:
if  条件判断
then
command
fi
eg.
#!/bin/sh
if test 3 -eq 3
then
echo Yes
fi

第二种语法格式:
if  条件判断
then
command
esle
command
fi

eg.
#!/bin/sh
if [ -d /home/user01/demo  ]
then
        ls  /home/user01/demo
else
        mkdir  /home/user01/demo
        echo 目录创建成功!!
fi

第三种语法格式:
if 条件判断
then 
command
elif 条件判断
then
command
elif 条件判断
then
command
...
else
command
fi

eg3.
#!/bi/sh
# read socre  and choose level
# read 相当于Java中的scanner 是一种交互式命令,读取设备的输入
read score
if test $score -ge 90
then
        echo level A
elif [ $score -ge  80 ] && [ $score -lt 90 ]
then  echo "level B"
elif [ $score -ge  70 ] && [ $score -lt 80 ]
then  echo "level C"
elif [ $score -ge  60 ] && [ $score -lt 70 ]
then  echo "level D"
else
        echo 不及格
fi

2. 循环语句
1)for 循环

第一种语法格式:
for((初始化变量值;结束循环条件;循环控制语句))
do
循环体
done
eg.
#!/bin/sh
sum=0
for ((i=0;i<10;i++))
do
echo $i
sum=$[ $sum + i ]
done
echo $sum


第二种语法格式:
for 变量 in 值1 值2 ...值N
do
循环体
done

eg.
#!/bin/sh
for MONTH in Jan Feb Mar Apr May Jun July Aug Sep Oct Nov Dec
do
echo $MONTH
done
2)while 循环
第一种语法格式:
while [ condition  #循环条件 ]
do
#statements
#【循环体】
#【循环控制】
done
eg.
#!/bin/sh
i=1
while [ $i -le 10 ]
do
sum=$((sum+i))
i=$[ i + 1 ]
done
echo $sum


第二种语法格式:
while  read -r line
do
#【循环体】
done

eg.
#!/bin/sh
#Read /ect/sysconfig/network-scripts/ifcfg-eh0 and print out
FILE=/ect/sysconfig/network-scripts/ifcfg-eh0
while read -r line
do
echo $line
done < $FILE

3)case  类似于java中的swich case
第一种语法格式:
#!/bin/sh
echo "input from: one two three"
read input
case $input in
one) echo "your input is one"
;;
two) echo "your input is two"
;;
three) echo "your input is three"
;;
*) echo your input is $input
esac
第二种语法格式:
#!/bin/sh
echo "input from :one two three ....."
read input
case $input in
one | two) echo "your input is one or two"
;;
three | four) echo "your input is three or four "
;;
five) echo "your input is five"
;;
*) echo your input is $input
esac


4).时间命令:date
查看当前的系统时间(带时区)
$ date -R


选项:-s  修改
用法:# date -s "2016-12-08 10:43:00"
# date "+%y%m%d%H%M"
# date +%Y-%m-%d

格式化日期:
$ date  '+%Y-%m-%d %H:%M'
2017-05-02 11:20

$ date  '+%Y/%m/%d %H:%M'
2017/05/02 11:20
$ date  '+%Y%m%d %H:%M'
20170502 11:20


回到2天前:
$ date -d '2 day ago' '+%Y%m%d%H%M'
$ date -d '2 day ago' '+%Y/%m/%d/ %H-%M'

三、Crontab计划任务
-->周期性执行计划任务

选项使用:
crontab -l   (list )   #查看目前的计划任务列表
crontab -r   (remove)  #删除计划任务
crontab -e   (eidt)    #编辑周期性计划任务

进程名称是crond
ps -ef | grep crond $查看此进程是否开启
默认进程是开启的,如果没有开启,可以使用命令手动开启
# service crond start
# service crond stop
# service crond restart

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
*    *      *            *             *
分钟 小时  天(of月)  月(of年)  星期(of周)

每天晚上18:00执行一次拷贝/home/uer01/2.log文件到/opt/software的任务
0 18 * * * /bin/cp /home/uer01/2.log /opt/software
15 18 * * * /bin/cp /home/uer01/2.log /opt/software

每天晚上6点到早上6点每15分钟记录一次时间到/home/uer01/2.log文件中
*/15  18-23,0-6 * * * /bin/date  >> /home/user01/2.log
0,15,30,45  18-23,0-6 * * * /bin/date  -R >> /home/user01/2.log

注意:如果是离散的值,使用“,”进行分割
  如果是连续的值,使用“-”进行连接


0 6-12/2  * * 7 /bin/sh /home/user01/datelog.sh

datelog.sh
#!/bin/sh
/bin/echo "当前时间是:" >> /home/user01/2.log
/bin/date -R >> /home/user01/2.log


关闭防火墙和安全子系统
--》在联机应用(分布式)中,一般会关闭防火墙。防火墙默认情况下,出于安全考虑会限制一些应用的网络访问(比如rpc通信端口),为了保证多机通信的稳定,可以选择关闭防火墙
Linux 防火墙
  # service iptables status   ##查看防火墙状态
  iptables: Firewall is not running.
  # service iptables stop     ##关闭防火墙

关闭开机启动防火墙
  #  chkconfig iptables off   ##不随机启动

查看防火墙的开机启动设置
$ sudo chkconfig --list | grep iptables

关闭安全子系统
  # vi /etc/sysconfig/selinux
  SELINUX=disabled