shell help: check for regex in variable

Giles Orr gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Mon Mar 12 17:57:55 UTC 2007


On 3/12/07, Neil Watson <tlug-neil-8agRmHhQ+n2CxnSzwYWP7Q at public.gmane.org> wrote:
> How can I check that a variable ends in .pid?
>
> loose example
> pidfile="/var/run/myproc.pid"
>
> if [[ $pidfile contains ".*\.pid$" ]]
>
> Is this possible?

If using bash, how about:

if [ ${pidfile} = ${pidfile%.pid} ]
then
   # the filename does NOT have .pid on the end.
fi

${pidfile%.pid} strips ".pid" off the end if it exists.  If it doesn't
exist, the variable is unchanged.  I wouldn't know how to do this in
sh, sorry.

-- 
Giles
http://www.gilesorr.com/
gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list