| | |
too many arguments
![]() |
•
•
Join Date: Jun 2008
Posts: 26
Reputation:
Solved Threads: 0
Does anyone know why this script would give me an error (only sometimes) with it saying "too many arguments"?
Output:
Shell Scripting Syntax (Toggle Plain Text)
#!/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:
Shell Scripting Syntax (Toggle Plain Text)
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!
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Jun 2008
Posts: 26
Reputation:
Solved Threads: 0
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:
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.
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)
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.
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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:
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:
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
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)
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:
Shell Scripting Syntax (Toggle Plain Text)
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
Hey again 
With that revised code, change this bottom chunk, also, from:
to
and you should be all set
Best wishes,
Mike

With that revised code, change this bottom chunk, also, from:
Shell Scripting Syntax (Toggle Plain Text)
elif [ $x = 0 ]; then echo "nothing to empty!"
to
Shell Scripting Syntax (Toggle Plain Text)
else 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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Similar Threads
- Command Line Arguments (C#)
- command line arguments (C++)
- Default values for STL container function arguments? (C++)
- passing command line arguments (C)
- Arguments of a function (JavaScript / DHTML / AJAX)
- Command-line Arguments. (C++)
- Batch file that takes arguments (C)
Other Threads in the Shell Scripting Forum
- Previous Thread: IPV6
- Next Thread: resetting an array
| Thread Tools | Search this Thread |





