Author |
Message |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Thu 18 Apr 2013, 04:56 Post subject:
[: too many arguments [SOLVED] |
|
Bonjour,
Sometimes, but not always , xerrs.log returns: Quote: | myscript.sh: line xx: [: too many arguments |
... in this case for example, where the script verify if /root/Desktop directory exists or not and create it if not:
Code: | if [ ! -d /root/Desktop ]; then
mkdir /root/Desktop
Xdialog --title "xxxx" --icon /usr/local/lib/X11/pixmaps/question.png --msgbox "xxxxx" 0 0
fi |
This works but I would like to understand what's wrong or seems to be so.
Thank you!
Cordialement.
Last edited by Argolance on Thu 18 Apr 2013, 08:44; edited 1 time in total
|
Back to top
|
|
 |
L18L
Joined: 19 Jun 2010 Posts: 3431 Location: www.eussenheim.de/
|
Posted: Thu 18 Apr 2013, 05:36 Post subject:
[: too many arguments Subject description: too less info |
|
Bonjour Argolance,
I have been trying to understand how to help you but no success so far
Please post a concrete example of
error wrote: | : [: too many arguments |
Cordialment
|
Back to top
|
|
 |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Thu 18 Apr 2013, 07:16 Post subject:
|
|
Hello L18L,
Thank you for replying.
I solved this issue by writing: Code: | if [[ ! -f /root/Desktop/*.desktop ]]; then | But this is quite strange indeed, because my script has other conditional lines that are exactly built the same way... and xerrs.log doesn't complain! Code: | if [ ! -d /root/Desktop ]; then
mkdir /root/Desktop
Xdialog --title "$(gettext 'Information')" --icon /usr/local/lib/X11/pixmaps/question.png --msgbox "$(gettext '"Desktop" directory, where wine copies
the desktop MENU entries files of its installed programs has been created!')" 0 0
else
echo "$(ls /root/Desktop/*.desktop)" > /root/test
fi
if [[ ! -f /root/Desktop/*.desktop ]]; then
Xdialog --title "$(gettext 'Information')" --icon /usr/local/lib/X11/pixmaps/info.png --msgbox "$(gettext '"Desktop" directory is empty and there are no desktop MENU entry file to modify/complete!')" 0 0
else
...
| And the best is that this can change as I am building my script....
At the beginning, xerrs.log said nothing, suddenly complained about the first conditional lines, then the second and now, writing double "[", it is mute and happy!
EDIT: ... But the first conditional lines doesn't work properly anymore!
Cordialement.
|
Back to top
|
|
 |
L18L
Joined: 19 Jun 2010 Posts: 3431 Location: www.eussenheim.de/
|
Posted: Thu 18 Apr 2013, 07:30 Post subject:
|
|
So it is about diffrence of [ and [[ (one or two [)
I have no .desktop in my /root/Desktop an run your
# if [[ ! -f /root/Desktop/*.desktop ]]; then echo no;else echo yes; fi
no
#
changing to one [ :
# if [ ! -f /root/Desktop/*.desktop ]; then echo no;else echo yes; fi
bash: [: too many arguments
yes
#
Never ignore the error message, result is wrong if error
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 4208 Location: Kiel,Germany
|
Posted: Thu 18 Apr 2013, 08:11 Post subject:
|
|
Code: | bash-3.00# [[ -f /usr/share/applications/*.desktop ]] && echo Y || echo N
N |
Then use
Code: | [ "`ls -1 /usr/share/applications/*.desktop`" ] && echo Y || echo N |
Note : If the variable may contain spaces, the "double quoting inside the single [ brackets are needed .
Note : I tend to quote them always, just in case - though it looks better in default geany .sh template without the double quotes.
Note : BK double quotes a lot of strings, where in 99% of the cases no double quotes are needed but tends to omit them many times in test lines .
Note : [ "$*" ] && ALL_PARAMETERS="$@"
Code: | function_test(){
[ "$@" ] && P1="$@"
[ "$*" ] && P2="$@"
[ "$*" ] && P3="$*"
echo "P1='$P1'"
echo "P2='$P2'"
echo "P3='$P3'"
}
function_test -d/dev/sdz99 --quiet "/path / to -f /filename" |
|
Back to top
|
|
 |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Thu 18 Apr 2013, 08:44 Post subject:
|
|
Thanks to both of you!
Cordialement.
Description |
|
Filesize |
22.72 KB |
Viewed |
362 Time(s) |

|
|
Back to top
|
|
 |
Bruce B
Joined: 18 May 2005 Posts: 11488 Location: The Peoples Republic of California
|
Posted: Thu 18 Apr 2013, 22:10 Post subject:
|
|
I wrote the snippet below. If there are any .txt files in the current directory, the return code is 0. If not the return code is 1
The 2>/dev/null hides the text error output if no .txt files are found.
Code: | [ "`ls -la *.txt 2>/dev/null`" ] ; echo $? |
Question: Maybe Karl or someone will explain to me why the quotes are necessary to make the [ test ] work properly. I mean, how or why do the quotes work?
TIA
_________________ New! Puppy Linux Links Page
|
Back to top
|
|
 |
Ibidem
Joined: 25 May 2010 Posts: 553 Location: State of Jefferson
|
Posted: Fri 19 Apr 2013, 01:45 Post subject:
|
|
Bruce B wrote: | I wrote the snippet below. If there are any .txt files in the current directory, the return code is 0. If not the return code is 1
The 2>/dev/null hides the text error output if no .txt files are found.
Code: | [ "`ls -la *.txt 2>/dev/null`" ] ; echo $? |
Question: Maybe Karl or someone will explain to me why the quotes are necessary to make the [ test ] work properly. I mean, how or why do the quotes work?
|
Because if there is more than one parameter, test ([) assumes that it's supposed to have an option. The double quotes make test see all the output as one parameter.
|
Back to top
|
|
 |
Karl Godt

Joined: 20 Jun 2010 Posts: 4208 Location: Kiel,Germany
|
Posted: Fri 19 Apr 2013, 05:54 Post subject:
|
|
And it seems not to be because of just spaces but for the common IFS FileSeparator since ls -1 would use newline . Will test things with other IFS set .
The common file separator is " \t\n" .
_________________ «Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal 
|
Back to top
|
|
 |
|