作为bash脚本输入参数的文件列表
我有一个文件列表:作为bash脚本输入参数的文件列表,bash,list,cluster-computing,Bash,List,Cluster Computing,我有一个文件列表: path/dir/*.gz 我想把这个文件列表作为bash脚本的输入,以及与分析相关的其他参数(即输出文件夹和线程数) myscript.sh包含以下命令: fastqc $1 -o $2 -t $3 --noextract -d ./ fastqc程序能够在输入文件列表的多个线程上运行。 我在SGE集群中使用这个脚本,因此使用命令“qsub”。 我用以下方式运行它: qsub ./myscript.sh path/dir/*.gz output_path 2 但是,它
path/dir/*.gz
我想把这个文件列表作为bash脚本的输入,以及与分析相关的其他参数(即输出文件夹和线程数)
myscript.sh包含以下命令:
fastqc $1 -o $2 -t $3 --noextract -d ./
fastqc程序能够在输入文件列表的多个线程上运行。
我在SGE集群中使用这个脚本,因此使用命令“qsub”。
我用以下方式运行它:
qsub ./myscript.sh path/dir/*.gz output_path 2
但是,它不起作用。有人知道原因并能提出解决方案?
我的理解是,我把输入文件列表作为参数弄乱了。您的问题是,在启动脚本之前,shell正在将通配符扩展到各个文件名 以下是一些策略:
#!/bin/bash
if (( $# < 3 )); then
echo not enough arguments
exit 1
fi
# threads is the last argument
n=$#
threads=${!n}
# output_path is the 2nd-last argument
((n--))
output_path=${!n}
# discard the last 2 arguments
((n--))
set -- "${@:1:n}"
# now "$@" is the list of input files.
for file in "$@"; do
fastqc "$file" -o "$output_path" -t "$threads" --noextract -d ./
done
我不知道
fastqc
,但是如果它可以接受多个输入文件,那么就不要循环,而是执行
fastqc "$@" -o "$output_path" -t "$threads" --noextract -d ./
我真的不知道,手册中没有规定。但是如果我以fastqc路径/dir/*.gz输出路径2的形式运行它,它工作得很好。试着以:
/myscript.sh'path/dir/*.gz'输出路径2的形式运行它,它正在工作!!!非常感谢。另一个选项(我更喜欢的选项)是将输出文件和线程数放在arg列表的第一位($1
和$3
),输入文件是其余的参数($3
到$whatever
,bash可以方便地将其检索为${:3}
)。然后您可以在脚本中执行fastqc“${@:3}”-o“$1”-t“$2”--noextract-d./
。
#!/bin/bash
while getopts :o:t:h opt; do
case $opt in
h) show_help; exit ;;
o) output_path=$OPTARG ;;
t) threads=$OPTARG ;;
*) exit ;; # some error
esac
done
if [[ -z $output_path ]]; then
echo error message
exit 1
fi
if [[ -z $threads ]]; then
echo error message
exit 1
fi
# other validations, like $threads is a sensible whole number
shift $((OPTIND - 1))
# now "$@" is the list of input files.
for file in "$@"; do
fastqc "$file" -o "$output_path" -t "$threads" --noextract -d ./
done
fastqc "$@" -o "$output_path" -t "$threads" --noextract -d ./