Passing bash array to perl

I have a shell script that checks how many instances of an application that are open and I would like to check the length of it to decide what to do next, currently I'm doing:

LIST=`pidof urxvt`
CNT=`perl -e '@ret=qw($LIST); print scalar @ret;'`
if [ "$CNT" -le 2 ]; then
    ...
fi

The problem is that the $CNT always = 1, it's pretty obvious that I'm not using the correct syntax to pass $LIST into the line of perl but I can't seem to find how to do it correctly. There's likely other ways of doing this than using perl but I'm more interested in how to do using perl than I am by using awk, python, or any of the numerous other options.

Thanks in advance.
LVL 3
coandaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SuperdaveConnect With a Mentor Commented:
$LIST is not being expanded when included in your perl script because it's inside single quotes.  Switch the single quotes to double-quotes.
0
 
TintinCommented:
And to give you the non-perl answer


CNT=$(pidof urxvt | wc -l)
if [ $CNT -le 2 ]; then
    ...
fi

Open in new window

0
 
TintinConnect With a Mentor Commented:
Whoops, I meant to write
CNT=$(pgrep urxvt | wc -l)
if [ $CNT -le 2 ]; then
    ...
fi

Open in new window

0
 
TintinCommented:
In fact, you can simplify it further
if [ $(pgrep -c urxvt) -le 2 ]
then
   ...
fi

Open in new window

0
All Courses

From novice to tech pro — start learning today.