文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > linux shell脚本程序
侵权投诉

linux shell脚本程序

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

第1页

热门文档

相关文档

  • linuxshell脚本新手入门

    在进行linux测试时编写脚本是必不可少的,Shell脚本的名称可以随便定义,也不要...几行 tail file : 打印文本文件末尾几行 sed: Sed是一个基本的查找替换程序...

  • linux Shell(脚本)编程入门 实例讲解详解

    linux Shell(脚本)编程入门 实例讲解详解_IT/计算机_专业资料。shell 基础...程序必须以下面的行开始(必须方在文 件的第一行): #!/bin/sh 符号#!用来...

  • Linux shell 脚本编程技巧

    Linux shell 1.1 shell 变量为使 shell 编程更有效,系统提供了一些 shell ...$0是一个特殊的变量,它的内容是当前这个 shell 程序的文件名即 脚本名。 ...

  • Linux下Shell脚本编程简介

    下面我们开始编写一个 Shell 脚本: 1. 程序必须以下面的行开始(必须放在文件的...执行脚本 Linux 下的 sh 文件默认是有执行权限的。我们可以用命令:ls -l ...

  • Linux shell脚本入门教程

    Linux shell脚本入门教程_互联网_IT/计算机_专业资料。Linux shell脚本入门教程...在这个例子中我们使 用/bin/sh 来执行程序。 当编辑好脚本时,如果要执行该...

  • linux+shell脚本语言基础知识

    通常用 Bash,也就是 Bourne Again Shell 进行编程。 二.shell 使用 1.建立和运行 shell 程序 shell 脚本程序:按照一定的语法结构把若干 linux 命令组织在一起,...

  • Linux shell脚本全面学习

    Linux shell脚本全面学习_IT/计算机_专业资料。linux shell 学习Linux shell 脚本全面学习 1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以...

  • Linux shell脚本编写基础

    Linux shell脚本编写基础_工学_高等教育_教育专区。详细地介绍linux脚本的编写...第 1 部分. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以...

  • Linux命令及Shell脚本使用

    Linux命令及Shell脚本使用_IT/计算机_专业资料。linux命令的应用...在 shell 程序中, 或者是 shell 命令中, 使用操作符$来对 shell 变量进行...

  • LinuxShell脚本编程实例

    LinuxShell脚本编程实例_IT/计算机_专业资料。一些关于linux的shell脚本编程的... linuxShell脚本 40页 1下载券 项目四LinuxShell程序设... 54页 免费 linux...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email