0

我正在尝试准备一个与 gnu 并行使用的 bash 脚本。该脚本应采用文件名,将文件名的前缀存储为描述器,并将行数 (wc -l) 存储为数字变量。如果这些都成为在 perl 脚本中使用的变量。描述器工作正常。

但是我对行数的存储,或者我对 ${mm} 的使用没有生成 perl 脚本识别的数字变量。任何更正表示赞赏。

#!/bin/bash

# Get the filename and strip suffix
sample=$1
describer=$(echo ${sample} | sed 's/.sync//')
echo ${describer} # works fine

# Get the number of rows
mm=$(cat ${sample} | wc -l)
echo ${mm} # works fine but is this a numeric variable?

# run the script using the variables; 
# the ${mm} is where the perl script says its not numeric
perl script.pl --input ${describer}.sync --output ${describer}.genepop --region ${describer}:1-${mm}
4

1 回答 1

0

这不是答案。我只是想用更好的风格重写你的脚本。你知道,你不需要一直用大括号来引用变量!例如,$mm足够好,${mm}在您的情况下不需要。此外,您sed删除注释的语句可以替换为等效项。我在这里和那里添加了双引号,以便您还可以使用包含空格和其他有趣符号的文件名的所有内容。我还删除了cat.

#!/bin/bash

# Get the filename and strip suffix
sample=$1
describer=${sample%.sync}
echo "$describer" # works fine

# Get the number of rows
mm=$(wc -l < "$sample")
echo "$mm" # works fine but is this a numeric variable?

# run the script using the variables; 
# the $mm is where the perl script says its not numeric
perl script.pl --input "$sample" --output "$describer.genepop" --region "$describer:1-$mm"

关于您的主要问题:问题可能出在程序中。

关于您的问题,这是一个数字变量吗?,答案是:变量没有类型。它们都是字符串。现在继续阅读:

某些版本wc在它们输出的数字前添加空格,例如,

$ wc -l < file
      42

(注意 42 前面的空格)。您应该能够wc通过运行我给您的脚本版本(带有正确的引用)来注意到您的版本是否以这种方式运行。如果您在数字前面看到一些空格,这可能是您的问题的原因。

如果是这种情况,您应该更换该行

mm=$(wc -l < "$sample")

read mm < <(wc -l < "$sample")

希望这可以帮助!

于 2013-07-03T13:55:01.073 回答