-4
#!/bin/bash

LOCATION=$1
FILECOUNT=0
DIRCOUNT=0

if [ "$#" -lt "1" ]
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi

I don't actually get what the If statement is saying can anyone help me to explain this?Thank you

user2786596
  • 135
  • 2
  • 5
  • 10

2 Answers2

3

$1 refers to the first argument of the bash file. In this case, you can pass your directory path by issuing the following command:

# ./test2.sh /path/of/your/directory

#!/bin/bash

LOCATION=$1 #first argument of the script
FILECOUNT=0
DIRCOUNT=0

if [ "$#" -lt "1" ] #if the number of argument(s) ($#) is less than 1
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi

You can read this article for more information about parameter passing. Hope it helps.

Dale
  • 1,903
  • 1
  • 16
  • 24
  • Thanks for the help, but something i am still not clear that, what does it mean by less than 1? maybe i should show you where i get this from http://stackoverflow.com/questions/13727069/count-files-and-directories-using-shell-script – user2786596 Apr 02 '14 at 07:38
  • It's just checking if there is an argument at all. – SKull Apr 02 '14 at 07:41
  • @user3488212 Matthew is right. If there is no argument, the number of argument will zero. – Dale Apr 02 '14 at 07:43
  • suppose i typed in ./test2.sh, i want the output to be like this Usage: ./test2.sh , but when i try that only thing i got is ./test2.sh: line 10: [: 1: integer expression expected] ? – user2786596 Apr 02 '14 at 11:16
1

$1 is the first argument that is passed to the bash script. If you start the script like ./test2.sh argument1 argument2 the $1 will refer argument1.

The if-statement checks, if the count of arguments (that's the $#) is smaller than 1, then it will output the usage statement (as it seems you can't run the script without any argument).

ConcurrentHashMap
  • 4,998
  • 7
  • 44
  • 53