Link to home
Start Free TrialLog in
Avatar of coanda
coanda

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Superdave
Superdave
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
Tintin

And to give you the non-perl answer


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

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In fact, you can simplify it further
if [ $(pgrep -c urxvt) -le 2 ]
then
   ...
fi

Open in new window