Re: Cannot form valid test expressions that involve brackets as string comparison targets

2009-10-08 Thread Andreas Schwab
Lyall Pearce  writes:

> Repeat-By:
>
> basePic="(2008-04)"
> if [ "${basePic:0:1}" = '(' -a "${basePic:4:1}" = ')' ]
> then
> echo "Got brackets"
> fi
  case $basePic in "("???")"*) echo "Got brackets";; esac

> Fix:
> Unsure, I think the bracket parsing should not be treated as
> expression delimiters if they are enclosed in quotes?

The quotes are irrelevant for the test builtin, they are already eaten
by the parser.

> If they are bare, then treat as (brackets), if enclosed in quotes,
> treat as "data".

Bare parens would be a syntax error, since they are part of the shell
grammar.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




Re: Cannot form valid test expressions that involve brackets as string comparison targets

2009-10-08 Thread Greg Wooledge
On Wed, Oct 07, 2009 at 08:07:19PM +1030, Lyall Pearce wrote:
> Description:
> Cannot form expressions which involve left or right brackets.

Parentheses?

> Repeat-By:
> 
> basePic="(2008-04)"
> if [ "${basePic:0:1}" = '(' -a "${basePic:4:1}" = ')' ]
> then
> echo "Got brackets"
> fi

  if [ "${basePic:0:1}" = "(" ] && [ "${basePic:(-1):1}" = ")" ]; then
echo "Got parentheses"
  fi

When using [ instead of [[, I would strongly recommend never using -a
or -o inside it.  POSIX has strict rules about exactly how to treat
a [ command with a specific number of arguments, and often the most
common cases are technically illegal.  The workaround is to use multiple
[ commands strung together with && and || as needed.

Or, the other workaround would be to use [[, since you're already using
${string:start:length} which is non-POSIX syntax.

  if [[ "${basePic:0:1}" = "(" && "${basePic:(-1):1}" = ")" ]]; then
echo "Got parentheses"
  fi

Both of those worked for me in bash 4.0.10.




Re: ignoring comments in a 'cat' call

2009-10-08 Thread Bob Proulx
Tamlyn1978 wrote:
> I have a file with a list of programs with comments describing what they do,
> e.g.
> 
> unison # file syncronisation
> grass # gis program
> 
> I have a script, which I run as root and includes the command:
> 
> m...@me:~$ aptitude install $(cat programs) -y
> 
> where 'programs' is the file with the list of programs. Aptitude does not
> ignore the '#' comments, reading them, and the subsequent comment text as
> package files.
> 
> Is there a way to make 'cat' ignore the comments or is there a better
> alternative to cat in this case?

Note that if 'cat' didn't exactly reproduce the contents of input on
the output I would consider that a grave bug.

Instead of using $(cat SOMEFILE) it is better to avoid the extra
process and use $(< SOMEFILE) instead.  It is built into the shell and
is the right way to do that task.

Since you want to post process the file then you should write a script
to do the post processing.  If you want to remove the comments from
the file before using it then approach the problem as you would to do
exactly that task, and then enclose it in $(...).  To remove comments
as in this case I would reach for 'sed'.

  $ sed 's/#.*//' programs
  unison
  grass

Testing that by itself to ensure that does what you want then you can
use it in the command substitution.

  $ echo aptitude install $(sed 's/#.*//' programs)
  aptitude install unison grass

And then if you are sure it is creating the right command for you then
you can invoke it without the 'echo' and run it for effect.

  $ sudo aptitude install $(sed 's/#.*//' programs)
  Reading package lists... Done
  ...

(Of course on my systems I spell 'aptitude' as 'apt-get'.  But that is
a topic for a different mailing list. :-)

Bob