简体   繁体   English

在Shell脚本中分析文件

[英]Analyzing Files in a Shell Script

I need to write a shell script that takes one or more arguments (filenames). 我需要编写一个包含一个或多个参数(文件名)的shell脚本。 Filenames should be handled corretly regardless of whether or not they contain spaces. 无论文件名是否包含空格,都应正确处理。 For each file, the script should check if the file is readable, writeable, executable, is a plain file, and is a directory. 对于每个文件,脚本应检查文件是否可读,可写,可执行,是否是纯文件以及是否是目录。 For each of these checks, aa Y or N should be placed in the appropriate column. 对于这些检查中的每一项,都应将Y或N放置在适当的列中。 If the file does not exist, dashes "---" should be placed in each of the fields. 如果文件不存在,则在每个字段中都应加上破折号“ ---”。

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

I am not very familiar with UNIX shell scripts, but after reading through various articles online, I came up with the following solution. 我对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
}

Unfortunately, script doesn't execute properly. 不幸的是,脚本无法正确执行。 I know there are problems (especially with the formatting), but I'm not sure how to fix them. 我知道有问题(尤其是格式问题),但是我不确定如何解决它们。 Any help/suggestions you have would be very much appreciated. 您的任何帮助/建议都将不胜感激。

Read Write Execute Plain Dir Filename ./script: line 7: syntax error near unexpected token done' ./script: line 7: done' 读写执行普通Dir文件名./script:第7行:意外标记附近的语法错误已完成'./script:7行:已完成

Its because, you need a ; 这是因为,您需要一个; before do . do之前。

Bash scans from top to down, and executes every line. Bash从上到下扫描,并执行每一行。 So in the top few lines, Bash does not know about FileExists and PrintFileName . 因此,在前几行中,Bash不了解FileExistsPrintFileName So what you'd need to do is put the declarations before calling them. 因此,您需要做的是调用声明之前放置声明。

function FileExists
{
...
}

function IsReadable
{
...
}
// more functions..

//Iterate here and call the above functions.

Cleaner way of iterating: 更干净的迭代方式:

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

You might have problems with formatting because echo spits out a newline; 您可能会遇到格式问题,因为echo会换行。 and you might just not get things in a single line. 而且您可能无法一things而就。 use printf instead, and manually write out printf "\\n" manually. 请改用printf ,然后手动写出printf "\\n"

Also, @devnull points out, fi is missing in every single instance of an if block. 另外,@ devnull指出,在if块的每个单个实例中都缺少fi

while "function Name () " syntax works, I prefer the style returned by declare -f Name as my written form, since I use "declare -f name ..." to reproduce function bodies. 尽管“ function Name()”语法有效,但我更喜欢declare -f Name返回的样式作为我的书面形式,因为我使用“ declare -f name ...”来重现函数主体。

also, you might factor the "echo Y" and "echo N" from each function, simply returning the truth of the assertion. 同样,您可以从每个函数中考虑“回声Y”和“回声N”,只需返回断言的真相即可。 so, ...IsReadable, .. become: 因此,... IsReadable,..变成:

  IsReadable () 
  {
      test -r $1
  }

and used 并使用

  IsReadable $1 && echo Y || echo N

since I don't find the "&&" (AND) and the "||" 因为我找不到“ &&”(与)和“ ||” (OR) syntax too noisy. (OR)语法太吵。 Also, i prefer this 另外,我更喜欢这个

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

So, my isreadable : 所以,我是isreadable

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

since i allow one-line exceptions to the "declare -f" rule, and even have a function, fbdy which does that: if the function body (less header, trailer) fits on one line, show it as a one-liner, otherwise, show it as the default. 因为我允许“ declare -f”规则的单行例外,甚至还有一个函数, fbdy可以做到这一点:如果函数主体(较少的标头,尾部)适合一行,则将其显示为单行,否则,将其显示为默认值。

Good to see you using functions. 很高兴看到您使用函数。 Keep it up. 保持。 I mightily encourage their use. 我大力鼓励使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM