Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.
#
# 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

Can anyone help me?

Thanks, I am new to UNIX programming.

share|improve this question
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

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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