3

I have two scripts ins.sh and variable.sh. variables.sh holds various key-value pairs.

ins.sh

#!/usr/bin/env bash

set -e # Exit upon error

# This script generates a 64-bit system
source variables.sh

# Parse options
while getopts ":t:" opt; do
    case $opt in
        t )
            if [ $OPTARG = TRUE ] || [ $OPTARG = FALSE ]; then
                sed -i "s/*.MAKE_TESTS=.*/MAKE_TESTS=${OPTARG}/" variables.sh
            else
                echo "Invalid argument. -t only takes either 'TRUE' or 'FALSE'."
                exit 1
            fi
            ;;

        \? )
            echo "Invalid option: -$OPTARG" >&2
            ;;

        : )
            echo "Option -$OPTARG requires an argument."
            ;;
    esac
done

variables.sh

MAKE_TESTS=TRUE
MAKE_PARALLEL=-j4
INSTALL_DIR=/tmp/install-dir

To change the value is issue the following command:

bash ins.sh -t FALSE

This command should change to the MAKE_TESTS=FALSE but this doesn't happens at all.

I just want to toggle the values from TRUE to FALSE and vice versa. To achieve this, I was replacing the whole string and passing the value provided by the user.

UPDATE

For the time being, I've found a way to accomplish my task. First, I'm deleting the whole string and then adding the new string.

sed -i "/MAKE_TESTS/d" variables.sh
echo "MAKE_TESTS=${OPTARG}" >> variables.sh

But I would still like to know why my string replacement ain't working.

2

Replace:

 sed -i "s/*.MAKE_TESTS=.*/MAKE_TESTS=${OPTARG}/" variables.sh

with:

 sed -i "s/.*MAKE_TESTS=.*/MAKE_TESTS=${OPTARG}/" variables.sh

.* means zero or more of any character. By contrast, the meaning of *. may likely vary from one implementation of sed to another. In GNU sed, it means a literal star, *, followed by any one character. Observe:

$ echo 'aa' | sed 's/*./HI/'
aa
$ echo '*a' | sed 's/*./HI/'
HI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.