Ash script using "-l" (L) for argument (SOLVED)

Using applications, configuring, problems
Post Reply
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

Ash script using "-l" (L) for argument (SOLVED)

#1 Post by sunburnt »

A utility I wrote to add & remove host sections from the file "dhcpd.conf" looks like:

Code: Select all

if [ "$1" = "-at" -o "$1" = "-rt" ];then	### Test Mode
 echo ""
 echo "$newDHCP"
elif [ "$1" = "-a" -o "$1" = "-r" ];then	### Run Mode
 echo ""
 echo " The dhcpd.conf file has been modified."
 echo "$newDHCP" > $pathDHCP
elif [ "$1" = "-l" ];then			### List Hosts
 i=11
 while :
 do
  MAC=`sed -n "/host PUP-$i/{n;p;}" /etc/dhcp3/dhcpd.conf`
  if [ -z "$MAC" ];then
   break
  else
   echo "PUP-$i     "`echo $MAC | cut -d " " -f 3 | cut -d ";" -f 1`
  fi
  i=`expr $i + 1`
 done
else						### Error
 echo ""
 echo " ERROR: Input error!"
fi
There's more code above this that makes a new string $newDHCP to write to the file.
The second elif statement "elif [ "$1" = "-l" ];then" has "-l" that won't work.

I tried changing it to "-d" & it works just fine!
In using "-l" as an argument to a Bash script, have I uncovered some kind of gotcha?
Last edited by sunburnt on Fri 03 Nov 2006, 05:27, edited 2 times in total.

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#2 Post by GuestToo »

i can't think of any problems using -l ... other than the fact that it's hard to tell an l from a 1

#!/bin/sh
[ "$1" = "-l" ] && echo 'true'
[ "$1" = "-l" ] || echo 'false'

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

I always think of this stuff after I post... I guess I should have posted the error (duh).

Code: Select all

# ./dhcp-util -l
[: -h: binary operator expected
[: -a: binary operator expected
[: -r: binary operator expected
[: -at: binary operator expected
[: -a: binary operator expected
[: -d: binary operator expected

 ERROR: Input error!
The lines are showing the sequence of if statement arguments:
-h = help, -a = add host, -r = remove host, ect.

If the command is "dhcp-util -l, then it goes to the error.
The errors output above only happen with the "-l" argument.
I've made sure it's an "l" & not a "1", but it just isn't recognized!

Here's my test run of GuestToo's suggested script!

Code: Select all

# ./test -l     
[: -l: binary operator expected
[: -l: binary operator expected
false
I think it's time for me to reboot my PC... Puppy starts acting wonky sometimes after I've been testing lots of bash scripts that I've written...

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#4 Post by sunburnt »

Just rebooted & it didn't change a thing!

SOLVED: On a lark I changed the shbang from "#!/bin/sh" to "#!/bin/bash".

Now it works, so why won't ASH handle a "-l" argument, & BASH will?

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#5 Post by GuestToo »

/bin/sh should be bash ... just running under a different name ... /bin/ash should be Busybox's ash

bash does behave a little differently when it is running as "sh" ... for one thing, bash executes /root/.bashrc when the shell starts ... sh does not, it executes the file in ENV, if ENV is set

on my machine (Puppy 212b), that test script works as expected, and i have SHELL=/bin/sh in profile.local

#!/bin/sh
[ "$1" = "-l" ] && echo 'true'
[ "$1" = "-l" ] || echo 'false'

# /tmp/zz
false
# /tmp/zz -L
false
# /tmp/zz -xyz
false
# /tmp/zz -l
true

GuestToo
Puppy Master
Posts: 4083
Joined: Wed 04 May 2005, 18:11

#6 Post by GuestToo »

oh, one thing i just noticed ... be careful of naming scripts "test" ... there is already a utility program called "test", and you sometimes can get strange results if you run a script named "test" ... it might be better to name it foo or something

[ is just a symlink to test ... so my program might be written:

#!/bin/sh
test "$1" = "-l" && echo 'true'
test "$1" = "-l" || echo 'false'

Puppy has a utility program called test, and [ is a symlink to test, but bash may not execute those external programs when you use "test" ... bash running as bash and bash running as sh may execute the internal and external tests differently

for example, bash has an internal test [[ ... it is similar to [, but more efficient because it's internal ... or something like that, it's in the bash docs ... i rarely use [[ because i try to make my scripts compatible with Busybox's ash (i probably shouldn't bother)

User avatar
Dougal
Posts: 2502
Joined: Wed 19 Oct 2005, 13:06
Location: Hell more grotesque than any medieval woodcut

#7 Post by Dougal »

Sunburnt: you might be better off using a case structure here... might save little errors in the tests, too.
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post by sunburnt »

Yep... I almost always forget to say I'm still working with Puppy 1.

GuestToo; Thanks for the point about "test", I've noticed both of them that you mentioned but didn't think about them in this regard!
The file name is dhcp-util, but I did use test for your example.
I frequently have to watch the ASH-BASH thing as I work with Puppy's boot scripts alot, & as in this case, I make alot of utilities also.

Dougal; Yep, your right... I getting better at seeking alternative ways to do jobs when things go horribly wrong.
Thanks to you, GuestToo, MU, TedDog, & many others I can now write good Bash code the first time (usually).

User avatar
Dougal
Posts: 2502
Joined: Wed 19 Oct 2005, 13:06
Location: Hell more grotesque than any medieval woodcut

#9 Post by Dougal »

sunburnt wrote:I getting better at seeking alternative ways to do jobs when things go horribly wrong.
If I had do write commandline options parsing script I'd probably choose the lazy way: open the ABS-Guide and copy it from one of the examples :)
What's the ugliest part of your body?
Some say your nose
Some say your toes
But I think it's your mind

Post Reply