[: too many arguments [SOLVED]

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

[: too many arguments [SOLVED]

#1 Post by Argolance »

Bonjour,
Sometimes, but not always :shock: , xerrs.log returns:
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: Select all

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, 12:44, edited 1 time in total.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

[: too many arguments

#2 Post by L18L »

Bonjour Argolance,
I have been trying to understand how to help you but no success so far :oops:
Please post a concrete example of
error wrote: : [: too many arguments
Cordialment :wink:

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#3 Post by Argolance »

Hello L18L,
Thank you for replying.
I solved this issue by writing:

Code: Select all

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! :shock:

Code: Select all

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! :D
EDIT: ... But the first conditional lines doesn't work properly anymore! :oops:

Cordialement.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#4 Post by L18L »

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
#

:arrow: Never ignore the error message, result is wrong if error :idea:
:D

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#5 Post by Karl Godt »

Code: Select all

bash-3.00# [[ -f /usr/share/applications/*.desktop ]] && echo Y || echo N
N
Then use

Code: Select all

[ "`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: Select all

function_test(){
[ "$@" ] && P1="$@"
[ "$*" ] && P2="$@"
[ "$*" ] && P3="$*"
echo "P1='$P1'"
echo "P2='$P2'"
echo "P3='$P3'"
}
function_test -d/dev/sdz99 --quiet "/path / to -f /filename"

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#6 Post by Argolance »

Thanks to both of you!

Cordialement.
Attachments
144532_582x183_easyshot.jpg
(22.72 KiB) Downloaded 376 times

Bruce B

#7 Post by Bruce B »

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: Select all

[ "`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

Ibidem
Posts: 549
Joined: Wed 26 May 2010, 03:31
Location: State of Jefferson

#8 Post by Ibidem »

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: Select all

[ "`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.

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#9 Post by Karl Godt »

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 :P

Post Reply