【问题标题】:Can't run a working shell script from inside Perl无法从 Perl 内部运行有效的 shell 脚本
【发布时间】:2016-05-01 16:50:59
【问题描述】:

我有一个 shell 脚本 makecsv.sh 可以单独运行,但在 Perl 中调用时会失败。

这是当我从 Perl 脚本运行 makecsv.sh 时它给出的消息:

fr call
Source file does not exist

Usage: fr -s <source_file> -o <output_file>
   -s <source_file> - Input file which has Counter Information
   -o <output_file> - Comma separated output file

third move
mv: cannot access /home/ateam/.../SAM5_01-05-  2016_11:07:29.csv
/home/ateam/.../SAM5_01-05-2016_11:07:29.xcm: No such   file or directory
cp: cannot access /home/ateam/.../SAMlog/ /home/ateam/.../SAM5_01-05-2016_11:07:29.csv
md5sum: /home/ateam/.../SAM5_01-05-2016_11:07:29.csv: No such file or directory
/home/ateam/.../SAM5_01-05-2016_11:07:29.csv: No such file or directory

这是包含fr 节点的目录列表

drwxrwxr-x   4 ateam    ateam       4096 May  1 11:12 ./
drwxr-xr-x   3 ateam    ateam       4096 Mar 17 11:18 ../
lrwxrwxrwx   1 ateam    ateam          7 Mar 12 19:13 c -> counter*
-rwxrwxr-x   1 ateam    ateam       1974 Mar 12 19:04 counter*
-rwxrwxr-x   1 ateam    ateam        134 Mar 21 11:22 e*
-rw-rw-r--   1 ateam    ateam    25152020 Mar 21 11:22 enb
-rw-rw-r--   1 ateam    ateam    1880872 Mar 21 11:21 enb283.xml
-rwxrwxr-x   1 ateam    ateam        811 Mar 28 11:59 expftp*
lrwxrwxrwx   1 ateam    ateam          7 Mar 17 11:23 fr -> fruinfo*
-rw-rw-r--   1 ateam    ateam      75753 Mar 22 13:07 fru_0322.xls
-rwxrwxr-x   1 ateam    ateam       2878 Apr 21 22:16 fruinfo*
drwxrwxr-x   2 ateam    ateam       4096 May  1 11:12 md5sum/
...

我不明白这个目录项是什么

 lrwxrwxrwx   1 ateam    ateam          7 Mar 17 11:23 fr -> fruinfo*



更多信息。

在 shell 脚本中,对fr 的调用如下所示

fr -s "$newfile" -o "$frufile"

shebang 线是这样的

#!/bin/bash

在Perl文件中,shell脚本是这样调用的

my $cmd = qq(bash makecsv.sh $initdir$sam.xcm $csvdest);
print $cmd, "\n";
#system 'bash makecsv.sh ${initdir}${sam}.xcm $csvdest';
system($cmd);

如果有帮助,上面的变量都加了双引号。

【问题讨论】:

  • 顺便说一句,makecsv.sh 和 perl 脚本都在同一个目录中。上面显示的 fr 文件列表位于不同的目录中。
  • 尝试在makecsv.sh 中的fr 调用之前添加echo "DEBUG: newfile: $newfile",然后查看从shell 和Perl 脚本运行makecsv.sh 时打印的内容。
  • 好的。我会试试的。你知道 fruinfo 和 counter 文件对 fr 和 c 部分做了什么吗?
  • frc 分别是到 fruinfocounter 的符号链接。这意味着例如要运行fruinfo,用户可以使用./fruinfo./fr 并且将执行相同的二进制文件。维基百科有更多关于 Unix 符号链接的内容:en.wikipedia.org/wiki/…
  • @noc_coder:这是一个非常糟糕的 Perl 脚本,无法正确处理绝对文件路径。你有兴趣修复它吗?同时,如果您有解决问题的方法,请将其写下来作为答案并接受它,以便其他人可以从您的经验中受益。

标签: linux bash perl shell


【解决方案1】:

从 Perl 脚本内部对“系统”的调用中,我不必要地添加了 $initdir 变量。

my $cmd = qq(bash makecsv.sh $initdir$sam.xcm $csvdest);
print $cmd, "\n"; 
system($cmd);

我将初始化 $cmd 的行更改为这个并且它起作用了:

 my $cmd = qq(bash makecsv.sh $sam.xcm $csvdest);

现在 makecsv.sh 长什么样,程序长这样。我故意缩短了目录。

#!/bin/bash
#/bin/env sh
# makecsv.sh /path/to/source/ /path/to/destination/

SRC=$1
DST=$2

# separate filename from ext
filename="${SRC%.*}"
extension="${SRC##*.}"

NOW="$(date +'%d-%m-%Y_%T')"
newfile="${filename}_${NOW}.$extension"

mv -f "$SRC" "$newfile"
mv -f "$newfile" "$DST"
#mv -f "${filename}_${NOW}.$extension" "$DST"

cd $DST
# rename file with .csv ext (without the .xcm)
noExt="${newfile%.*}"
frufile="${noExt}.csv"
fr -s "$newfile" -o "$frufile"

mv "$frufile" "/home/.../SAMlog"
rm "$newfile"

# remove files older than 90 days
find /home/..../SAMlog -type f -mtime +90 -exec rm {} \;

# create a md5sum stored in a file for frufile
cp /home/.../SAMlog/"$frufile" "${DST}/md5sum"
cd "${DST}/md5sum"
# create a fingerprint for the new frufile
md5file="${noExt}.md5"
touch "$md5file"
md5sum "$frufile" > "$md5file"
rm "$frufile"

# check for md5sum files older than 1 day and delete them
# only ever need to keep the sums of the current day and the previous one for cmp
find /home/.../md5sum -type f -mtime +1 -exec rm {} \;

这段代码从不应该接受完整路径,尽管我在顶部写了它应该接受的。因此,我将更改它以接受完整路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    相关资源
    最近更新 更多