1

I'd like help with calling a perl script from within the while loop of a bash script. An extract of my script is:

touch $OUTPUT_DIR/$OUTPUT_XML_PARSE

while read -r LINE; do
   perl newscript.pl "$LINE" >> $OUTPUT_DIR/$OUTPUT_XML_PARSE
done < $OUTPUT_DIR/$OUTPUT_FILENAME_LIST 

The file $OUTPUT_FILENAME_LIST contains a list of filenames each of which the Perl script uses as input argument to open and modify the data and writes the output to $OUTPUT_XML_PARSE.

The error when i run the program is "Can't open perl script "IDOCXML_parse.pl": A file or directory in the path name does not exist.". The perl script is in the same directory as the bash script, so I'm not sure why this is the case. Please advise.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Kaladin
  • 11
  • 3
  • 3
    Get out of the habit of using ALL_CAPS_VARNAMES. Someday you'll use `PATH=...` and break your script. – glenn jackman Apr 17 '18 at 16:01
  • 1
    To learn more about shell variable naming standards, see [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/6862601). – codeforester Apr 17 '18 at 17:19

1 Answers1

1

It sounds like you are invoking your script like /path/to/myscript.sh from some directory other than /path/to.

Try

perl "$(dirname "$0")/IDOCXML_parse.pl" ...

or

cd "$(dirname "$0")"
while read ...; do perl ./IDOCXML_parse.pl ...

Also, bash while-read loops are really slow. Try this

xargs -I X -L 1 perl ./IDOCXML_parse.pl X < input.file > output.file

or rewrite the perl script to accept the input file-of-filenames and let perl iterate over it.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thanks Glenn, I tried your code that does not involve the while loop, that resulted in the error - "A file or directory in the path name does not exist. at /home/kal/IDOCXML_parse.pl line 22 at /home/kal/IDOCXML_parse.pl line 22", and line 22 is `$twig->parsefile( $ARGV[0] );` . How can i change your statement in a way that the filename is passed as an **argument** to the perl script? – Kaladin Apr 17 '18 at 17:49
  • Apparently the filenames in the input file do not have absolute paths. Fix that if you can. Otherwise you have to `cd` to the directory containing those files and call the perl script using an absolute path – glenn jackman Apr 17 '18 at 17:58