Does anyone know why this script would give me an error (only sometimes) with it saying "too many arguments"?

#!/bin/bash

HOME='/home/eric'

arrFiles=("$HOME/.kde/share/apps/kaffeine/playlists/NEW.kaffeine"\
 "$HOME/.kde/share/apps/kcookiejar/cookies"\
 "$HOME/.kde4/share/apps/kcookiejar/cookies")
arrDirs=("$HOME/.kde4/share/apps/RecentDocuments/*"\
 "$HOME/.kde/share/apps/RecentDocuments/*"\
 "$HOME/.macromedia/Flash_Player/#SharedObjects/*")

case "$1" in

trash)
	 rm -r $HOME/.local/share/Trash/*
	 echo Trash emptied
;;

history)
  #Clears Files
  for x in "${arrFiles[@]}"; do
	 if [ $x = "$x" ]; then 
	   rm -rf $x
	 else
    	   echo "nothing to empty!"
	 fi
  done
  
  #Clears Dirs
  for x in "${arrDirs[@]}"; do
	 if [ $x = "$x" ]; then 
	   echo "nothing to empty!"
	 else
    	   rm -rf $x
	 fi
  done
;;

all)
  for x in "${arrFiles[@]}"; do
	 if [ $x = "$x" ]; then 
	   rm -rf $x
	 else
    	   echo "nothing to empty!"
	 fi
  done
  
  #Clears Dirs
  for x in "${arrDirs[@]}"; do
	 if [ $x = "$x" ]; then 
	   echo "nothing to empty!"
	 else
    	   rm -rf $x
	 fi
  done

	 rm -r $HOME/.local/share/Trash/*
;;

esac

Output:

root@siduxbox:/home/eric# empty trash
Trash emptied
root@siduxbox:/home/eric# empty history
/usr/bin/empty: line 31: [: too many arguments
nothing to empty!
root@siduxbox:/home/eric# empty history
nothing to empty!
nothing to empty!
nothing to empty!

Recommended Answers

All 6 Replies

Hey there,

I can see two places where this script might get you.

1. Since you create parts of some arrays by using the wildcard asterisk (*) you run the risk of creating really long lists which could give you a problem with programs like "rm" which tap out after they reach their limit of characters (not shown here ;)

2. The other one is happening because there's nothing to empty and the x in your "for x" loop doesn't contain a value. I know there are better ways around this, but I generally would just do a cursory check on the array before iterating through it. For instance, if you took the value of:

${#arrDirs[@]}

or

${#arrDirs[*]}

If those counts equal zero, you can go to your "nothing to do message" and otherwise iterate through the array.

Hope that helps some :) Happy THanksgiving !

, Mike

Sorry for a delayed response...should the else conditions take care of that?

I thought it was like this:

If the DIR equal the DIR, then remove it.
if it's anything else, then it's nothing.

But what you're saying is to try:

for x in "${arrDirs[@]}"; do
	 if [ $x = "$x" ]; then 
	   rm -rf $x
	 elif [ $x = 0 ]; then 
    	   echo "nothing to empty!"
	 fi
  done

Correct? I guess that's right since it wouldn't equal anything else except for zero if its not there...but I thought that's what an else is for. Seems the same to me.

Hey again,

The actual issue for you here, I think, is that every $x is going to equal $x. For this reason. Taking the top of your code:

for x in "${arrDirs[@]}"; do
	 if [ $x = "$x" ]; then

the "for x" is populating the variable x with the first value of @arrDirs, then the second, etc, until it's done. Within the loop, however, you're comparing x with x. It would seem that they would have to match because they're the exact same variable with the exact same value.

It's possible you may be looking to do this:

for x in "${arrDirs[@]}"; do
	 if [ -d "$x" ]; then

where you'd just be checking if the value of x (the nth variable in the arrDirs array) exists and is a directory.

let me know if I'm way off ;)

Best wishes,

Mike

That works! Except when i know there is nothing in the directories that I have emptied, or it doesn't exist, its not echoing...

Hey again :)

With that revised code, change this bottom chunk, also, from:

elif [ $x = 0 ]; then 
    	   echo "nothing to empty!"

to

else 
    	   echo "nothing to empty!"

and you should be all set :)

Best wishes,

Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.