newbie would like help

B

Brian Garcia

Guest
Hi,
My name is Brian, I am retired AF, I just started some community college classes of which one is a Linux class. We have been tasked with writing a script that when given a directory as an argument it should only display the directories in that directory, if no directory is give it echos a usage then exits. Can anyone walk me through this?
Any help would be greatly appreciated!
Brian
 


OK, take a look at this. Don't worry too much about the code, I'll explain it as I go:

Code:
#!/bin/bash

if [ $# -eq 1 ] && [ -d $1 ]; then
    find $1 -maxdepth 1 -type d
else
    echo $0 - Lists all directories in a specified directory
    echo Usage:
    echo $0 directoryName
    echo OR:
    echo $0 /path/to/directory
fi

Explanation:
The first line '#!/bin/bash' uses the shebang operator (#!) to tell the shell which interpreter to use to interpret the script. In this case bash.

The if statement at line 3 checks that two conditions are true:
[ $# -eq 1 ]
Checks that the number of arguments ($#) passed to the script is exactly one. -eq is the equality operator. Checks that the number of arguments equals 1.

[ -d $1 ]
Checks whether the parameter passed to the script ($1) is the name of a valid directory. -d means check if the following item is a directory. You can also use -f to check for a file, or -L for a symbolic link etc.
There is an example list of expressions that can be used with if statements to check files for certain conditions here:
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

The boolean AND operator (&&) between both conditions is used to ensure that both of these conditions are true.

If both conditions are TRUE, we execute the find command.

So effectively, the if statement says "If I (the script) was passed exactly one parameter AND that parameter was the name or path of a valid directory, then I execute the find command in the line under the 'then' clause."

Which moves us onto the find command:
The find command uses parameter 1 ($1) as the directory to start searching in. Setting the maxdepth to 1 prevents find from recursing into any sub-directories. And setting -type to d causes it to only list directories.

So the find command effectively says:
Find all directories that exist in the specified directory (but don't recurse into any sub-directories)

If the conditions in the if statement at line 3 were not met - i.e. The number of parameters passed to the script was more or less than one; or the passed-in parameter was not a valid directory name. We end up executing the code under the 'else' statement. This prints some usage information using the echo command.
Incidentally, $0 is the name of the script. This actually gets passed to the script too.

When you call a script/program from the command line like so:
./somescript param1 param2 ... paramN

Each part of the command line used to call the script is passed into the script as parameters, stored in numbered variables from $0 to $n, where n is the number of parameters.
$0 is always the name of the script/program. $1 is the first parameter, $2 is the second parameter etc.....

I hope I have explained this clearly enough!
 
Hi JasKinasis,
Thanks for great explanation! I will give a try and go over this again and again, just so I fully understand. The last time I touched a command line was high school in '84'- '85'. again.
Thank you!
 
OK, take a look at this. Don't worry too much about the code, I'll explain it as I go:

Code:
#!/bin/bash

if [ $# -eq 1 ] && [ -d $1 ]; then
    find $1 -maxdepth 1 -type d
else
    echo $0 - Lists all directories in a specified directory
    echo Usage:
    echo $0 directoryName
    echo OR:
    echo $0 /path/to/directory
fi

Explanation:
The first line '#!/bin/bash' uses the shebang operator (#!) to tell the shell which interpreter to use to interpret the script. In this case bash.

The if statement at line 3 checks that two conditions are true:
[ $# -eq 1 ]
Checks that the number of arguments ($#) passed to the script is exactly one. -eq is the equality operator. Checks that the number of arguments equals 1.

[ -d $1 ]
Checks whether the parameter passed to the script ($1) is the name of a valid directory. -d means check if the following item is a directory. You can also use -f to check for a file, or -L for a symbolic link etc.
There is an example list of expressions that can be used with if statements to check files for certain conditions here:
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

The boolean AND operator (&&) between both conditions is used to ensure that both of these conditions are true.

If both conditions are TRUE, we execute the find command.

So effectively, the if statement says "If I (the script) was passed exactly one parameter AND that parameter was the name or path of a valid directory, then I execute the find command in the line under the 'then' clause."

Which moves us onto the find command:
The find command uses parameter 1 ($1) as the directory to start searching in. Setting the maxdepth to 1 prevents find from recursing into any sub-directories. And setting -type to d causes it to only list directories.

So the find command effectively says:
Find all directories that exist in the specified directory (but don't recurse into any sub-directories)

If the conditions in the if statement at line 3 were not met - i.e. The number of parameters passed to the script was more or less than one; or the passed-in parameter was not a valid directory name. We end up executing the code under the 'else' statement. This prints some usage information using the echo command.
Incidentally, $0 is the name of the script. This actually gets passed to the script too.

When you call a script/program from the command line like so:
./somescript param1 param2 ... paramN

Each part of the command line used to call the script is passed into the script as parameters, stored in numbered variables from $0 to $n, where n is the number of parameters.
$0 is always the name of the script/program. $1 is the first parameter, $2 is the second parameter etc.....

I hope I have explained this clearly enough!
Nicely done!:):)
 

Staff online


Top