Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new to Bash - keep getting Illegal option error

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
like image 950
Duenna Avatar asked Apr 07 '14 10:04

Duenna


People also ask

What does [- Z $1 mean in Bash?

$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.

What does $() mean in Bash?

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).

What is $1 in bash script?

Process script inputs $1 - The first argument sent to the script. $2 - The second argument sent to the script.

What does #@ mean in Bash?

$@ 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.


2 Answers

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:

like image 103
Jayesh Bhoi Avatar answered Sep 20 '22 17:09

Jayesh Bhoi


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
like image 41
mattst Avatar answered Sep 22 '22 17:09

mattst