-1

This should be pretty straightfoward in bash, and I don't know why I am struggling with it. I am a bash newbie, so please be gentle.

pseudo code:

    read a configuration file, extract the first line beginning with a key/value pair
    in the format exec=/path/to/myprog -opt1 -opt2 $var1 $var2 ...
    check that the /path/to/myprog is executable
    if executable then
       replace $var1, ... with the contents of the same bash variables in the script
       check that all variables were replaced with existing bash variables
       if aok 
           execute the command and be happy
       else
           complain echoing the partially-substituted command string
       fi
    else
       complain echoing the un-substituted command string
    fi

Nothing I try seems to work properly. I have killed enough time trying various things. Suggestions, anyone?

1 Answers1

1

The conf file:

exec=/bin/ls -l $var1 $var2

The bash file:

#!/bin/bash

CONFIG="tmp.conf"

var1=./
var2=helo

function readconf() {
    args=()
    while IFS=' ' read -ra argv; do
        exec=${argv[0]#*=}
        `command -v ${exec} >/dev/null 2>&1 || { echo >&2 "I require ${exec} but it's not installed.  Aborting."; exit 1; }`
        for i in "${argv[@]:1}"; do
            if [[ $i == \$* ]]; then
                sub=${i:1}
                args+=(${!sub})
            fi
        done
    done < $CONFIG
    echo ${args[@]}
}

readconf

The code above provides the key components you need to implement what you what. At least I think so. You could add your logic based on this skeleton.

The following urls might be of some help:

Check if a program exists from a Bash script

In bash, how can I check if a string begins with some value?

How do I split a string on a delimiter in Bash?

Bash: add value to array without specifying a key

Use variable name as variable name

Community
  • 1
  • 1
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44