5

i'm having trouble fixing the error in this bash script, I seriously have no idea what I did wrong, any help is appreciated thanks

[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
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Joe E
  • 85
  • 2
  • 6

2 Answers2

9

Add a space after [ and before ].

[ -d $1 ]
 ^     ^
Salah Eddine Taouririt
  • 24,925
  • 20
  • 60
  • 96
  • 3
    @JoeE: While you are at it, consider using `[[` and `]]` -- bash's more powerful builtins, which provide additional operators (=~, <, >) and remove the need to quote your string operands. – DevSolar May 22 '13 at 14:41
  • this [link](http://mywiki.wooledge.org/BashFAQ/031) present the main differences between **[** and **[[**. – Salah Eddine Taouririt May 22 '13 at 14:45
  • Care to explain why and give some reference link? That would make this answer so much more useful! – Kissaki Sep 07 '13 at 17:21
  • @Kissaki see the previous comment. – Salah Eddine Taouririt Sep 07 '13 at 17:24
  • @tarrsailah I saw that comment. This is mainly / targeted to be a Q&A site though rather than a Q&A&C site. Answers should be as complete as possible so not everybody looking for an answer needs to read all the comments as well. – Kissaki Sep 07 '13 at 17:25
1

You need spaces before and after [ and ]. See help [ for details.

l0b0
  • 55,365
  • 30
  • 138
  • 223