114

I came up with a basic one to help automate the process of removing a number of folders as they become unneeded.

#!/bin/bash
rm -rf ~/myfolder1/$1/anotherfolder
rm -rf ~/myfolder2/$1/yetanotherfolder
rm -rf ~/myfolder3/$1/thisisafolder

This is evoked like so:

./myscript.sh <{id-number}>

The problem is that if you forget to type in the id-number (as I did just then), then it could potentially delete a lot of things that you really don't want deleted.

Is there a way you can add any form of validation to the command line parameters? In my case, it'd be good to check that a) there is one parameter, b) it's numerical, and c) that folder exists; before continuing with the script.

codeforester
  • 39,467
  • 16
  • 112
  • 140
nickf
  • 537,072
  • 198
  • 649
  • 721

10 Answers10

178
#!/bin/sh
die () {
    echo >&2 "$@"
    exit 1
}

[ "$#" -eq 1 ] || die "1 argument required, $# provided"
echo $1 | grep -E -q '^[0-9]+$' || die "Numeric argument required, $1 provided"

while read dir 
do
    [ -d "$dir" ] || die "Directory $dir does not exist"
    rm -rf "$dir"
done <<EOF
~/myfolder1/$1/anotherfolder 
~/myfolder2/$1/yetanotherfolder 
~/myfolder3/$1/thisisafolder
EOF

edit: I missed the part about checking if the directories exist at first, so I added that in, completing the script. Also, have addressed issues raised in comments; fixed the regular expression, switched from == to eq.

This should be a portable, POSIX compliant script as far as I can tell; it doesn't use any bashisms, which is actually important because /bin/sh on Ubuntu is actually dash these days, not bash.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • remember to set +e and use '-eq' instead of '==' for integer comparisons – guns Mar 31 '09 at 00:50
  • Changed it to -eq; what does set +e buy you here? – Brian Campbell Mar 31 '09 at 01:11
  • i found two things in my answer that you may also want to fix in yours: first the SO hilighter goes crazy for $# (treating it as a commentar). i did "$#" to fix it. second, the regex also matches "foo123bar". i fixed it by doing ^[0-9]+$. you may also fix it by using grep's -x option – Johannes Schaub - litb Mar 31 '09 at 01:21
  • 1
    @ojblass I was missing one of the tests he was asking about. Adding that in meant also adding in his directories to test against, which significantly expanded the size of the answer since they can't fit on one line. Can you suggest a more compact way of testing for the existence of each directory? – Brian Campbell Mar 31 '09 at 01:30
  • @Brian I am not knocking your clarifications but tailoring your answer to his specific case makes the answer less 'oh cool I can grab that use it and understand it quickly`. – ojblass Mar 31 '09 at 01:32
  • @Brian Campbell - 'set -e' sets the script to exit on the failure of any simple command not part of a conditional test. This breaks stray && and || lists, so you should turn it off with 'set +e' in case it was inherited by the shell (as part of ENV for instance) – guns Mar 31 '09 at 01:35
  • @ojblass Fair enough; you can still grab the part from before the while loop as generic code, but the while loop is rather special case for answering this question, and I was trying to answer the full question. – Brian Campbell Mar 31 '09 at 01:59
  • @guns "-e When this option is on, if a simple command fails ... and is not a part of an AND or OR list ... then the shell shall immediately exit." In all the shells I tested (bash, zsh, and dash), "foo && bar" and "foo || bar" work fine regardless of the set -e/+e state. – Brian Campbell Mar 31 '09 at 02:03
  • @Brian Campbell Yes, that's true, but it still does raise the ERR signal inside of functions. I know that's not how you presented it, so I should have qualified my comment. – guns Mar 31 '09 at 02:09
  • @guns Hmm, I'm not quite following you. I'm actually relatively new to shell scripting beyond basic command line use and .bashrc, so I'm trying to understand more than argue. In what case will an inherited set -e break this script, or are you just saying that set +e is a good habit to be in? – Brian Campbell Mar 31 '09 at 02:23
  • @Brian Campbell I looked into it and found that I had a misunderstanding: && and || lists work fine with the errexit option on, so long as the final return value for the list is zero. So '[ -n "" ] || false; echo DONE' fails to echo DONE if set -e is set. This was never a problem with your script. – guns Mar 31 '09 at 03:18
  • 1
    as per @Morten Nielsen's answer-comment below, the grep '$[0-9]+^' looks strange indeed. Shouldn't it be '^[0-9]+$' ? – martin jakubik Jun 01 '11 at 08:57
  • I do not have enough votes to leave a comment, but I think there is a minor bug in Brian Campbell's answer above: echo $1 | grep -E -q '$[0-9]+^' || die "Numeric argument required, $1 provided" should be echo $1 | grep -E -q '^[0-9]+$' || die "Numeric argument required, $1 provided" – Morten Nielsen May 31 '11 at 21:31
22

Not as bulletproof as the above answer, however still effective:

#!/bin/bash
if [ "$1" = "" ]
then
  echo "Usage: $0 <id number to be cleaned up>"
  exit
fi

# rm commands go here
Boiler Bill
  • 1,900
  • 1
  • 22
  • 32
22

The sh solution by Brian Campbell, while noble and well executed, has a few problems, so I thought I'd provide my own bash solution.

