too many arguments

Reply

Join Date: Jun 2008
Posts: 26
Reputation: krammer is an unknown quantity at this point 
Solved Threads: 0
krammer krammer is offline Offline
Light Poster

too many arguments

 
0
  #1
Nov 26th, 2008
Does anyone know why this script would give me an error (only sometimes) with it saying "too many arguments"?

Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. HOME='/home/eric'
  4.  
  5. arrFiles=("$HOME/.kde/share/apps/kaffeine/playlists/NEW.kaffeine"\
  6. "$HOME/.kde/share/apps/kcookiejar/cookies"\
  7. "$HOME/.kde4/share/apps/kcookiejar/cookies")
  8. arrDirs=("$HOME/.kde4/share/apps/RecentDocuments/*"\
  9. "$HOME/.kde/share/apps/RecentDocuments/*"\
  10. "$HOME/.macromedia/Flash_Player/#SharedObjects/*")
  11.  
  12. case "$1" in
  13.  
  14. trash)
  15. rm -r $HOME/.local/share/Trash/*
  16. echo Trash emptied
  17. ;;
  18.  
  19. history)
  20. #Clears Files
  21. for x in "${arrFiles[@]}"; do
  22. if [ $x = "$x" ]; then
  23. rm -rf $x
  24. else
  25. echo "nothing to empty!"
  26. fi
  27. done
  28.  
  29. #Clears Dirs
  30. for x in "${arrDirs[@]}"; do
  31. if [ $x = "$x" ]; then
  32. echo "nothing to empty!"
  33. else
  34. rm -rf $x
  35. fi
  36. done
  37. ;;
  38.  
  39. all)
  40. for x in "${arrFiles[@]}"; do
  41. if [ $x = "$x" ]; then
  42. rm -rf $x
  43. else
  44. echo "nothing to empty!"
  45. fi
  46. done
  47.  
  48. #Clears Dirs
  49. for x in "${arrDirs[@]}"; do
  50. if [ $x = "$x" ]; then
  51. echo "nothing to empty!"
  52. else
  53. rm -rf $x
  54. fi
  55. done
  56.  
  57. rm -r $HOME/.local/share/Trash/*
  58. ;;
  59.  
  60. esac


Output:

Shell Scripting Syntax (Toggle Plain Text)
  1. root@siduxbox:/home/eric# empty trash
  2. Trash emptied
  3. root@siduxbox:/home/eric# empty history
  4. /usr/bin/empty: line 31: [: too many arguments
  5. nothing to empty!
  6. root@siduxbox:/home/eric# empty history
  7. nothing to empty!
  8. nothing to empty!
  9. nothing to empty!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: too many arguments

 
0
  #2
Nov 27th, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 26
Reputation: krammer is an unknown quantity at this point 
Solved Threads: 0
krammer krammer is offline Offline
Light Poster

Re: too many arguments

 
0
  #3
Dec 5th, 2008
Sorry for a delayed response...should the else conditions take care of that?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 26
Reputation: krammer is an unknown quantity at this point 
Solved Threads: 0
krammer krammer is offline Offline
Light Poster

Re: too many arguments

 
0
  #4
Dec 5th, 2008
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:

Shell Scripting Syntax (Toggle Plain Text)
  1. for x in "${arrDirs[@]}"; do
  2. if [ $x = "$x" ]; then
  3. rm -rf $x
  4. elif [ $x = 0 ]; then
  5. echo "nothing to empty!"
  6. fi
  7. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: too many arguments

 
0
  #5
Dec 5th, 2008
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:

Shell Scripting Syntax (Toggle Plain Text)
  1. for x in "${arrDirs[@]}"; do
  2. 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:

Shell Scripting Syntax (Toggle Plain Text)
  1. for x in "${arrDirs[@]}"; do
  2. 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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 26
Reputation: krammer is an unknown quantity at this point 
Solved Threads: 0
krammer krammer is offline Offline
Light Poster

Re: too many arguments

 
0
  #6
Dec 6th, 2008
That works! Except when i know there is nothing in the directories that I have emptied, or it doesn't exist, its not echoing...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: too many arguments

 
0
  #7
Dec 6th, 2008
Hey again

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

Shell Scripting Syntax (Toggle Plain Text)
  1. elif [ $x = 0 ]; then
  2. echo "nothing to empty!"

to

Shell Scripting Syntax (Toggle Plain Text)
  1. else
  2. echo "nothing to empty!"

and you should be all set

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC