Hi there,
I hope, that I do not open an thread, that is already existing, but I didn't found something matching with my problem while searching for problems with "getopts"
My problem ist, that I'm taking arguments from commandline into my script with getopts, I've an flag -s, after that there comes an path to an *.sql File.
e.g. script.sh -s /batch/alpha/database/create_db.sql
This is no problem, but I want to have it more save, want, that the custumer must exactly hit ONE file, even, if he/she is using joker like *.
Therefore I wanted to count the hits, and if there is more than 1 hit, the script should terminate.
But if I type for example: ./script.sh -s /test/test*.sql and in the directory are the files "test.sql" and "test1.sql" I will not find the term "/test/test*.sql" in $OPTARG but "/test/test1.sql" !!!
Is there any chance, perhaps some kind of quoting, that I can get what I typed in on the commandline?? Only then I could check, if the term produces more than one matchings.
Thank you for the help and excuse my worse English, as I'm coming from Germany.
So long,
Thomas
Hello,
You can get count of command line arguments using $# variable. Sample code
if [ $# -gt 1 ]
then
echo "Only one argument can be provided to $0" ;
exit 1 ;
else
#your desired coding
echo "Continue" ;
fi
-Nithin.
I'm afraid, that you don't understand my problem right now.
I'm reading many arguments with getopts from the commandline. Many of them are optional. My problem is, that I'm reading an argument, that includes an absolute path, as "/test/test*.sql". When I read the argument from $OPTARG, I found the joker "*" substituted with the first file, that matches, for example, "/test/test1.sql", but I need the non substituted argument, so that I can use the input.
So if I had a variable with "/test/test*.sql" I could do the following:
e.g.:
var_path="/test/test*.sql"
var_file_matches=`ls $var_path | wc -l`
if [ $var_file_matches -gt 1 ]
then
echo "More than one match with you argument, BREAK!"
exit 99
fi
.
.
.
other programmcode
This is the part in my script, reading arguments from the shell:
#! /usr/bin/ksh
.
.
.
while getopts ":rRvVHhk:K:FfQ:q:D:d:Z:z:s:S:p:P:cC" arg # Variablen einlesen
do
case $arg in
q|Q)
QUELL_PFAD="$OPTARG"
;;
d|D)
QUELL_DATEI="$OPTARG"
;;
k|K)
KONFIG_PFAD="$OPTARG"
;;
s|S)
SQL_PFAD="$OPTARG"
;;
p|P)
PARAMETER="$OPTARG"
;;
z|Z)
ZIEL_PFAD="$OPTARG"
;;
c|C)
COMPRESS_FLAG=1
;;
h|H)
usage
exit_log 0
;;
v|V)
version
exit_log 0
;;
f|F)
FORCIERE_NEU_DIR=1
;;
r|R)
REKURSIV_FLAG=1
;;
*)
para_fehler
;;
esac
done
shift