我有一个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脚本内部“system”的调用中,我不必要地添加了$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/