Bash,命令未找到。

5

我在修复这个Bash脚本中遇到了问题,真的不知道我做错了什么,非常感谢您的帮助。

[centoslive@livecd ~]$ sh ListFich.sh test
Synthese, Question 2
ListFich.sh: line 9: [-d: command not found
Il n'y a aucun repertoire qui se nomme test
ListFich.sh: line 15: [-f: command not found
Il n'y a aucun fichier qui se nomme test
[centoslive@livecd ~]$ 

#!bin/bash

echo "Synthese, Question 2"

if test $# -eq 0; then
    echo "Argument Manquants"
    exit 1 
else 
    if [-d $1];then
        ls -d $1
        exit 1
    else 
        echo "Il n'y a aucun repertoire qui se nomme $1"
    fi
    if [-f $1]; then
        if [ -s $1]; then
            ls -l $1
            exit 1
        else 
            ls -l $1
            echo "Le fichier $1 est vide"
            exit 1
        fi
    else 
        echo "Il n'y a aucun fichier qui se nomme $1"   
    fi
fi

3
您似乎在成功时退出了1;按照惯例,这表示失败。 - chepner
2个回答

9
[后面和]前面添加一个空格。
[ -d $1 ]
 ^     ^

3
@JoeE:在处理这个问题时,考虑使用[[]]——bash更强大的内置命令,它们提供了额外的运算符(=~、<、>),并且无需引用字符串操作数。 - DevSolar
这个链接介绍了**[[[**之间的主要区别。 - Salah Eddine Taouririt
能否解释一下原因,并提供一些参考链接?这将使得答案更加有用! - Kissaki
@Kissaki 请参考前面的评论。 - Salah Eddine Taouririt
@tarrsailah,我看到了你的评论。这个网站主要是为问答而设计的,答案应该尽可能完整,这样不是每个寻找答案的人都需要阅读所有的评论。 - Kissaki

1

[] 前后需要留空格。有关详细信息,请参见 help [


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接