Wednesday, August 21, 2013

Setting a default if an argument isn't passed

In doing some bash scripting... I use some simple scripts to automate commands so I don't have to type in (or remember) all the parameters. This script uses the du command to show me the disk usage on directories. The depth parameter specifies how deep in the path to go when reporting. I wanted the script to function correctly without an argument. So my solution was to test for the argument and use a default value if it's not present. Without a default value, this command would produce an error.
# If depth is not specified, default to zero
if [[ $depth = "" ]]; then
du -mc -d$depth --apparent-size * | sort -nr | less

No comments:

Post a Comment