Syntax shell scripting? "word unexpected in" getopts - shell

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

Related

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

How to exit recent getopts options?

My script:
run() {
while getopts ":dr" option; do
case "$option" in
d) echo "__DEBUG__";;
r) echo "__RELEASE__";;
esac
done
if [ $option -eq ""]
then
echo "__DEBUG__" ;
fi
}
Hi, i'm using zsh. When i call run without any options
$ run
$ __ DEBUG __
in the first time it's ok and show DEBUG
Then i call with option (d/r) and call run with no agrument again it show this error
$ run -r
$ __ RELEASE __
$ run
$ run-9: parse error: condition expected: r
I don't know how to fix this, it seem that the while loop still has effect
You need to reset OPTIND before the getopts call at the start of your function, this will not be done automatically in this case. In bash, it is set to 1 when a shell or shell script starts, but not when a function is called.
This seems to be what POSIX getopts specifies.
Whenever the shell is invoked, OPTIND shall be initialized to 1.
zsh getopts behaves differently (thanks #PesaThe):
OPTIND has an initial value of 1, and is normally set to 1 upon entry to a shell function and restored upon exit.
Also, when getopts has finished parsing, option will be set to ?, which will not help you determine whether a (valid) option was provided.
Try something like this:
run() {
OPTIND=1
found=0
while getopts ":dr" option; do
case "$option" in
d) echo "__DEBUG__"; found=1 ;;
r) echo "__RELEASE__"; found=1 ;;
esac
done
if [ $found -eq 0 ]
then
echo "__DEBUG__" ;
fi
}
Or a bit simpler (but not identical to yours, to illustrate):
run() {
OPTIND=1
mode="__DEFAULT__"
while getopts ":dr" option; do
case "$option" in
d) mode="__DEBUG__" ;;
r) mode="__RELEASE__" ;;
esac
done
echo "$mode"
}

Command line parsing in shell script

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.

Syntax Error at Line * ) `in Ksh?

Im trying to execute the following code in Unix and getting the Above Error ,
Please Hep me to correct the code
SERVER_NM=`uname -n`
case $SERVER_NM in
infad1) export ETL_SYS=TST
;;
infasa1) export ETL_SYS=TST
;;
infasb1) export ETL_SYS=TST
;;
infap1) export ETL_SYS=PRD
;;
infap2) export ETL_SYS=PRD
;;
infap3) export ETL_SYS=PRD
;;
infap4) export ETL_SYS=PRD
;;
*) echo "No Dir";;
esac
when i execute this im getting
unexpected4]: syntax error at line 5 : `in and im using !/usr/bin/ksh
Is there a chance you have funny characters like carriage returns (\r) in your script?
Please run od -c yourscript to find out and if so, use a better editor that can display funny characters (vim) or dos2unix yourscript.
export SERVER_NM=`uname -n`
case $SERVER_NM in
infad1|infasa1|infasb1) export ETL_SYS=TST ;;
infap1|infap2|infap3|infap4) export ETL_SYS=PRD ;;
*) echo "No Dir" ;;
esac
echo $ETL_SYS
SERVER_NM="`uname -n`"
echo "${SERVER_NM}" | sed 's/infad1.*/PRD/;t;s/infasa1.*/PRD/;t;s/infasb1/TST/;t;s/infap[1-4].*/PRD/;t;s/.*/No Dir/' | read Tempo
export ETL_SYS="${Tempo}"
quicker maybe (depending of use of SERVER_NM)
export ETL_SYS="`uname -n | sed 's/infad1.*/PRD/;t;s/infasa1.*/PRD/;t;s/infasb1/TST/;t;s/infap[1-4].*/PRD/;t;s/.*/No Dir/'`"

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