0

I have a bash script "test.sh" and one parameter I want to use is --no-email.

when I run test.sh --no-email, everything works as expected and I do not receive an email status report.

However what I really want to run is "test.sh test.cnf" where the --no-email parameter is stored in the test.cnf file along with a load of other parameters. I cant for the life of me get this to work. Perhaps I am being completely stupid and not understanding?

Many thanks

echo $*|grep -se '--no-email'&>/dev/null
SEND_MAIL=`echo $?`

echo -e "DEBUG: \$*=$*"

if [ ! "$SEND_MAIL" == "0" ]; then
  echo 'Mail would have been sent!'
else
  echo 'NO MAIL WOULD HAVE BEEN SENT!'
fi

1 Answer 1

1

If you cannot modify the script test.sh to support this you still can use this syntax to fetch parameters from a config file:

test.sh $(<test.cnf)

If this assumption is not true, i.e. you want to modify test.sh itself to support this then you have to be more specific about what happens inside test.sh.

Edit: Now the content of test.sh has been added to the question. Starting from there the most simple thing to do would be like this:

grep -sqe '--no-email' "$*"
SEND_MAIL=$?

But you wrote that you have a bunch of other paramaters. Doing a grep for each one might be inconvenient. In this case you can loop over the word of a cnf file like this:

#!/bin/bash

while read line; do 
    for word in $line; do
        echo "examing $word"
        case "$word" in 
        --no-email) 
            SEND_MAIL=0
        ;;
        --no-foo)
            NO_FOO=0
        ;;
        *)
            echo 1>&2 "WARNING: Unknown parameter: $word"
        ;;
    esac
    done
done < "$1"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.