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

时间:2013-04-29 06:27:40

标签: linux shell

下面是我正在编写的bash shell脚本的赋值。我有一个 即使我使用-f选项输出-u信息的问题。 这堂课是初级班,所以请耐心等待。将不胜感激 我的代码有一些输入。感谢您抽出时间来检查一下 做。

以下是示例输出:

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

share是一个目录,它是可读的可写|可执行文件abecker是 目前登录的主目录是/ students / abecker

以下是用法

chk -f filepath
  • 如果存在filepath,则以可读句子输出
  • 如果是符号链接,请说明。您不必继续并报告 权限。
  • 如果不存在,请说明。不要继续报告权限
  • 报告它是什么:文件,目录或其他内容,并继续 报告权限:
  • 报告您的读取,写入和执行访问权限的组合 程序有数据。请注意,这取决于谁运行您的 程序。不要尝试通过将权限视为输出来执行此操作 按ls -l。您必须使用测试运算符才能执行此操作。
  • 如果filepath不存在(并且不是符号链接),您的程序应该 在信息性错误消息中报告此内容。在这种情况下,你 应该以错误退出。
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

1 个答案:

答案 0 :(得分:1)

您不是将不同选项的处理放入不同的块中;代码只是传递所有选项的所有内容。

e.g。对于-f选项,您有:

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

然后处理filepath的所有选项,而不将它们放入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函数中,这样可以更容易地阅读代码; e.g。

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选项的类似内容。