shell help: check for regex in variable

Sergey Semenyuk ssemenyuk-5xk6gukWl5lWk0Htik3J/w at public.gmane.org
Mon Mar 12 20:16:37 UTC 2007


Hi Giles,

 really quick one from the top of my head:

if  [ -z "`echo $pidfile | egrep -e '\.pid$'`" ]; then
  doesn't have pid at the end

......

Sergey

Giles Orr wrote:
> 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.
>
--
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