DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   too many arguments (http://www.daniweb.com/forums/thread159653.html)

krammer Nov 26th, 2008 10:44 pm
too many arguments
 
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!

eggi Nov 27th, 2008 12:03 am
Re: too many arguments
 
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

krammer Dec 5th, 2008 7:54 pm
Re: too many arguments
 
Sorry for a delayed response...should the else conditions take care of that?

krammer Dec 5th, 2008 9:54 pm
Re: too many arguments
 
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.

eggi Dec 5th, 2008 10:58 pm
Re: too many arguments
 
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

krammer Dec 6th, 2008 8:49 am
Re: too many arguments
 
That works! Except when i know there is nothing in the directories that I have emptied, or it doesn't exist, its not echoing...

eggi Dec 6th, 2008 11:54 pm
Re: too many arguments
 
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


All times are GMT -4. The time now is 2:20 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC