Shell脚本问题与“ if [-[-d $ {directory}]”

A_Pete

我想要执行以下循环操作,以在路径(1e-04、3e-07等)中找到所有“ * e-0 [1-9]”目录。如果找到这样的目录,我希望执行一些命令。我的问题是if条件,它使用shbash给我不同的错误我为sh遇到的错误是“ [:意外序列”,而对于bash则是“ [:过多参数”。我在stackoverflow的其他问题中发现了此问题,但是这些问题主要与if条件中从“ ==”到“ =”的转换有关,在这里不是这种情况。问题部分如下所示:

for i in `seq 1 9`;
do
    directory=*e-0$i
    // directory="*e-0"+$i    // also tried things like that
    if [ -d ${directory} ]    // THIS is the line stated in the error
    then
        echo $directory
    fi
done

提前致谢。

00

使用更多报价™

if [ -d "$directory" ]

更简单的方法来处理整个循环:

shopt -s nullglob
for directory in *e-0[1-9]/

这不需要检查是否存在匹配项,因为循环主体将仅针对匹配路径运行。斜杠确保仅匹配目录

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Shell脚本| tr -d'\ [\] \ _'

来自分类Dev

Shell脚本替换问题

来自分类Dev

Shell脚本功能问题

来自分类Dev

Shell脚本功能问题

来自分类Dev

查找问题(shell脚本)

来自分类Dev

\ W \ D在shell脚本中代表什么?

来自分类Dev

init.d脚本启动问题

来自分类Dev

Active Directory VBScript问题

来自分类Dev

从shell问题执行脚本

来自分类Dev

从shell问题执行脚本

来自分类Dev

shell脚本中sudo的问题

来自分类Dev

Shell脚本中的编码问题

来自分类Dev

Shell脚本中的echo问题

来自分类Dev

Shell脚本单引号问题

来自分类Dev

shell脚本中sudo的问题

来自分类Dev

Is it possible to delete directory when in the directory in shell

来自分类Dev

Swift iOS GoogleAPIClientForREST – Shell 脚本调用错误 GTMSessionFetcher.framework: No such file or directory

来自分类Dev

优化Active Directory审核脚本

来自分类Dev

Shell script change directory with variable

来自分类Dev

Directory.GetFiles()性能问题

来自分类Dev

bash / shell CGI脚本的Perl打印问题

来自分类Dev

初学者Unix Shell脚本问题

来自分类Dev

我的shell脚本有什么问题?

来自分类Dev

在Applescript中执行Shell脚本问题

来自分类Dev

我的shell脚本有什么问题?

来自分类Dev

使用运算符的shell脚本问题

来自分类Dev

安装Hadoop时Shell脚本面临的问题

来自分类Dev

在Shell脚本中获取$ PWD的问题

来自分类Dev

我的shell脚本出了什么问题