How do I best pass arguments to a Perl one-liner?

Viewed 20302

I have a file, someFile, like this:

$cat someFile
hdisk1 active
hdisk2 active

I use this shell script to check:

$cat a.sh
#!/usr/bin/ksh
for d in 1 2
do
    grep -q "hdisk$d" someFile && echo "$d : ok"
done

I am trying to convert it to Perl:

$cat b.sh
#!/usr/bin/ksh
export d
for d in 1 2
do
    cat someFile | perl -lane 'BEGIN{$d=$ENV{'d'};} print "$d: OK" if /hdisk$d\s+/'
done

I export the variable d in the shell script and get the value using %ENV in Perl. Is there a better way of passing this value to the Perl one-liner?

8 Answers

It's already written on the top in one long paragraph but I am also writing for lazy developers who don't read those lines.

Double quotes and single quote has big different meaning for the bash.

So please take care

Doesn't WORK perl '$VAR' $FILEPATH

WORKS perl "$VAR" $FILEPATH

Related