shell 简单例子(五)
来源:互联网 发布:foxtable建立数据库 编辑:程序博客网 时间:2024/05/16 11:55
shell 简单例子
①编写一个名为iffile程序,它执行时判断/bin目录下date文件是否存在?
#!/bin/shfileName=/bin/date
if [ -f "$fileName" ]
then
echo "exists"
exit 0
fi
注重解释:
-f 存在且是普通文件
-d 存在且是目录
-s 存在且字节数大于0
-r 存在且可读
-w 存在且可写
-x 存在且可执行
检测文件目录的
①编写一个名为greet的问候程序,它执行时能根据系统当前的时间向用户输出问候信息。设从半夜到中午为早晨,中午到下午六点为下午,下午六点到半夜为晚上。
#!/bin/sh
nowHour=$(date +%Y%m%d%H | cut -c 9-10) #截取9 8 两位 就是截取小时
firstChar=${nowHour:0:1} #这个也是字符串截取 截取第一位 判断是否以0开头
if [ "$firstChar" -eq "0" ]
then
nowHour=$(date +%Y%m%d%H | cut -c 9-10) #截取9 8 两位 就是截取小时
firstChar=${nowHour:0:1} #这个也是字符串截取 截取第一位 判断是否以0开头
if [ "$firstChar" -eq "0" ]
then
nowHour=${nowHour:1:2}#如果以0开头 就拿取第二位就可以了
echo "time first char is zero now time is $nowHour"
fi
if [ "$nowHour" -ge 0 -a "$nowHour" -le 11 ]
then
echo "good morning"
elif [ "$nowHour" -gt 11 -a "$nowHour" -le 12 ]
then
echo "good afternoon"
else
echo "Good evening"
fi
echo "time first char is zero now time is $nowHour"
fi
if [ "$nowHour" -ge 0 -a "$nowHour" -le 11 ]
then
echo "good morning"
elif [ "$nowHour" -gt 11 -a "$nowHour" -le 12 ]
then
echo "good afternoon"
else
echo "Good evening"
fi
注重解释:
-a 逻辑与
-o 逻辑或
! 逻辑非
其它解释:
-lt 小于
-le 小于等于
-gt 大于
-ge 大于等于
-eq 等于
-ne 不等于
shell 常用判断
①编写一个名为ifuser的程序,它执行时带用户名作为命令行参数,判断该用户是否已经在系统中登录,并给出相关信息。
#!/bin/sh
if test $# -ne 1
then
echo "Incorrect number of arguments"
echo "Usage: ifuser username"
else
user=$1
if who | grep $user
then
echo $1 "user is logged on."
exit 0
else
echo $1 "user is not logged on."
exit 1
fi
fi
说明:
(1)由于who | grep $user不是表达式而是命令组合,所以不需要test语句测试;如果命令组合执行成功,即找到指定的用户名则返回码为0;否则为非0。
(2)在这个程序中使用了grep字符串搜索命令和“|”管道命令
(3)用grep 命令–q选项来禁止显示搜索到的信息。
(4)还使用了exit命令,以终止shell程序的执行。exit 0表示程序终止后返回0值,而exit1表示程序终止后返回非0的值。
(1)由于who | grep $user不是表达式而是命令组合,所以不需要test语句测试;如果命令组合执行成功,即找到指定的用户名则返回码为0;否则为非0。
(2)在这个程序中使用了grep字符串搜索命令和“|”管道命令
(3)用grep 命令–q选项来禁止显示搜索到的信息。
(4)还使用了exit命令,以终止shell程序的执行。exit 0表示程序终止后返回0值,而exit1表示程序终止后返回非0的值。
将所有.txt的文件改为后缀为.doc的文件
快速创建几个txt方式:
[root@hadoop06 shtest]# vi createTxt.txt
1.txt
2.txt
3.txt
4.txt
2.txt
3.txt
4.txt
---执行创建
[root@hadoop06 shtest]# vi runCreateTxt.sh
#!/bin/sh
for i in $(cat createTxt.txt)
do
$(touch $i)
done
for i in $(cat createTxt.txt)
do
$(touch $i)
done
这是在看你的那个主目录 就会生成一些对应文件了
更改后缀为.doc
[root@hadoop06 shtest]# vi runCreateTxt.sh
#!/bin/sh
for i in $(cat createTxt.txt)
do
mv $i ${i/txt/doc}
done
~
运行之后查看结果
for i in $(cat createTxt.txt)
do
mv $i ${i/txt/doc}
done
~
运行之后查看结果
-rw-r--r--. 1 root root 0 Oct 28 21:06 1.doc
-rw-r--r--. 1 root root 0 Oct 28 21:06 2.doc
-rw-r--r--. 1 root root 0 Oct 28 21:06 3.doc
-rw-r--r--. 1 root root 0 Oct 28 21:06 4.doc
-rw-r--r--. 1 root root 0 Oct 28 21:06 2.doc
-rw-r--r--. 1 root root 0 Oct 28 21:06 3.doc
-rw-r--r--. 1 root root 0 Oct 28 21:06 4.doc
将当前目录将当前目录下所有的.txt文件更名下所有的.txt文件更名
将当前目录下所有的.txt文件更名将当前目录下所有的.txt文件更名当前目录下所有的.txt文件更名
0 0
- shell 简单例子(五)
- Shell 编程简单例子
- 简单的shell scripts例子
- 25个简单shell例子
- 2.6.30内核Netfilter的简单例子、五(filterProtocol)
- shell编程的例子(简单的CD管理代码)
- shell 清空日志简单例子(三)
- shell编程其实真的很简单(五)
- shell编程其实真的很简单(五)
- shell编程其实真的很简单(五)
- shell编程其实真的很简单(五)
- shell编程其实真的很简单(五)
- shell编程其实真的很简单(五)
- windows shell语法及简单的例子
- Shell 编程的两个简单小例子
- 用shell简单处理文本的例子
- 自己实现简单shell的小例子
- 【Linux】shell---函数的简单例子
- 快速傅里叶变换FFT(一维)
- Hadoop、Spark、HBase与Redis的适用性见解
- 计算机组成原理3---I/O技术
- 插件--旋钮插件jQuery knob
- MyReport报表系统(三)
- shell 简单例子(五)
- IOS —— 判断设备屏幕尺寸、分辨率
- html字体代码
- 字符数组和字符串的相关函数
- 手机屏幕坐标获取方法
- MSTest.exe 命令行选项
- PL/SQL开发环境准备
- Linux动态库的编译与使用
- android常识