I'm quite new to ubuntu and bash scripting and wanted to know why I might be getting this error when using GETOPTS.
here is the code I use to run it.
sh /home/ubuntu/Desktop/test.sh -f /home/u/Desktop/ -p 'TEST'
I think i'm calling the script correctly, and it should search for the term I enter as a search term using grap. but for some reason it doesn't. Any advice on what I can do as a general rule when working with grep would also be appreciated, thanks.
#!/bin/bash
valid=0
file_arg=""
display_help=""
column=""
pattern=""
while getopts f:d:s:m: opt
do
case "$opt" in
d) display_help=$OPTARG
;;
f) file_arg=$OPTARG
;;
c) column=$OPTARG
;;
p) pattern=$OPTARG
;;
*) valid=1
break
;;
esac
done
if [ $valid -eq "0" ]
then
if [ $pattern != "" ]
then
cat $file_arg | grep $pattern
else
cat $file
fi
else
echo -n "Usage: FILE -f <name> | COLUMN -> -c <name> | HELP -> -d | PATTERN -> -p <expression>"
fi
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
Example of command substitution using $() in Linux: Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).
Process script inputs $1 - The first argument sent to the script. $2 - The second argument sent to the script.
$@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.
In getopts
you not specify p
option you only have f:d:s:m:
options.
I think you mean p
instead m
or vice versa.
It should f:d:s:m:p:
or f:d:s:p:
You should also consider the error supression and error handling features of getopts
.
If the very first character of the option string is a colon (:
) then getopts
will not report errors and instead will provide a means of handling the errors yourself. Two additional characters can then be used within your case conditional handling:
?
If an invalid option is entered then $opt
will be set to ?
and $OPTARG
will hold the invalid character, e.g. if -z
was used, which is not in your option string, then $OPTARG
will be set to z
.
:
If a required additional argument is omitted by the user then $opt
will be set to :
and $OPTARG
will hold the command character, e.g. if -p
was used instead of -p arg
then $OPTARG
will be set to p
.
If this is implemented then the catch-all of *
becomes redundant and should be removed. Note: If you leave it in and it is above either ?
or :
then you'll be asking for problems. Also make sure the ?
is escaped like this \?)
.
Hope this helps.
# Note the addition of the inital colon before 'f'.
while getopts :f:d:c:p: opt;
do
case $opt in
d) display_help=$OPTARG
;;
f) file_arg=$OPTARG
;;
c) column=$OPTARG
;;
p) pattern=$OPTARG
;;
# Option error handling.
\?) valid=0
echo "An invalid option has been entered: $OPTARG"
;;
:) valid=0
echo "The additional argument for option $OPTARG was omitted."
;;
# This is now redundant:
# *) valid=0
# break
# ;;
esac
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With