1.编写一个shell脚本,显示当前日期时间,shell的搜索路径、用户账户及所在的目录位置
#!/bin/bash
echo "Current date is `date`" //显示当前日期时间
echo "Current shell path is `echo $PATH`" //shell搜索路径
echo "User is `whoami`" //用户账户
echo "Current directory `pwd`" //目录位置
结果演示:
2.编写一个shell脚本,从键盘输入一个文件名,判断/root目录下是否存在该文件,不存在则新建此文件。
首先切换到/root目录下
在/root目录下编写shell2.sh
#!/bin/bash
echo -n "Enter a file name:"
read file
if [ -f "/root/$file" ]
then
echo "the file is exist!"
else
touch $file
echo "the file doesn't exist!Create over. "
fi
结果演示:
3.编写一个shell脚本,删除当前目录下所有文件大小为0的文件名为*.c文件。
首先在当前目录下创建文件大小为0的文件
编写shell3.sh
#!/bin/bash
for filename in `ls`
do
if test -d $filename
then
b=0
else
a=$(ls -l $filename | awk '{print $5}')
if test $a -eq 0
then
rm $filename
fi
fi
done
演示结果: