linux - 无法从 Perl 内部运行有效的 shell 脚本

标签 linux bash perl shell

我有一个 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);

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

最佳答案

从对 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 {} \;

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

关于linux - 无法从 Perl 内部运行有效的 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36969237/

相关文章:

perl - foreach 和特殊变量 $_ 未按预期运行

mysql - 使用 Perl 将多个逗号分隔值添加到 MySQL 行

c - 是否有 C 函数来获取文件数据段的大小?

mysql - 如何设置旋转完全和增量innobackupex?

linux - 通过 ipv6 连接时 SSH 超时,但适用于 ipv4

bash - 从逗号分隔的文件中删除主机

linux - 通过 Linux 命令行检查文件是否为空

linux - 现在复制文件稍后粘贴

bash - 在 awk 中将列移到最后

regex - Perl 忽略正则表达式替换的替换侧的空格