From: Richard Kettlewell on
Folderol <folderol(a)ukfsn.org> writes:

> The script below works perfectly in bash, but if I try to run it on a
> system with dash I get the following error:
>
> Start_Midisport.sh: 21: [[: not found
>
> Can anyone suggest what I should do to get it to work equally well in
> either shell?

Use grep to filter the output of lsusb instead of comparing each line
'by hand'.

> #!/bin/bash
>
> # echo's can be removed -just for testing
>
> name="MidiSport"
> lsusb | cat | while read line
> do
> if [[ "$line" =~ "$name" ]]
> then
> echo $line
> result=${line#'Bus '}
> busnumber=${result%' Device'*}
> result=${result#*'Device '}
> devicenumber=${result%': ID'*}
> echo $busnumber
> echo $devicenumber
> result="sudo fxload
> -I /usr/local/share/usb/maudio/MidiSport2x2.ihx -D /dev/bus/usb/"$busnumber"/"$devicenumber
> echo $result
> exec $result
> fi
> done
> echo

--
http://www.greenend.org.uk/rjk/
From: Mark Hobley on
unruh <unruh(a)wormhole.physics.ubc.ca> wrote:
> [[ is an internal command in bash. I guess it is not in dash.
> complain to the writers of dash.

If you do find a conversion for this, please follow up here, or update the
wiki page, because I am researching conversions of bashisms into portable
shell script syntax, and I currently have no solution for this.

http://markhobley.yi.org:9012/doublebox

Regards,

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

From: unruh on
On 2010-03-12, Richard Kettlewell <rjk(a)greenend.org.uk> wrote:
> Folderol <folderol(a)ukfsn.org> writes:
>
>> The script below works perfectly in bash, but if I try to run it on a
>> system with dash I get the following error:
>>
>> Start_Midisport.sh: 21: [[: not found
>>
>> Can anyone suggest what I should do to get it to work equally well in
>> either shell?
>
> Use grep to filter the output of lsusb instead of comparing each line
> 'by hand'.
>
>> #!/bin/bash
>>
>> # echo's can be removed -just for testing
>>
>> name="MidiSport"
>> lsusb | cat | while read line
>> do
>> if [[ "$line" =~ "$name" ]]

There is a program called test, or [ in /usr/bin which will give a 0 or
1 as the result of a test Thus

if ! [ "$line" = "$name" ]; then
would do what you seem to want, although I do not know what =~ means.
(mine will do the commands if $line as a string is not identical to
"$name". If =~ means "included in" or somethng like that, then doing it
with egrep could work

if ` echo $name|egrep -n "$line" >/dev/null 2>&1` ;then
or something like that.

>> then
>> echo $line
>> result=${line#'Bus '}
>> busnumber=${result%' Device'*}
>> result=${result#*'Device '}
>> devicenumber=${result%': ID'*}
>> echo $busnumber
>> echo $devicenumber
>> result="sudo fxload
>> -I /usr/local/share/usb/maudio/MidiSport2x2.ihx -D /dev/bus/usb/"$busnumber"/"$devicenumber
>> echo $result
>> exec $result
>> fi
>> done
>> echo
>
From: Tony Houghton on
In <slrnhpl33m.8eo.unruh(a)wormhole.physics.ubc.ca>,
unruh <unruh(a)wormhole.physics.ubc.ca> wrote:

> On 2010-03-12, Richard Kettlewell <rjk(a)greenend.org.uk> wrote:
>> Folderol <folderol(a)ukfsn.org> writes:
>>
>>> The script below works perfectly in bash, but if I try to run it on a
>>> system with dash I get the following error:
>>>
>>> Start_Midisport.sh: 21: [[: not found
>>>
>>> Can anyone suggest what I should do to get it to work equally well in
>>> either shell?
>>
>> Use grep to filter the output of lsusb instead of comparing each line
>> 'by hand'.

I agree. grep makes it simpler as well as not relying on bash. There may
be other ways that don't rely on either, but they'd be more complicated.

>>> #!/bin/bash

If you want to get rid of the bashisms, shouldn't it be /bin/sh?

>>> # echo's can be removed -just for testing
>>>
>>> name="MidiSport"
>>> lsusb | cat | while read line
>>> do
>>> if [[ "$line" =~ "$name" ]]
>
> There is a program called test, or [ in /usr/bin which will give a 0 or
> 1 as the result of a test Thus
>
> if ! [ "$line" = "$name" ]; then
> would do what you seem to want, although I do not know what =~ means.

=~ means the right-hand expression is an extended regex. It doesn't look
like it needs to be extended in this case, so grep should suffice
instead of egrep.

> (mine will do the commands if $line as a string is not identical to
> "$name". If =~ means "included in" or somethng like that, then doing it
> with egrep could work
>
> if ` echo $name|egrep -n "$line" >/dev/null 2>&1` ;then
> or something like that.

I don't think you need the backticks, and I don't think you should
include -n.

#!/bin/bash

# echo's can be removed -just for testing

name="MidiSport"
lsusb | grep "$name" 2>/dev/null | while read line
do
echo $line
result=${line#'Bus '}
busnumber=${result%' Device'*}
result=${result#*'Device '}
devicenumber=${result%': ID'*}
echo $busnumber
echo $devicenumber
result="sudo fxload
-I /usr/local/share/usb/maudio/MidiSport2x2.ihx -D /dev/bus/usb/"$busnumber"/"$devicenumber
echo $result
exec $result
done
echo

--
TH * http://www.realh.co.uk
From: Folderol on
On Fri, 12 Mar 2010 20:12:36 +0000
Paul Martin <pm(a)nowster.org.uk> wrote:

> In article <20100312102617.0341f268(a)debian>,
> Folderol wrote:
> > The script below works perfectly in bash, but if I try to run it on a
> > system with dash I get the following error:
>
> > Start_Midisport.sh: 21: [[: not found
>
> > Can anyone suggest what I should do to get it to work equally well in
> > either shell?
>
>
> > #!/bin/bash
>
> > # echo's can be removed -just for testing
>
> > name="MidiSport"
> > lsusb | cat | while read line
> > do
> > if [[ "$line" =~ "$name" ]]
> > then
> > echo $line
> > result=${line#'Bus '}
> > busnumber=${result%' Device'*}
> > result=${result#*'Device '}
> > devicenumber=${result%': ID'*}
> > echo $busnumber
> > echo $devicenumber
> > result="sudo fxload
> > -I /usr/local/share/usb/maudio/MidiSport2x2.ihx -D /dev/bus/usb/"$busnumber"/"$devicenumber
> > echo $result
> > exec $result
> > fi
> > done
> > echo
>
> #!/bin/sh
>
> lsusb | grep MidiSport | while read line
> do
> echo $line
> result=${line#'Bus '}
> busnumber=${result%' Device'*}
> result=${result#*'Device '}
> devicenumber=${result%': ID'*}
> echo $busnumber
> echo $devicenumber
> result="sudo fxload -I /usr/local/share/usb/maudio/MidiSport2x2.ihx -D /dev/bus/usb/"$busnumber"/"$devicenumber
> echo $result
> exec $result
> done
> echo


Thank you everyone for going to all this trouble. You get so used to
seeing bash scripts you forget to consider it is bash that may not
be compliant rather than another program.

Both this and unruh's version work perfectly with both dash & bash. I
do like the simplicity of this one too!


--
Will J G