Command line parsing in shell script - bash

I have a shell script where i am parsing command line argument. My arguments has path in it.
Example: mycript.sh -c test -s global -n /mydir/test1 -d /orgdir/test35
When I run the script and echo the arguments which contains path(special char "/"), it gives me empty path.
#/!bin/bash
...
while getopts c:hs:n:d opt
do
case "$opt" in
c) INST=$OPTARG;;
d) INST_DIR=$OPTARG;;
h) usage;;
s) METHOD=$OPTARG;;
n) MAINTENANCE_DIR=$OPTARG;;
\?) usage;;
esac
done
echo INST dir is [$INST_DIR]
echo MAINTENANCE dir is [$MAINTENANCE_DIR]
.......
Result of this echo is
INST dir is []
MAINTENANCE dir is []
Can someone tell what is incorrect here?

With your script, I have a different result:
INST dir is []
MAINTENANCE dir is [/mydir/test1]
I have changed line 4 of your script like this:
while getopts c:h:s:n:d: opt
It works fine for me under Cygwin, with bash version 4.1.10(4)-release.

Related

Bash getopts not reading arguments to variables

Hi all I think I am going insane. I have the following at the beginning of a bash script:
#!/bin/bash
while getopts i:o:c:p:g: flag
do
case "${flag}" in
g) genome=${OPTARG};;
p) poscol=${OPTARG};;
c) chromcol=${OPTARG};;
i) inf=${OPTARG};;
o) outf=${OPTARG};;
esac
done
echo ""
echo "This should be a list of the arguments:"
echo $inf
echo $outf
echo $poscol
echo $chromcol
echo $genome
echo "-----------------------"
Yet when I run a test of the code, eg:
bash Code.sh -i test.txt -o test.txt -c 0 -p 1 -g testgenome
bash returns:
This should be a list of the arguments:
-----------------------
I literally just copied this section of code from a script where the arguments work fine. Does anyone have any idea what could be going on here? Thanks in advance.

Bash script outputs the command but will not run it

I have written a script that is shown below. However, when I run the script it just outputs the final command instead of running it.
What do I need to add at the end so that the command will be run?
Thank you
#!/bin/bash -l
#$ -cwd
# file name: singlecell.sh
rundir=$1
outdir=$2
samplesheet=$3
USAGE="Usage: $0 -i [path to input run directory] -o [path to output directory] -s [path to sample sheet]"
while getopts i:o:s:h opt
do
case "$opt" in
i) RunDir="$OPTARG";;
o) OutDir="$OPTARG";;
s) SampSheet="$OPTARG";;
h) echo $USAGE; exit 1
esac
done
if [[ $RunDir == "" || $OutDir == "" || $SampSheet == "" ]]
then
echo $USAGE
exit 1
fi
# asssume only one index type per run
index8=`grep "SI-GA\|SI-NA" $SampSheet | wc -l`
ignore=""
# check if single indexes.
if [ $index8 -gt 0 ]; then
ignore="--filter-single-index"
else
ignore="--filter-dual-index"
fi
cmd="cellranger mkfastq --run=$RunDir --output-dir=$OutDir --csv=$SampSheet --localcores=8 --localmem=40 $ignore --barcode-mismatches=0"
echo $cmd
$cmd
You can replace the last lines:
cmd="cellranger mkfastq --run=$RunDir --output-dir=$OutDir --csv=$SampSheet --localcores=8 --localmem=40 $ignore --barcode-mismatches=0"
echo $cmd
$cmd
With:
cellranger mkfastq --run=$RunDir --output-dir=$OutDir --csv=$SampSheet --localcores=8 --localmem=40 $ignore --barcode-mismatches=0
If you are in doubt the program actually ran as it may not give output in the terminal, the usual way is to check for its return value. This is the general status value returned by a program, such as the common return 0; at the end of main function in C programs.
cellranger mkfastq --run=$RunDir --output-dir=$OutDir --csv=$SampSheet --localcores=8 --localmem=40 $ignore --barcode-mismatches=0
cellranger_ret=$?
echo "cellranger_ret = ${cellranger_ret}"
This gives you a clue if the expected output from the program didn't show up and it is not reporting any error. Typically 0 means success and other values indicate error. Other useful tip is to look for debug related parameters (command line arguments) that will allow you to increase the verbosity level of the program.

error :- test: ing: unary operator expected

below is my script to run some sub commands. When i am running the script with ./test.sh sub_cmd $APP it is running fine. But when i ran it like test sub_cmd $APP it throwing an error.
I want to run the script without using ./
error :- test: ing: unary operator expected
#!/bin/bash
ProgName=$(basename $0)
APP=${2?Need chart name}
sub_help(){
echo "Usage: $ProgName <subcommand> [options]\n"
}
sub_dep(){
echo "Running 'dep' command."
vi some_path/${APP}/templates/deployment.yaml
}
sub_ing(){
echo "Running 'ing' command."
vi some_path/${APP}/templates/ingress.yaml
}
sub_svc(){
echo "Running 'ing' command."
vi some_path/${APP}/templates/service.yaml
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
sub_help
;;
*)
shift
sub_${subcommand} $#
if [ $? = 127 ]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$ProgName --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac
You need to run "test.sh" not "test".
test sub_cmd $APP
What you are actually running is the test command which lives in one of the lib directories on your path.
If you type
which test
you can see which command/script/program that will be run.
When you prefix the command with a relative path (such as ./) or an absolute path (ie.e starting with a /) then bash will look in that specific folder for the command. If you omit the path then it will search the directories listed in the environment variable $PATH and execute the first one it comes across. The which command does the same thing, but just lists the script/program instead of executing it.

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

Syntax shell scripting? "word unexpected in" getopts

Good evening everybody, We're looking forward to execute a shell script on an Ubuntu 8.04, But it returns a special error:
line 14: Word unexpected (expecting "in")
So here is a preview of the script:
#!/bin/sh
#Declaration of shell parameters
A3REP=""
A3FILE=""
A3HELP=""
A3PORT="80"
A3PORTTC="80"
A3IP400=""
A3USER=""
A3PWD=""
A3TASK=""
while getopts d:f:h:p:i:u:w:t:n: option
do
case $option in
d)
A3REP="$OPTARG"
;;
f)
A3FILE="$OPTARG"
;;
p)
A3PORT="$OPTARG"
;;
t)
A3PORTTC="$OPTARG"
;;
i)
A3IP400="$OPTARG"
;;
u)
A3USER="$OPTARG"
;;
w)
A3PWD="$OPTARG"
;;
n)
A3TASK="$OPTARG"
;;
h)
A3HELP="aide"
;;
esac
done
PROBLEM SOLVED:
the script was in dos:
insert the following line in d2u.sh:
#!/bin/bash
cat $1|tr -d '\015'
and now execute :
chmod +x d2u.sh
now the script is ready to run.
Try this script typing the following command:
sh d2u.sh filename.sh > filename2.sh
and the script isn't in dos anymore.
Thank you everybody! :)
Perhaps a problem with line endings, try
d2u FILE
ref

Resources