frazman
frazman

Reputation: 33243

cleaning out optional arguments and passing it out shell script

I am trying to execute a cpp binary from shell script.. The cpp takes arguments and some of them are optional.

So basically what I am trying to do is generating the appropiate string and then executing that string something like:

generate_string()
{
    string="$path/to/binary"
    param1=$1
    param2=$2
    generated_string="$string $param1 --param2=$param2"
    echo $generated_string
}

#  execution function
execute()
{
    read -p 'param1: ' param2
    read -p 'param2: ' param2
    echo 'optional arguments'
    *read -p "param3: " param3   
    read  -p "param4: " param4*
    string=$(generate_string $param1 $param2 ???????)
    eval $string
}

Now in this function.. either or both of param3 and param4 can be blank What I want is if it is blank then offcourse I dont generate it in a string.

But I feel its too messy to have if statements..

Is there a way to solve this gracefully

Upvotes: 1

Views: 218

Answers (1)

Zombo
Zombo

Reputation: 1

This should work sir

#!/bin/sh

usage ()
{
  echo usage: $0 PARAM1 PARAM2 [PARAM3] [PARAM4]
  exit
}

[ $2 ] || usage
eval "path/to/binary" "$@"

Upvotes: 1

Related Questions