2

I have seen many examples for how to use getopts. But I know very basic of bash and I was not able to to implement it in my situation. I really appreciated if anyone expert can show me the template.

I have a script with minimum 6 and maximum 10 input. Here is a brief description:

script.sh -P passDir -S shadowDir -G groupDir -p password -s shadow

User must provide argument for -P -S -G and if not I must display usage and close the program. If argument are provided I need them to be saved into an appropriate variable.

But -p and -s is optional. However, if there is no -p I should do some tasks and if there is no -s I should do some other tasks and if none of them is there I should do some other tasks. Following is what I have written so far but it stock in the for loop.

#!/bin/bash

if [ "$(id -u)" != "0" ]; then
    echo "Only root may add a user to system"
    exit 2
else
    usage() { echo "Usage: $0 [-P <password file path>] [-S <shadow file path>] [-G <group path>]" 1>&2; exit 1; }
    passDir=""
    shadowDir=""
    groupDir=""
    while getopts ":P:S:G:" inp; do 
        case "${inp}" in
            P)
                $passDir = ${OPTARG};;
            S)  
                $shadowDir = ${OPTARG};;
            G)  
                $groupDir = ${OPTARG};;
            *)
                usage;;

         esac
    done

    echo "${passDir}"
    echo "${shadowDir}"
    echo "g = ${groupDir}"
fi

At the moment is user does not enter arguments nothing will be shown and if there is arguments it goes inside a loop!

2 Answers 2

3

As I understand it, you are just missing some if statements to handle missing arguments. Consider:

usage() { echo "Usage: $0 [-P <password file path>] [-S <shadow file path>] [-G <group path>]" 1>&2; exit 1; }

if [ "$(id -u)" != "0" ]; then
    echo "Only root may add a user to system"
    exit 2
fi
passDir=""
shadowDir=""
groupDir=""
while getopts "P:S:G:" inp; do_
    case "${inp}" in
        P) 
            passDir=${OPTARG};;
        S)
            shadowDir=${OPTARG};;
        G)
            groupDir=${OPTARG};;
        *) 
            usage;;   
     esac
done

if  [ -z "$passDir" ] && [ -z "$shadowDir" ]
then
    # if none of them is there I should do some other tasks
    echo do some other tasks
elif ! [ "$passDir" ]
then
    # if there is no -p I should do some tasks_
    echo do some tasks
elif ! [ "$shadowDir" ]
then
    #if there is no -s I should do some other tasks
    echo do some other tasks
fi
Sign up to request clarification or add additional context in comments.

Comments

2

I fixed a couple of things in your script. This works for me:

#!/bin/bash

if [ "$(id -u)" != "0" ]; then
    echo "Only root may add a user to system"
    exit 2
fi

usage() { echo "Usage: $0 [-P <password file path>] [-S <shadow file path>] [-G <group path>]" 1>&2
    exit 1 
}

passDir=""
shadowDir=""
groupDir=""
while getopts ":P:S:G:" inp; do 
    case "${inp}" in
        P)
            passDir=${OPTARG};;
        S)  
            shadowDir=${OPTARG};;
        G)  
            groupDir=${OPTARG};;
        *)
            usage;;

    esac
done

echo "p = $passDir"
echo "s = $shadowDir"
echo "g = $groupDir"
  • Assignments must not contain spaces: a=1 works, a = 1 doesn't
  • The variable name should not be prefixed with a $ in an assignment
  • If your if branch contains an exit statement, there's no need to put the rest of your code in the else branch

2 Comments

Thanks for your answer. I appreciate it. I chose other answer as solution because it explained solution for -s and -p is not provided. Thanks again
@Bernard : Now you have to add the cases for -p password and '-s shadow'. And don't forget to write a helpful usage message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.