Cannot convert string '=' to double (m_Pos = 0) error in bash - bash

I want to give a numerical value as input argument for a bash script which is as below
#!/bin/bash
while getopts ":hl:q:s:e:k:g:v" opt; do
case $opt in
l)
lincRNAfasta=$OPTARG
;;
q)
query_species=$OPTARG
;;
s)
subject_species=$OPTARG
;;
e)
subject_gff=$OPTARG
;;
g)
subject_genome=$OPTARG
;;
k)
known_lincRNAs=$OPTARG
;;
v)
evalue=$OPTARG
;;
h)
usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
echo "Starting on $subject_species"
# Formatting the genome
makeblastdb -logfile stderr.out -in $subject_genome -dbtype nucl -out BLAST_DB/$subject_genome.blast.out
# Blasting the lincRNA transcripts against the genome to find out the location on the genome And identify if there are any paralogs in the query genome.
blastn -logfile stderr.out -query $lincRNAfasta -db BLAST_DB/$subject_genome.blast.out -num_threads 4 -penalty -2 -reward 1 -gapopen 5 -gapextend 2 -dust no -word_size 8 -evalue $evalue -outfmt "6 qseqid sseqid pident length qlen qstart qend sstart send evalue bitscore" -out Homology_Search/$subject_species.out
When i run the above script like this:
sh test.sh -l sample.data/Hsap_lincRNAs.fasta -q Hsap -s Hsap -e sample.data/Hsap_genes.gff -g sample.data/Hsap_genome.fasta -k sample.data/Hsap_lincRNAs -v 1e-20
I get this following error.
Error: Cannot convert string '=' to double (m_Pos = 0)
Error: Argument "evalue". Argument cannot be converted: `='
Error: (CArgException::eConvert) Argument "evalue". Argument cannot be converted: `='
I have never run a numeric value as argument with the getopts module in bash and so i am not sure what am i doing wrong here. Any help is much appreciated.
Thanks
Upendra

Related

Update parameter with default value in a bash script [duplicate]

I'm trying to make a getopt command such that when I pass the "-ab" parameter to a script,
that script will treat -ab as a single parameter.
#!/bin/sh
args=`getopt "ab":fc:d $*`
set -- $args
for i in $args
do
case "$i" in
-ab) shift;echo "You typed ab $1.";shift;;
-c) shift;echo "You typed a c $1";shift;;
esac
done
However, this does not seem to work. Can anyone offer any assistance?
getopt doesn't support what you are looking for. You can either use single-letter (-a) or long options (--long). Something like -ab is treated the same way as -a b: as option a with argument b. Note that long options are prefixed by two dashes.
i was struggling with this for long - then i got into reading about getopt and getopts
single char options and long options .
I had similar requirement where i needed to have number of multichar input arguments.
so , i came up with this - it worked in my case - hope this helps you
function show_help {
echo "usage: $BASH_SOURCE --input1 <input1> --input2 <input2> --input3 <input3>"
echo " --input1 - is input 1 ."
echo " --input2 - is input 2 ."
echo " --input3 - is input 3 ."
}
# Read command line options
ARGUMENT_LIST=(
"input1"
"input2"
"input3"
)
# read arguments
opts=$(getopt \
--longoptions "$(printf "%s:," "${ARGUMENT_LIST[#]}")" \
--name "$(basename "$0")" \
--options "" \
-- "$#"
)
echo $opts
eval set --$opts
while true; do
case "$1" in
h)
show_help
exit 0
;;
--input1)
shift
empId=$1
;;
--input2)
shift
fromDate=$1
;;
--input3)
shift
toDate=$1
;;
--)
shift
break
;;
esac
shift
done
Note - I have added help function as per my requirement, you can remove it if not needed
That's not the unix way, though some do it e.g. java -cp classpath.
Hack: instead of -ab arg, have -b arg and a dummy option -a.
That way, -ab arg does what you want. (-b arg will too; hopefully that's not a bug, but a shortcut feature...).
The only change is your line:
-ab) shift;echo "You typed ab $1.";shift;;
becomes
-b) shift;echo "You typed ab $1.";shift;;
GNU getopt have --alternative option
-a, --alternative
Allow long options to start with a single '-'.
Example:
#!/usr/bin/env bash
SOPT='a:b'
LOPT='ab:'
OPTS=$(getopt -q -a \
--options ${SOPT} \
--longoptions ${LOPT} \
--name "$(basename "$0")" \
-- "$#"
)
if [[ $? > 0 ]]; then
exit 2
fi
A=
B=false
AB=
eval set -- $OPTS
while [[ $# > 0 ]]; do
case ${1} in
-a) A=$2 && shift ;;
-b) B=true ;;
--ab) AB=$2 && shift ;;
--) ;;
*) ;;
esac
shift
done
printf "Params:\n A=%s\n B=%s\n AB=%s\n" "${A}" "${B}" "${AB}"
$ ./test.sh -a aaa -b -ab=test
Params:
A=aaa
B=true
AB=test
getopt supports long format. You can search SO for such examples.
See here, for example

