On Tue, 15 Apr 2008, Denys Vlasenko wrote:
> On Tuesday 15 April 2008 20:32, Cristian Ionescu-Idbohrn wrote:
> > On Tue, 15 Apr 2008, James Simmons wrote:
> >
> > > This fails.
> > >
> > > runlevel=$(cat /proc/1/cmdline 2> /dev/null)
> > > echo $runlevel # init [2]
> > > runlevel=${runlevel#* [}
> > >
> > > echo "runlevel '$runlevel'" # Should be runlevel
> > > '2]'
> > > # we get runlevel 'init [2]'
> >
> > Hmm...
> >
> > But:
> >
> > # runlevel=${runlevel#* \[}
> > # echo $runlevel
> > 2]
>
> Well, we are trying to emulate bash here, and so far it does not fully work:
>
> # ./busybox ash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#*
> [}'
> init [2]
> init [2]
> # bash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#* [}'
> init [2]
> 2]
>
> # ./busybox ash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#*
> \[}'
> init [2]
> 2]
> # bash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#* \[}'
> init [2]
> 2]
>
>
> The reason seems to be that fnmatch() doesn't threat that [ as a literal:
>
> # ./busybox ash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#*
> [}'
> init [2]
> ash: fnmatch('* [','',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','i',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','in',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','ini',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','init',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','init ',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','init [',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','init [2',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* [','init [2]',0)=1 (FNM_NOMATCH=1)
> init [2]
> # ./busybox ash -c "runlevel='init [2]';"' echo $runlevel; echo ${runlevel#*
> \[}'
> init [2]
> ash: fnmatch('* \[','',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* \[','i',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* \[','in',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* \[','ini',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* \[','init',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* \[','init ',0)=1 (FNM_NOMATCH=1)
> ash: fnmatch('* \[','init [',0)=0 (FNM_NOMATCH=1)
> 2]
So, what's the conclusion on this? Does anything to be done?
Just to add to the confusion:
# bash -c "r='init [2]';"' echo $r; echo ${r#* [}'
init [2]
2]
# bash -c "r='init [2]';"' echo $r; echo ${r#* \[}'
init [2]
2]
# zsh -c "r='init [2]';"' echo $r; echo ${r#* [}'
init [2]
zsh:1: bad pattern: * [
# zsh -c "r='init [2]';"' echo $r; echo ${r#* \[}'
init [2]
2]
# dash -c "r='init [2]';"' echo $r; echo ${r#* [}'
init [2]
init [2]
# dash -c "r='init [2]';"' echo $r; echo ${r#* \[}'
init [2]
2]
# ash -c "r='init [2]';"' echo $r; echo ${r#* [}'
init [2]
init [2]
# ash -c "r='init [2]';"' echo $r; echo ${r#* \[}'
init [2]
2]
Looks like '[' is special and (easy way out) needs to be escaped.
Is bash at fault?
Cheers,
--
Cristian
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox