LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 773|回复: 2

在linux 下自己建造一个新的命令

[复制链接]
发表于 2004-5-1 04:54:04 | 显示全部楼层 |阅读模式
CHK can be used to check a file if it is invoked as
chk -f filepath

or to check a user if it is invoked as
chk -u user

if filepath exists,output
. what it is: file, directory, symbolic link or other
. what combination or read,write and execute access right your program has for the data. Note that this is dependent on who runs your program: you cannot easily determine this by looking at the file's permission as output by ls -l
if the filepath doesn't exist or is inaccessible, your program should report this instead in a informative error message.

if the user exits on the system,report
.the path ot he user's home directory
.when the user last logged in OR if the user is currently loggedin
if the user doesn't exist, report his as an error.

下到有个以运行的例子:
bash$ chk -f chk
'chk' is a regular file
It is readable, writable, executable.
bash$
bash$ chk -f .
'.' is a directory
It is readable, writable, executable.
bash$
bash$ chk -f /dev/null
'/dev/null' is not a known file type
It is readable, writable, not executable.
bash$
bash$
bash$ chk -f xxxx
chk:ERROR: 'xxxx' does not exist
bash$
bash$ echo $?
1
bash$
bash$ chk -u gboyd
'gboyd's home directory is /users/gboyd
Currently logged in
bash$ echo $?
0
bash$ chk -u foobar6
chk:ERROR: user 'foobar6' cannot be found on the system
bash$ echo $?
1
bash$ chk -d
chk:ERROR: must have exactly two arguments
chk: USAGE: chk { -f filepath | -u user }
 楼主| 发表于 2004-5-1 04:55:22 | 显示全部楼层

这是我自己写的,只是写了一点就不会写了

希望大家指点一下我的code.

bash-2.04$ cat chk2
#!/bin/bash
#
#
#
#
#
fatal() {
    echo "$(basename $0): ERROR: $*"
    exit 1
}
usage() {
    echo "Usage: $(basename $0) -f filepath or -u user"
}

if [ $# -ne 2 ]; then
    usage
    fatal "incorrect number of arguments"
fi
if [ "$1" != "-f" ]; then
    usage
    fatal "first argument must be -f or -u"
fi
filepath=$2
user=$2
if [ ! -f "$filepath" ]; then
    usage
    fatal "'$filepath' does not exist file"
fi
if [ ! -r "$filepath" ]; then
   usage
   fatal "'$filepath' cannot read"
fi
if [ -f "$filepath" -o -r "$filepath" ]; then
  echo "$(ls -l $filepath)"

fi
bash-2.04$
 楼主| 发表于 2004-5-2 00:22:22 | 显示全部楼层
up ,大虾们,出下手啦. 我快就要交功课了...
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表