The problems with the sh one:

  • The tilde in ~/foo doesn't expand to your homedirectory inside heredocs. And neither when it's read by the read statement or quoted in the rm statement. Which means you'll get No such file or directory errors.
  • Forking off grep and such for basic operations is daft. Especially when you're using a crappy shell to avoid the "heavy" weight of bash.
  • I also noticed a few quoting issues, for instance around a parameter expansion in his echo.
  • While rare, the solution cannot cope with filenames that contain newlines. (Almost no solution in sh can cope with them - which is why I almost always prefer bash, it's far more bulletproof & harder to exploit when used well).

While, yes, using /bin/sh for your hashbang means you must avoid bashisms at all costs, you can use all the bashisms you like, even on Ubuntu or whatnot when you're honest and put #!/bin/bash at the top.

So, here's a bash solution that's smaller, cleaner, more transparent, probably "faster", and more bulletproof.

[[ -d $1 && $1 != *[^0-9]* ]] || { echo "Invalid input." >&2; exit 1; }
rm -rf ~/foo/"$1"/bar ...
  1. Notice the quotes around $1 in the rm statement!
  2. The -d check will also fail if $1 is empty, so that's two checks in one.
  3. I avoided regular expressions for a reason. If you must use =~ in bash, you should be putting the regular expression in a variable. In any case, globs like mine are always preferable and supported in far more bash versions.
twernt
  • 20,271
  • 5
  • 32
  • 41
lhunath
  • 120,288
  • 16
  • 68
  • 77
16

I would use bash's [[:

if [[ ! ("$#" == 1 && $1 =~ ^[0-9]+$ && -d $1) ]]; then 
    echo 'Please pass a number that corresponds to a directory'
    exit 1
fi

I found this faq to be a good source of information.

Dave
  • 4,356
  • 4
  • 37
  • 40
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
14

The man page for test (man test) provides all available operators you can use as boolean operators in bash. Use those flags in the beginning of your script (or functions) for input validation just like you would in any other programming language. For example:

if [ -z $1 ] ; then
  echo "First parameter needed!" && exit 1;
fi

if [ -z $2 ] ; then
  echo "Second parameter needed!" && exit 2;
fi
whaley
  • 16,075
  • 10
  • 57
  • 68
13

Use set -u which will cause any unset argument reference to immediately fail the script.

Please, see the article: Writing Robust Bash Shell Scripts - David Pashley.com.

shmichael
  • 2,985
  • 3
  • 25
  • 32
8

one liner Bash argument validation, with and without directory validation

Here are some methods that have worked for me. You can use them in either the global script namespace (if in the global namespace, you can't reference the function builtin variables)

quick and dirty one liner

: ${1?' You forgot to supply a directory name'}

output:

./my_script: line 279: 1: You forgot to supply a directory name

Fancier - supply function name and usage

${1? ERROR Function: ${FUNCNAME[0]}() Usage: " ${FUNCNAME[0]} directory_name"}

output:

./my_script: line 288: 1:  ERROR Function: deleteFolders() Usage:  deleteFolders directory_name

Add complex validation logic without cluttering your current function

Add the following line within the function or script that receives the argument.

: ${1?'forgot to supply a directory name'} && validate $1 || die 'Please supply a valid directory'

You can then create a validation function that does something like

validate() {

    #validate input and  & return 1 if failed, 0 if succeed
    if [[ ! -d "$1" ]]; then
        return 1
    fi
}

and a die function that aborts the script on failure

die() { echo "$*" 1>&2 ; exit 1; }

For additional arguments, just add an additional line, replicating the format.

: ${1?' You forgot to supply the first argument'}
: ${2?' You forgot to supply the second argument'}
AndrewD
  • 4,924
  • 3
  • 30
  • 32
8

Use '-z' to test for empty strings and '-d to check for directories.

if [[ -z "$@" ]]; then
    echo >&2 "You must supply an argument!"
    exit 1
elif [[ ! -d "$@" ]]; then
    echo >&2 "$@ is not a valid directory!"
    exit 1
fi
guns
  • 10,550
  • 3
  • 39
  • 36
5

You can validate point a and b compactly by doing something like the following:

#!/bin/sh
MYVAL=$(echo ${1} | awk '/^[0-9]+$/')
MYVAL=${MYVAL:?"Usage - testparms <number>"}
echo ${MYVAL}

Which gives us ...

$ ./testparams.sh 
Usage - testparms <number>

$ ./testparams.sh 1234
1234

$ ./testparams.sh abcd
Usage - testparms <number>

This method should work fine in sh.

MattK
  • 1,431
  • 13
  • 14
1

Old post but I figured i could contribute anyway.

A script is arguably not necessary and with some tolerance to wild cards could be carried out from the command line.

  1. wild anywhere matching. Lets remove any occurrence of sub "folder"

    $ rm -rf ~/*/folder/*
    
  2. Shell iterated. Lets remove the specific pre and post folders with one line

    $ rm -rf ~/foo{1,2,3}/folder/{ab,cd,ef}
    
  3. Shell iterated + var (BASH tested).

    $ var=bar rm -rf ~/foo{1,2,3}/${var}/{ab,cd,ef}
    
Community
  • 1
  • 1
Xarses
  • 315
  • 3
  • 8