0

I am using the following kind of bash code. I store some information within log file which name is defined in the bash script.

LOGNAME="/tmp/ETH"
LOG_FILE="${LOGNAME}.log"    

function exit_error()
{
    case "$1" in
        100 )
            echo "Bad arguments supplied - Enter help"
            echo "Bad arguments supplied - Enter help" >> "${LOG_FILE}"
            ;;      
        101 )
            echo "Illegal number of parameters"
            echo "Illegal number of parameters" >> "${LOG_FILE}"
            ;;       
        * )
            ;;
    esac    
    exit 1;
}

function current_status()
{
    INT_STATUS=$(cat /sys/class/net/eth1/operstate)
    echo "status : $INT_STATUS"
    echo "status : $INT_STATUS" >> "${LOG_FILE}"
}

function connect_eth()
{
    ...
}

...

case "$1" in
    current_status )
        if [ "$#" -ne 1 ]
        then
            exit_error 101
        else
            current_status
        fi
        ;;  
    connect_eth )
        if [ "$#" -ne 1 ]
        then
            exit_error 101
        else
            connect_eth
        fi
        ;;
    read_MAC_addr )
        if [ "$#" -ne 1 ]
        then
            exit_error 101
        else
            read_MAC_addr 
        fi      
        ;;  
    read_IP_addr )
        if [ "$#" -ne 1 ]
        then
            exit_error 101
        else
            read_IP_addr
        fi
        ;;
    * )
        exit_error 100
        ;;
esac
exit 0; 

I would like to modify the script in order to use the specified log name if no other log name is specified as last parameter. However, I would like to keep my "exit_error 101" in switch case which is based on the number of parameters passed to the script. Is there a way to do that ? Because I can not modify the $# variable.

1 Answer 1

2

It should be possible. Do something like this:

CMD="$1"
shift
# use provided logname or set to default if not found
LOGNAME="${1:-/tmp/ETH}
shift
LOGFILE="${LOGNAME}.log"
# now, since we shifted, you just have to check for $# -eq 0 to
# be sure there are no params left.

... your function definitions here ...

# exit 101 if there are some parameters left
if [ $# -ne 0 ]; then
  exit_error 101
fi

case "$CMD" in
  current_status)
    current_status
    ;;
  ...
  *)
    exit_error 100
    ;;
esac

If you want more flexibility, you can always use getopts and named parameters. It is usually much easier to maintain.

And, if I were you, I would also centralize error handling before the case statement to avoir repeating the same check everywhere.

Sign up to request clarification or add additional context in comments.

4 Comments

I have already tried your solution, I pass thru exit_error 100 everytime.. because after shift $1 is equal to nothing
I completed the sample. Obviously $1, will be empty and $# equal to zero in non error cases. That was the point. Thus you have to use $CMD.
Thank tgo it works ! But your solution works only if one parameter is gave to the functions, is not ? If I have n function which accept variable parameters, I have to use an a switch case, based on each posibilities, on top of the script or there is a quite simple and efficient solution ?
@SnP you are welcome. Yes, this way things can grow quite complex indeed. That's why I was referencing getopts. And if, with getopts, things get too complex, it might mean you need more than one script/command.

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.