shell编程:入门例子

来源:互联网 发布:免费推广软件下载 编辑:程序博客网 时间:2024/05/01 07:23

1,编写一个Bash脚本,它以一个普通文件作为参数。如果这个文件的大小为0,则把它删除。否则逐行打印文件的名字,大小,

    硬链接的个数,拥有者和最后的修改日期。脚本应该有恰当的的错误检查功能。

    源代码:

#! /bin/bash
declare filename=$1 //第一个参数为文件名
# 判断该文件是否是一个普通文件

if test -f $filename
        then
                #判断文件长度是否大于0

                if test -s $filename 
                        then
                                set $(ls -il $filename)
                echo "File Name  size  hardlink   owner  lastupdatedatal"
                echo "$9    $6   $3         $4       $7"
                 #文件长度等于0,则删除文件

                else
                        rm $filename
                        echo "Size of $filename is 0,already delete"
                fi
                exit 0
#不是普通文件则提示和推出

else
        echo "$filename is not a  regular file"
        exit 0
fi
~

原创粉丝点击