如何在 getopts bash 中使参数可选?
How to make an argument optional in getopts bash?
我想创建一个可选字符 (-t
),它不应接受 getopts bash 中的任何参数。这是我到目前为止的进展
while getopts ":hb:q:o:v:t" opt; do
case $opt in
b)
Blasting_list=$OPTARG
;;
l)
query_lincRNA=$OPTARG
;;
q)
query_species=$OPTARG
;;
o)
output=$OPTARG # Output file
;;
t)
species_tree=$OPTARG
;;
h)
usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
我想运行上面的脚本是这样的..
bash test.sh -b Blasting_list.txt -l Sample_query.fasta -q Atha -o test_out -v 1e-20 -t
然后它应该执行下面的循环
(-----)
if [ ! -z $species_tree ];
then
mkdir -p ../RAxML_families
perl /Batch_RAxML.pl aligned_list.txt
rm aligned_list.txt
else
rm aligned_list.txt
fi
(-----)
如果我运行这样,它应该跳过循环。
bash test.sh -b Blasting_list.txt -l Sample_query.fasta -q Atha -o test_out -v 1e-20
(-----)
(-----)
我尝试使用 getopts 选项,但我无法让它工作。
可能最简单的方法是将 species_tree
设置为 true
,前提是存在 -t
命令行标志:
species_tree=false # <-- addition here
while getopts ":hb:q:o:v:t" opt; do
case $opt in
...
t)
species_tree=true # <-- change here
;;
...
esac
done
if $species_tree; then # <-- change here
...
我想创建一个可选字符 (-t
),它不应接受 getopts bash 中的任何参数。这是我到目前为止的进展
while getopts ":hb:q:o:v:t" opt; do
case $opt in
b)
Blasting_list=$OPTARG
;;
l)
query_lincRNA=$OPTARG
;;
q)
query_species=$OPTARG
;;
o)
output=$OPTARG # Output file
;;
t)
species_tree=$OPTARG
;;
h)
usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
我想运行上面的脚本是这样的..
bash test.sh -b Blasting_list.txt -l Sample_query.fasta -q Atha -o test_out -v 1e-20 -t
然后它应该执行下面的循环
(-----)
if [ ! -z $species_tree ];
then
mkdir -p ../RAxML_families
perl /Batch_RAxML.pl aligned_list.txt
rm aligned_list.txt
else
rm aligned_list.txt
fi
(-----)
如果我运行这样,它应该跳过循环。
bash test.sh -b Blasting_list.txt -l Sample_query.fasta -q Atha -o test_out -v 1e-20
(-----)
(-----)
我尝试使用 getopts 选项,但我无法让它工作。
可能最简单的方法是将 species_tree
设置为 true
,前提是存在 -t
命令行标志:
species_tree=false # <-- addition here
while getopts ":hb:q:o:v:t" opt; do
case $opt in
...
t)
species_tree=true # <-- change here
;;
...
esac
done
if $species_tree; then # <-- change here
...