0

I am trying to pass a config file asan argument from the command line to a script and not getting the expected results. Thank you :).

config called on command line as:

bash script.sh /path/to/config.txt

contents of /path/to/config.txt

#File1comment
var1=/path/to/file1
#File2comment
var2=/path/to/file2

contents of script.sh

config=$1

. $config

echo -e "$@"
test -n "$1" && set -- a b c

current

/path/to/config.txt

desired --- each file avaliable to script ---

/path/to/file2
/path/to/file2

new code

config=$1

set -- $config
echo -e "$@"
while read line; do
  printf "$line" >script.sh
done <"$config"
3
  • 1
    Why aren't you printing $var1 and $var2 in your script? Commented Jan 5, 2020 at 13:56
  • so I just need to printf after the set to print each line in config? Thank you :). Commented Jan 5, 2020 at 14:18
  • I added a new code to the post that if I just print the $line, I do see each var` printed. However when I re-direct it to the script I do not see them. Are they avaliable to the script to use? Thank you :). Commented Jan 5, 2020 at 14:45

1 Answer 1

1

It's a bit unclear what you want or need to do with your configuration file.

If your configuration file is a file that you wish to source in your script, as you do in one of you examples, then the code in the file name by $config will be executed.

Executing the code in the file name in $config sets the two variables var1 and var2. To use their values, use $var1 and $var2 in the script.

#!/bin/bash

config=$1

. $config

printf 'var1 is %s\n' "$var1"
printf 'var2 is %s\n' "$var2"

If you instead want to parse out the strings after the = characters in the file pointed to by $config, then you would probably not source the file, and instead use sed like so:

#!/bin/bash

config=$1

sed -n '/^#/d; /[^=]*=/s///p' "$config"

The sed command in this script ignores any lines that start with a # character or that does not contain a = character. For every other line, the first = character on the line and all that precedes it is removed and the remaining data is printed.

1
  • Thank you very much :) Commented Jan 7, 2020 at 0:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.