[TriLUG] bash scripting and regex help
Mitchell Amiano
mamiano at nc.rr.com
Wed Apr 25 11:25:52 EDT 2012
Or use variable substring removal:
f=${filename#version_}
CMD=${f%.jar}
Two steps, but they are both built-ins, and avoids the quotes as well as the process that expr will fork.
On Apr 25, 2012, at 10:52 AM, Chris Merrill wrote:
> I'm trying to script a process and part of it needs to extract a version
> number from a filename. I can do this from the command line easily enough...
> here is a simplified example:
>
>> [root at dawn scripts]# expr match "version_5.1.10577.jar" 'version_\(.*\)\.jar'
>> 5.1.10577
>
> The output is exactly what I need. But when I try to do the same thing inside
> a script, I get nothing. This is the script:
>
> #!/bin/bash
> #
> filename=version_5.1.10577.jar
> expression="version_\(.*\)\.jar"
> CMD="expr match \"$filename\" '${expression}'"
> echo $CMD
> $CMD
>
> and this is the output:
>
>> [root at dawn scripts]# ./test.sh
>> expr match "version_5.1.10577.jar" 'version_\(.*\)\.jar'
>>
>> [root at dawn scripts]#
>
>
> I've tried a lot of variations, but none of them get me anything that resembles
> success, so I think I must be missing something fundamental. Can anyone point
> me in the right direction?
>
> TIA!
> Chris
>
>
> --
> ------------------------------------------------------------------------ -
> Chris Merrill | Web Performance, Inc.
> chris at webperformance.com | http://webperformance.com
> 919-433-1762 | 919-845-7601
>
> Web Performance: Website Load Testing Software & Services
> ------------------------------------------------------------------------ -
> --
> This message was sent to: Mitch Amiano <mamiano at nc.rr.com>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web : http://www.trilug.org/mailman/options/trilug/mamiano%40nc.rr.com
> TriLUG FAQ : http://www.trilug.org/wiki/Frequently_Asked_Questions
Mitchell Amiano
(919) 410-8008
mamiano at nc.rr.com
More information about the TriLUG
mailing list