Why do shortened versions of long options work with getopt?

In the following script:
#!/usr/bin/env bash
func_usage ()
{
cat <<EOF \
USAGE: ${0} \
EOF
}
## Defining_Version
version=1.0
## Defining_Input
options=$(getopt -o "t:" -l "h,help,v,version,taxonomy:" -a -- "$#")
eval set -- "$options"
while true;do
case $1 in
-h|--h|-help|--help)
func_usage
exit 0
;;
-v|--v|-version|--version)
echo $version
;;
-t|--t|-taxonomy|--taxonomy)
echo "Option t = $2 ";
Taxonomy_ID=$2
echo $Taxonomy_ID
shift
;;
--)
shift
break;;
esac
shift
done
## Defining Taxonomy Default Value (in case is not provided)
TaxonomyID=${Taxonomy_ID:=9606};
echo $TaxonomyID
exit 0
The commands:
./script.sh -v
./script.sh --v
./script.sh -version
./script.sh --version
Work as expected. But what I do not understand is why the commands:
./script.sh -ver
./script.sh --ver
work at all. An equivalent unexpected behavior is also observed for the commands:
./script.sh -tax 22
./script.sh --tax 22
I would be grateful to get an explanation and/or a way to correct this unexpected behavior.
Note that getopt is an external utility unrelated to Bash.
what I do not understand is why the commands: .. work at all.
Because getopt was designed to support it, there is no other explanation. From man getopt:
[...] Long options may be abbreviated, as long as the abbreviation is not ambiguous.
Unambiguous abbreviations of long options are converted to long options.
Based on the comments I have received, specially from #CharlesDuffy, I have modified my code to what I believe is a more robust and compatible version. Importantly, the code below addresses the pitfalls of the original code
#!/usr/bin/env bash
func_usage ()
{
cat <<EOF
USAGE: ${0}
EOF
## Defining_Version
version=1.0
## Defining_Input
while true;do
case $1 in
-h|--h|-help|--help|-\?|--\?)
func_usage
exit 0
;;
-v|--v|-version|--version)
echo $version
;;
-t|--t|-taxonomy|--taxonomy)
echo "Option t = $2 ";
Taxonomy_ID=$2
echo $Taxonomy_ID
shift
;;
--)
shift
break;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*)
break
esac
shift
done
TaxonomyID=${Taxonomy_ID:=9606};
echo $TaxonomyID
exit 0
The code above behaves as expected in that the commands:
./script -tax 22
Gives the warning:
WARN: Unknown option (ignored): -tax
9606
As expected

Issues storing arguments from getopts in shell script

