On Thursday, Nov 27, 2003, at 10:04 US/Pacific, Marcus Claesson wrote:


Hi!

I have a problem with variables when using command-line perl in a bash
script. The script should update a date (in 2003-10-10 form) if the
argument, which is a file name, exists on the same line in the file
updated_files.txt.

#!/bin/bash
FILENAME=$1
UPDATED=`date +%F`
echo
echo "perl -wne 'if (/$FILENAME/) { s/\d{4}-\d{2}-\d{2}/$UPDATED/;print;
} ' updated_files.txt" #Exactly as below, to see how the command looks
like


perl -wne 'if (/$FILENAME/) { s/\d{4}-\d{2}-\d{2}/$UPDATED/;print; } '
updated_files.txt

exit

You will forgive me if I ask the silly starting question here, but why write a bash script to wrap a perl piece? You might want to start with getting in touch with

Getopt::Long

if all you are doing in the bash script is getting command line options.
but let us move along here with the minor problem of
shell interpolation.

First off you are getting warnings because of the '-w' flag.
hence it is noticing that you only used the variable once.



[EMAIL PROTECTED] Scripts]$ my_bash_script file1
perl -wne 'if (/file1/) { s/\d{4}-\d{2}-\d{2}/2003-11-27/;print; } else
{ print; }' updated_files.txt


Name "main::UPDATED" used only once: possible typo at -e line 1.
Name "main::FILE" used only once: possible typo at -e line 1.

Use of uninitialized value in regexp compilation at -e line 1, <> line
1.
[..]

THe problem is that the script is doing the 'interpretation'
of the variables to CREATE that 'echo' output, but the
$FILENAME is not getting expanding in the script
at the line where you wnat to execute it, because it
is INSIDE of the single tick mark.

But when I run what was shown in the "echo line" it works just perfectly
on the command-line. It looks like the bash variables won't follow into
the 'perl -ne' command, hence the 'Use of uninitialized value in
substitution' messages.

Old Dogs are still your Friend, had to bring up my /bin/sh head, so how about

#!/bin/bash
FILENAME=$1
UPDATED=`date +%F`
echo
CMD='if (/'$FILENAME'/) { s/\d{4}-\d{2}-\d{2}/'$UPDATED/';print; } '
echo "Have Command: $CMD"

perl -wne "$CMD" updated_files.txt
exit

Boy does that make ME Nervous...

ciao
drieux

---


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to