Bash 存储文件中的文本和数字变量以在perl脚本中使用

Bash 存储文件中的文本和数字变量以在perl脚本中使用,bash,variable-assignment,perl,Bash,Variable Assignment,Perl,我正在准备一个bash脚本,用于gnupallel。脚本应该采用文件名,将文件名的前缀存储为描述符,并将行数(wc-l)存储为数字变量。如果这些变量成为perl脚本中要使用的变量,则两者都可以。描述器工作正常 但是我对行数的存储,或者我对${mm}的使用并没有生成perl脚本能够识别的数值变量。如有任何更正,我们将不胜感激 #!/bin/bash # Get the filename and strip suffix sample=$1 describer=$(echo ${sample} |

我正在准备一个bash脚本,用于gnupallel。脚本应该采用文件名,将文件名的前缀存储为描述符,并将行数(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}

这不是答案。我只是想用一种更好的方式重写你的剧本。你知道,你不需要总是引用带有花括号的变量!例如,
$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"
(注意42前面的空格)。您应该能够通过运行我提供给您的脚本版本(使用正确的引用),注意到您的
wc
版本是否有这种行为。如果您看到数字前面有空格,这可能是您出现问题的原因

如果是这种情况,则应更换管路

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


read mm<我觉得不错。问题可能出在perl脚本中。您是否正确地从参数中提取$mm?您是否尝试过打印perl脚本中的参数?谢谢@gniourf_gniourf,非常感谢您关于{}和cat的详细信息,以及引导空间的绝妙可能性,这确实是个问题。我可能应该将OSX声明为我的操作系统,这样可能会更容易。现在一切都好了。我学到了很多!哦,还要知道bash可以做sed喜欢的事情。杰出的
mm=$(wc -l < "$sample")
read mm < <(wc -l < "$sample")