【问题标题】:Analyzing Files in a Shell Script在 Shell 脚本中分析文件
【发布时间】:2013-11-27 01:07:27
【问题描述】:

我需要编写一个带有一个或多个参数(文件名)的 shell 脚本。无论文件名是否包含空格,都应正确处理文件名。 对于每个文件,脚本应检查文件是否可读、可写、可执行、是否为普通文件以及是否为目录。对于这些检查中的每一项,应将 Y 或 N 放在相应的列中。如果文件不存在,则应在每个字段中放置破折号“---”。

Example output: 
Read Write Execute Plain Dir Filename 
Y    Y     Y       Y     N   /tmp/testfiles/executable 
Y    Y     N       Y     N   /tmp/testfiles/file with spaces 
Y    N     N       Y     N   /tmp/testfiles/justread 
N    Y     N       Y     N   /tmp/testfiles/justwrite 
N    N     N       Y     N   /tmp/testfiles/noperms 
---  ---   ---     ---   --- /tmp/testfiles/nosuchfile 
Y    Y     N       Y     N   /tmp/testfiles/readwrite 
Y    Y     Y       N     Y   /tmp/testfiles/somedir

我对 UNIX shell 脚本不是很熟悉,但是看了网上的各种文章后,我想到了以下解决方案。

#! /bin/sh
echo Read Write Execute Plain Dir Filename
argnum=0
while $argnum < $# do
FileExists $($argnum)
PrintFileName $($argnum)
done

function FileExists()
{
if test -e $1
then
    IsReadable $1
    IsWritable $1
    IsExecutable $1
    IsPlain $1
    IsDir $1
else
    echo --- --- --- --- ---
}

function IsReadable()
{
if test -r $1
then
    echo Y
else
    echo N
}

function IsWritable()
{
if test -w $1
then
    echo Y
else
    echo N
}

function IsExecutable()
{
if test -x $1
then
    echo Y
else
    echo N
}

function IsPlain()
{
if test -f $1
then
    echo Y
else
    echo N
}

function IsDirectory()
{
if test -d $($argnum)
then
    echo Y
else
    echo N
}

function PrintFilename()
{
echo $1
}

很遗憾,脚本无法正确执行。我知道存在问题(尤其是格式问题),但我不确定如何解决它们。非常感谢您的任何帮助/建议。

【问题讨论】:

  • “很遗憾,脚本没有正确执行。”,什么是正确的?你能附上当前的输出吗?
  • Read Write Execute Plain Dir Filename ./script: line 7: syntax error near unexpected token done' ./script: line 7: done'
  • 您需要在字符串周围添加一些 " 或 ' 以避免出现空格问题。

标签: linux bash shell terminal sh


【解决方案1】:

虽然“function Name ()”语法有效,但我更喜欢declare -f Name返回的样式作为我的书面形式,因为我使用“declare -f name ...”来重现函数体。

此外,您可能会从每个函数中考虑“echo Y”和“echo N”,只需返回断言的真实性。所以,...IsReadable,.. 变成:

  IsReadable () 
  {
      test -r $1
  }

使用过

  IsReadable $1 && echo Y || echo N

因为我没有找到“&&”(AND)和“||” (OR) 语法太嘈杂。另外,我更喜欢这个

  [[ -r $1 ]] && echo Y || echo N

所以,我的isreadable

  isreadable () {  [[ test -r $1 ]] ; }

因为我允许“declare -f”规则的单行例外,甚至还有一个函数,fbdy that:如果函数体(少头,尾)适合一行,则显示为单行,否则显示为默认值。

很高兴看到您使用函数。继续努力吧。我强烈鼓励使用它们。

【讨论】:

    【解决方案2】:

    Read Write Execute Plain Dir Filename ./script: line 7: syntax error near unexpected token done' ./script: line 7: done'

    因为,do 之前需要一个;

    Bash 从上到下扫描,并执行每一行。所以在前几行中,Bash 不知道FileExistsPrintFileName。所以你需要做的是把声明放在之前调用它们。

    function FileExists
    {
    ...
    }
    
    function IsReadable
    {
    ...
    }
    // more functions..
    
    //Iterate here and call the above functions.
    

    更简洁的迭代方式:

    for var in "$@"
    do
        FileExists $var
        PrintFileName $var
    done
    

    您可能在格式化时遇到问题,因为 echo 会吐出一个换行符;而且您可能无法在一行中得到任何东西。改用printf,手动写出printf "\n"

    另外,@devnull 指出,fiif 块的每个实例中都缺失。

    【讨论】:

    • 谢谢!如果没有传入任何参数,脚本现在可以正确执行。如果传入任何文件名,我会得到以下输出: ./script testfile Read Write Execute Plain Dir Filename ./script: line 6: FileExists: command not found ./script :第 7 行:PrintFileName:找不到命令
    • 现在很好用!感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多