1
#
# if MAXFILES is not set, set to 10
#

if [ -z "MAXFILES" ]
then
        MAXFILES=10
fi

#
# now check to see if the number of files being removed is > MAXFILES
# but only if MAXFILES = 0
#

if [ $# -gt "$MAXFILES" -a "$MAXFILES" -ne 0 ]
then
        # if it is, prompt user before removing files
        echo "Remove $# files (y/n)? \c"
        read reply
        if [ "$reply" = y ]
        then
                rm "$@"
        else
                echo "files not removed"
        fi
else
        # number of args <= MAXFILES
        rm "$@"
fi

The above program I have to remove files. However when I attempt to run it, its telling me that

line 15: [: : integer expression expected

  • 5
    You're missing the dollar sign in if [ -z "MAXFILES" ]. – Mikel Dec 2 '14 at 16:36
  • 3
    You can assign variable in easy way: if [ $# -gt "${MAXFILES:=10}" -a "$MAXFILES" -ne 0 ] is enough without above lines – Costas Dec 2 '14 at 16:45
2

The problem is here:

if [ -z "MAXFILES" ]
then
        MAXFILES=10
fi
# ...
if [ $# -gt "$MAXFILES" -a "$MAXFILES" -ne 0 ]

You are checking to see whether the string MAXFILES is zero. Since it's not, $MAXFILES never gets set, and so your later test is lexing as:

if [ $# -gt "" -a "" -ne 0 ]

This is why it's complaining about needing an integer.

What you want to do is this:

if [[ -z "$MAXFILES" ]]; then
  MAXFILES=10
fi

The difference is the dollar sign, which you are missing. Also, I tend to use [[ ]] rather than [ ] for tests (when using bash) as they have some nice built-in sanity checks and are more versatile than [ and test.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.