1.单分支的if语句
2.示例
[ ] && ——快捷if
[ -f "/etc/shadow" ] && echo "This computer uses shadow passwors"
1)检查/var/log/messages文件是否存在,若存在则统计文件内容的行数,否则不做任何操作?
[root@stationx ch08_ref]# cat chklog.sh
#!/bin/bash
LogFile="/var/log/messages"
if [ -f $LogFile ] ; then
wc -l $LogFile
fi
2)判断输入的数是否小于15?
[root@stationx ch08_ref]# cat if_exam1.sh
#!/bin/bash
echo "Please input a integer:"
read integer1
if [ $integer1 -lt 15 ]
then
echo "The integer which you input is lower than 15."
fi
3)创建一个文件,然后测试是否创建成功,并测试文件权限?
[root@stationx ch08_ref]# cat if_exam2.sh
#!/bin/bash
touch if_file1
if [ -e if_file1 ]
then
echo "The file if_file1 is created successfully!"
fi
if [ -r if_file1 ]
then
echo "The file can read !"
fi
if [ -w if_file1 ]
then
echo "The file can write !"
fi
if [ -x if_file1 ]
then
echo "The file can execute. "
fi
4)如果/boot分区的空间使用超过80%,输出报警信息?
#!/bin/bash
RATE=`df -hT | grep "/boot" | awk '{print $6}' | cut -d "%" -f1 `
if [ $RATE -gt 80 ]
then
echo "Warning,DISK is full!"
fi
-------------------------------------------------------------------------------------------
2.双分支的if语句
3.示例
1)判断vsftpd是否在运行,若已运行则输出提示信息,否则重启vsftpd服务?
[root@stationx ch08_ref]# cat chkftpsvr.sh
#!/bin/bash
service vsftpd status &> /dev/null
if [ $? -eq 0 ]
then
echo "The ftp service is running."
else
/etc/init.d/vsftpd start
echo "FTP Server has been restart. At time: `date`"
fi
2) 提示用户指定备份目录的路径,若存在,则提示创建该目录,并创建?
[root@stationx ch08_ref]# more mkbak.sh
#!/bin/bash
read -p "What is your backup directory:" BakDir
if [ -d $BakDir ] ; then
echo "$BakDir already exist."
else
echo "$BakDir is not exist, will make it."
mkdir $BakDir
fi
3)统计当前登录到系统中的用户数量,如果超过3个给出警告,否则列出登录的用户信息?
[root@stationx ch08_ref]# more chkuser.sh
#/bin/bash
UsersNum=`who | wc -l`
if [ $UsersNum -gt 3 ] ; then
echo "Alert, too many login users ( Total: $UsersNum ). "
else
echo "Login users:"
who | awk '{print $1,$2}'
fi
4)删除test.txt文件?
[root@stationx ch08_ref]# cat ifelse_example1.sh
#!/bin/bash
echo "Please input the file which you want to delete:"
read file
if rm -f $file
then
echo "Delete the file $file sucessfully !"
else
echo "Delete the file $file failed ! "
fi
#!/bin/sh
echo “Is it morning? Please answer yes or no”
read timeofday
if [ $timeofday = “yes” ]; then
echo “Good morning”
else
echo “Good afternoon”
fi
exit
在进行linux测试时编写脚本是必不可少的,Shell脚本的名称可以随便定义,也不要...几行 tail file : 打印文本文件末尾几行 sed: Sed是一个基本的查找替换程序...
linux Shell(脚本)编程入门 实例讲解详解_IT/计算机_专业资料。shell 基础...程序必须以下面的行开始(必须方在文 件的第一行): #!/bin/sh 符号#!用来...
Linux shell 1.1 shell 变量为使 shell 编程更有效,系统提供了一些 shell ...$0是一个特殊的变量,它的内容是当前这个 shell 程序的文件名即 脚本名。 ...
下面我们开始编写一个 Shell 脚本: 1. 程序必须以下面的行开始(必须放在文件的...执行脚本 Linux 下的 sh 文件默认是有执行权限的。我们可以用命令:ls -l ...
Linux shell脚本入门教程_互联网_IT/计算机_专业资料。Linux shell脚本入门教程...在这个例子中我们使 用/bin/sh 来执行程序。 当编辑好脚本时,如果要执行该...
通常用 Bash,也就是 Bourne Again Shell 进行编程。 二.shell 使用 1.建立和运行 shell 程序 shell 脚本程序:按照一定的语法结构把若干 linux 命令组织在一起,...
Linux shell脚本全面学习_IT/计算机_专业资料。linux shell 学习Linux shell 脚本全面学习 1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以...
Linux shell脚本编写基础_工学_高等教育_教育专区。详细地介绍linux脚本的编写...第 1 部分. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以...
Linux命令及Shell脚本使用_IT/计算机_专业资料。linux命令的应用...在 shell 程序中, 或者是 shell 命令中, 使用操作符$来对 shell 变量进行...
LinuxShell脚本编程实例_IT/计算机_专业资料。一些关于linux的shell脚本编程的... linuxShell脚本 40页 1下载券 项目四LinuxShell程序设... 54页 免费 linux...
热门文档