gpt4 book ai didi

linux - 检查文件或用户脚本分配问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:38 26 4
gpt4 key购买 nike

下面是我正在编写的 bash shell 脚本的作业。我有一个即使我正在使用 -f 选项,但输出 -u 信息的问题。这个类是初级类,所以请多多包涵。将不胜感激对我的代码有一些意见。感谢您抽出宝贵时间查看此内容,如果您做。

这是示例输出:

[***@***]$ chk3 -f share

share 是一个目录,它是可读的 |可写 |可执行 |阿贝克是当前登录他们的主目录是/students/abecker

这是用法

chk -f filepath
  • 如果文件路径存在,输出可读的句子
  • 如果是符号链接(symbolic link),请说明。您不必继续并报告权限。
  • 如果不存在,请说出来。不要继续报权限
  • 报告它是什么:文件、目录或其他东西,然后继续报告权限:
  • 报告您的读、写和执行访问权限的组合程序有数据。请注意,这取决于谁运行您的程序。不要试图通过查看权限作为输出来执行此操作通过 ls -l。您必须使用测试运算符来执行此操作。
  • 如果文件路径不存在(并且不是符号链接(symbolic link)),你的程序应该而是在信息性错误消息中报告这一点。在这种情况下,你应该退出并出错。
chk -u user
  • 如果用户存在于系统中,则报告
  • 用户主目录的路径
  • 如果用户当前已登录,请说明。否则,报告它们最后一次已登录。(请注意确保生成可靠且快速。)
  • 如果用户不存在,则在信息性错误消息中报告此情况,并且出现错误退出。

这是我的代码

#!/bin/bash
if [ $# -gt 2 ]
then
echo "only 2 aruments can be used"
exit 1
fi
if [ "$1" != '-f' -a "$1" != '-u' ]
then
echo "first argument must be -f or -u"
exit 1
fi
if [ "$1" = '-f' -a $# -ne 2 ]
then
echo 'Usage: chk -f [FILEPATH]'
exit 1
fi
if [ "$1" = '-f' ]
then
FILEPATH=$2
fi
if [ -L "$FILEPATH" ]
then
echo "$FILEPATH is a symbolic link"
exit 0
elif [ -d "$FILEPATH" ]
then
echo -e "$(basename "$FILEPATH") is a directory and it is \c"
elif [ -f "$FILEPATH" ]
then
echo -e "$(basename "$FILEPATH") is a file and it is \c"
else
echo "I cannot determine what $(basename "$FILEPATH") is"
exit 1
fi
if [ -r "$FILEPATH" ]
then
echo -e "readable | \c"
fi
if [ -w "$FILEPATH" ]
then
echo -e "writable | \c"
fi
if [ -x "$FILEPATH" ]
then
echo -e "executable | \c"
fi
if [ "$1" = '-u' -a $# -eq 1 ]
then
USER=$LOGNAME
elif [ "$1" = '-u' -a $# -eq 2 ]
then
USER=$2
fi
USERINFO=$(grep "^$USER:" /etc/passwd)
if ! grep "^$USER:" /etc/passwd > /dev/null
then
echo "$USER cannot be found on this system"
exit 1
fi
if ! who | grep "^$USER " > /dev/null
then
echo "$USER is not currently logged on and last logged on"
echo "$(last -1 "$USER")"
exit 0
else
echo "$USER is currently logged in their home directory is"
echo "$(echo "$USERINFO" | awk -F":" '{print $6}')"
fi

最佳答案

您没有将不同选项的处理放在不同的 block 中;代码只是通过所有选项的所有内容。

例如对于 -f 选项,您有:

if [ "$1" = '-f' ]
then
FILEPATH=$2
fi

然后处理文件路径的所有选项,而不将它们放入 if 语句中,因此如果您传入 -f-u,它总是传入代码:

if [ -L "$FILEPATH" ]
then
echo "$FILEPATH is a symbolic link"
exit 0
elif

如果您不想将程序分解为函数,您要做的是将所有与处理 -f 选项相关的代码放入同一个 if 语句中,有点像:

if [ "$1" = '-f' ]
then
FILEPATH=$2
if [ -L "$FILEPATH" ]
then
echo "$FILEPATH is a symbolic link"
exit 0
elif [ -d "$FILEPATH" ]
then
echo -e "$(basename "$FILEPATH") is a directory and it is \c"
elif [ -f "$FILEPATH" ]
then
echo -e "$(basename "$FILEPATH") is a file and it is \c"
else
echo "I cannot determine what $(basename "$FILEPATH") is"
exit 1
fi
if [ -r "$FILEPATH" ]
then
echo -e "readable | \c"
fi
if [ -w "$FILEPATH" ]
then
echo -e "writable | \c"
fi
if [ -x "$FILEPATH" ]
then
echo -e "executable | \c"
fi
fi # if [ "$1" = '-f' ]

同样对于-u选项,您需要将其分解为多个语句,然后处理该语句的所有选项:

if [ "$1" = 'u' ]
then
if [ $# -eq 1 ]
then
USER=$LOGNAME
elif [ $# -eq 2 ]
then
USER=$2
fi
USERINFO=$(grep "^$USER:" /etc/passwd)
if ! grep "^$USER:" /etc/passwd > /dev/null
then
echo "$USER cannot be found on this system"
exit 1
fi
if ! who | grep "^$USER " > /dev/null
then
echo "$USER is not currently logged on and last logged on"
echo "$(last -1 "$USER")"
exit 0
else
echo "$USER is currently logged in their home directory is"
echo "$(echo "$USERINFO" | awk -F":" '{print $6}')"
fi
fi # if [ "$1" = '-u' ]

不过,我建议将作用于选项的代码放入 shell 函数中,这样更容易阅读代码;例如

filepath() {
FILEPATH="$1"
if [ -L "$FILEPATH" ]
then
echo "$FILEPATH is a symbolic link"
exit 0
elif [ -d "$FILEPATH" ]
then
echo -e "$(basename "$FILEPATH") is a directory and it is \c"
elif [ -f "$FILEPATH" ]
then
echo -e "$(basename "$FILEPATH") is a file and it is \c"
else
echo "I cannot determine what $(basename "$FILEPATH") is"
exit 1
fi
if [ -r "$FILEPATH" ]
then
echo -e "readable | \c"
fi
if [ -w "$FILEPATH" ]
then
echo -e "writable | \c"
fi
if [ -x "$FILEPATH" ]
then
echo -e "executable | \c"
fi
}

然后是处理代码:

if [ "$1" = '-f' ]
then
filepath "$2"
fi

-u 选项类似的内容。

关于linux - 检查文件或用户脚本分配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16272454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com