Bash Unix Shell脚本:如果目录中不存在给定的扩展文件,则显示自定义消息

Bash Unix Shell脚本:如果目录中不存在给定的扩展文件,则显示自定义消息,bash,shell,unix,Bash,Shell,Unix,我有以下unixshell脚本,用于列出给定目录中的文件。只有我们需要传递文件的扩展名,脚本应该列出一个或多个文件或显示自定义消息 我的尝试: 脚本: #!/bin/sh FileNameWithPath=`ls home\docs\customers\*.$1 | wc -w` if [ $FileNameWithPath -gt 0 ] then ls home\docs\customes\*.$1 else echo "Custom Message abo

我有以下unixshell脚本,用于列出给定目录中的文件。只有我们需要传递文件的扩展名,脚本应该列出一个或多个文件或显示自定义消息

我的尝试: 脚本

#!/bin/sh

FileNameWithPath=`ls home\docs\customers\*.$1 | wc -w`

if [ $FileNameWithPath -gt 0 ]
then
     ls home\docs\customes\*.$1
else
     echo "Custom Message about failure(File not found)"
fi
$ ./Test.sh txt
跑步

#!/bin/sh

FileNameWithPath=`ls home\docs\customers\*.$1 | wc -w`

if [ $FileNameWithPath -gt 0 ]
then
     ls home\docs\customes\*.$1
else
     echo "Custom Message about failure(File not found)"
fi
$ ./Test.sh txt

注意:如果我提供存在的文件扩展名,上述脚本可以正常工作,但是如果我提供一些不存在的文件扩展名,它将通过错误和自定义错误消息来完成。我只想打印自定义消息,就是这样。

只需一个命令即可:

ls home/docs/customers/*.$1 2> /dev/null || echo "Custom message about failure (File not found)"
第一个命令(“ls”)尝试列出文件。如果失败,它将打印错误消息(被“2>/dev/null”抑制)并返回错误代码。由于退出代码与0不同,因此将执行第二部分(“echo”)

如果要保留代码,可以通过以下方式删除将stderr重定向到/dev/null的ls错误:

FileNameWithPath=`ls home\docs\customers\*.$1 2>/dev/null | wc -w`

只需一个命令即可:

ls home/docs/customers/*.$1 2> /dev/null || echo "Custom message about failure (File not found)"
第一个命令(“ls”)尝试列出文件。如果失败,它将打印错误消息(被“2>/dev/null”抑制)并返回错误代码。由于退出代码与0不同,因此将执行第二部分(“echo”)

如果要保留代码,可以通过以下方式删除将stderr重定向到/dev/null的ls错误:

FileNameWithPath=`ls home\docs\customers\*.$1 2>/dev/null | wc -w`

这不需要使用
ls

您可以通过globbing本身实现这一点:

# turn on glob failure for no matches
shopt -s failglob

# list files or a custom error message
(echo home/docs/customers/*."$1") 2>/dev/null ||
echo "Custom Message about failure"

这不需要使用
ls

您可以通过globbing本身实现这一点:

# turn on glob failure for no matches
shopt -s failglob

# list files or a custom error message
(echo home/docs/customers/*."$1") 2>/dev/null ||
echo "Custom Message about failure"

您收到的错误消息发生在分配给
FileNameWithPath
的行中。您可以通过将其重定向到/dev/null来抑制它。i、 e.
2>/dev/null

使用
$()
而不是backtick操作符要好得多(并且符合Posix),因为脚本是用
#启动的/bin/sh
而不是
#/bin/bash
。然后,您将可以在现代伯恩贝壳上随身携带。 使用
$()
的另一大好处是,它们可以很容易地嵌套,而在嵌套时必须避免反勾号

正如Andrea Carron在回答中指出的那样,您可以使用
|
逻辑or运算符在一行上完成整个操作。这是一个很常见的成语

万一你的MVCE指的是更复杂的东西,我在下面为你修正了它

#!/bin/sh

FileNameWithPath=$(ls home\docs\customers\*.$1 2>/dev/null | wc -w)

if [ $FileNameWithPath -gt 0 ]
then
     ls home\docs\customes\*.$1
else
     echo "Custom Message about failure(File not found)"
fi

您收到的错误消息发生在分配给
FileNameWithPath
的行中。您可以通过将其重定向到/dev/null来抑制它。i、 e.
2>/dev/null

使用
$()
而不是backtick操作符要好得多(并且符合Posix),因为脚本是用
#启动的/bin/sh
而不是
#/bin/bash
。然后,您将可以在现代伯恩贝壳上随身携带。 使用
$()
的另一大好处是,它们可以很容易地嵌套,而在嵌套时必须避免反勾号

正如Andrea Carron在回答中指出的那样,您可以使用
|
逻辑or运算符在一行上完成整个操作。这是一个很常见的成语

万一你的MVCE指的是更复杂的东西,我在下面为你修正了它

#!/bin/sh

FileNameWithPath=$(ls home\docs\customers\*.$1 2>/dev/null | wc -w)

if [ $FileNameWithPath -gt 0 ]
then
     ls home\docs\customes\*.$1
else
     echo "Custom Message about failure(File not found)"
fi

只需将错误重定向添加到脚本第二行的
null
设备文件:-

FileNameWithPath=`ls home\docs\customers\*.$1 2>/dev/null | wc -w`

只需将错误重定向添加到脚本第二行的
null
设备文件:-

FileNameWithPath=`ls home\docs\customers\*.$1 2>/dev/null | wc -w`

完美的很好,很好!很好。