I am new using unix and I recently tried to create a code that would take some arguments and pass it to a python script and later use those arguments for downstream processes.
I am trying to use getopts but after I tried running the script it looks like my arguments were not stored in variables and anything downstream failed.
This is a piece of what I tried so far
#!/bin/bash
function usage() {
cat <<USAGE
Usage: $0 [-f fasta] [-o output name] [-m memory] [-t time] [-n number of tasks] [-c cores] [-g gpus] [-e extra]
Options:
-f fasta file
-o output name
-m memory to request
-t time to request
-n number of tasks
-c cores to request
-g gpus to request - no more than 4!
-e extra arguments for program
USAGE
}
while getopts ":f:o:m:t:n:c:g:e:h" flag
do
case "${flag}" in
f)
fasta=${OPTARG}
;;
o)
out_name=${OPTARG}
;;
m)
memory=${OPTARG}
;;
t)
time=${OPTARG}
;;
n)
number=${OPTARG}
;;
c)
cores=${OPTARG}
;;
g)
gpus=${OPTARG}
;;
e)
extra=${OPTARG}
;;
h)
usage
;;
:)
echo "Invalid option: $OPTARG" 1>&2
usage
exit 1
;;
\?)
echo "Invalid option"
usage
exit 1
;;
esac
done
python3 /scratch/amolinav/programs/my_script_single.py ${fasta} ${out_name} ${memory} ${time} ${number} ${cores} ${gpus} ${extra}
I would really appreciate if someone could point out where I am making the mistake.
Thank you so much in advance for your help

bash getopts, why is my script not parsing additional input parameter?

I'm trying to create a bash script which reads a set of optional input parsing parameters. I tried to follow this example on GitHub.com.
I tried to add another integer parameter to read, which I called x (or xxx-size). I thought it was sufficient to duplicate the stack-size line command in the while loop, but it was not.
When I run the script, the system puts the x parameter as the last one in the order, and does not read it. I cannot understand what is going on.
Here's my code:
#!/bin/bash
#
# Example of how to parse short/long options with 'getopt'
#
OPTS=`getopt -o vhnxs: --long verbose,dry-run,help,xxx-size,stack-size: -n 'parse-options' -- "$#"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
echo "$OPTS"
eval set -- "$OPTS"
VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE=0
XXX=-1
printf "\n1st parameter: "$1"\n"
printf "2nd parameter: "$2"\n"
printf "3rd parameter: "$3"\n"
printf "4th parameter: "$4"\n"
printf "5th parameter: "$5"\n\n"
while true; do
case "$1" in
-v | --verbose ) VERBOSE=true; shift ;;
-h | --help ) HELP=true; shift ;;
-n | --dry-run ) DRY_RUN=true; shift ;;
-x | --xxx-size ) XXX="$2"; shift; shift ;;
-s | --stack-size ) STACK_SIZE="$2"; shift; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
echo VERBOSE=$VERBOSE
echo HELP=$HELP
echo DRY_RUN=$DRY_RUN
printf "STACK_SIZE "$STACK_SIZE"\n"
printf "XXX "$XXX"\n"
printf "\n\n"
Here's what happens if I try to set the s parameter to 100 and the x parameters to 65:
$ ./script.sh -s 100 -x 65
Standard output:
-s '100' -x -- '65'
1st parameter: -s
2nd parameter: 100
3rd parameter: -x
4th parameter: --
5th parameter: 65
VERBOSE=false
HELP=false
DRY_RUN=false
STACK_SIZE 100
XXX --
As you can see, the program does not associate the value 65 to XXX, as I would like. How can I solve this problem?
Thanks!

Getopts not properly parsing arguments

I have following shell script -
while getopts “h:f:p:u” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
f)
FILE=$OPTARG
;;
u)
US=$OPTARG
;;
p)
PASSWD=$OPTARG
;;
?)
usage
exit
;;
esac
done
echo "$FILE"
echo "$PASSWD"
echo "$US"
I use following commandline arguments -
-u root -f mydb -p h2
There is no output on screen. Why?
Your call to getopt should look like this ...
while getopts “hf:p:u:” OPTION
... because h takes no args and the other options do.
It should be while getopts “hf:p:u:” OPTION

Resources