0

Bash version : 5.0.3(1)-ui-release

I don't know where I'm wrong.

1. One file "install_options.sh" with variables inside, like :

ADMIN_EMAIL="[email protected]"                               
ADMIN_FIRST_NAME="John"                                                  
ADMIN_NAME="Doe" 
....

2. another file : "admin_settings.txt" with data like "KEY VALUE" (withespace separator), and few value is variable:

KEY VALUE
KEY "${ADMIN_EMAIL}"
NEED_HELP PLEASE
KEY "${ADMIN_FIRST_NAME}"
...

3. When i try on cli:

$ echo "${ADMIN_EMAIL}"

I have the right output:

[email protected]

4. I try to use while read loop:

#!/bin/bash

while read IFS= -r KEY VALUE; do
 
COMMAND $KEY "${VALUE}"
 
done < admin_settings.txt

I get output:

KEY "${ADMIN_EMAIL}"

I would like get the output with the value:

KEY [email protected]

I have tried many things, like put source file inside the while read loop, but not working:

#!/bin/bash

while read IFS= -r KEY VALUE; 
do
   source install_options.sh
   COMMAND $KEY "${VALUE}"

done < admin_settings.txt

Inside "admin_settings.txt", I have tried with:

KEY "${ADMIN_EMAIL}"
KEY ${ADMIN_EMAIL}
KEY "$ADMIN_EMAIL"
KEY $ADMIN_EMAIL

Same with COMMAND inside while loop, I have tried:

COMMAND $KEY "${VALUE}" 
COMMAND $KEY ${VALUE} 
COMMAND $KEY "$VALUE"
COMMAND $KEY $VALUE

What can I do to get the expected output?

5
  • Tanks for your repply, inside the "admin_settings.txt", I have try : KEY "${ADMIN_EMAIL}" KEY ${ADMIN_EMAIL} KEY "$ADMIN_EMAIL" KEY $ADMIN_EMAIL And try to leave the shebang, but i dont get the excpected output. Commented Sep 2, 2022 at 14:33
  • 1
    So you can modify admin_settings.txt as you want? using KEY $ADMIN_EMAIL would be the easiest solution because it'll allows to use envsubst Commented Sep 2, 2022 at 15:52
  • 1
    It would be impossible to write secure code handling untrusted data in bash if variable references, command substitutions, and other syntax were silently expanded when present in data as this question seems to assume will happen! Commented Sep 2, 2022 at 16:37
  • 1
    I'm pretty sure you don't want to be doing source install_options.sh in the loop body, for every iteration. Commented Sep 2, 2022 at 17:23
  • Please edit your question to show the expected output given your posted sample input. Commented Sep 2, 2022 at 17:54

2 Answers 2

3

Assuming that admin_settings.txt is EXACTLY in the following format (no shebang, no blank lines, no comments, no double-quotes around variables, etc...):

KEY0 VALUE0
KEY1 $ADMIN_EMAIL
NEED_HELP PLEASE
KEY2 $ADMIN_FIRST_NAME

Then you can use envsubst for substituting the variables with their value:

set -a
source install_options.sh
set +a

envsubst < admin_settings.txt |
while IFS=' ' read -r key val
do
    COMMAND "$key" "$val"
done
Sign up to request clarification or add additional context in comments.

Comments

0

In this setup i cant use envsubst , but i just found another way with for loop and arrays, like :

source install_options.sh

VALUE=("$ADMIN_EMAIL" "$ADMIN_FIRST_NAME" "$ADMIN_NAME") 
KEY=("KEY0" "KEY1" "KEY2_TANKS_FOR_YOUR_REPPLY")    

for (( z=0; z<${#VALUE[@]}; z++ )); do

    COMMAND ${KEY[$z]} ${VALUE[$z]}

done

3 Comments

It's not clear how that code is related to your question. For example, where are your input files install_options.sh and admin_settings.txt? Also, copy/paste that into shellcheck.net and fix the issues it tells you about and read correct-bash-and-shell-script-variable-capitalization then fix those issues too. Your loop is also more complicated than it needs to be and if you can have arrays as your starting point you could just use 1 associative array instead of 2 indexed arrays.
I forgot to mention 'install_options.sh', I just edited my answer, and all data inside 'admin_settings.txt' has been transferred into the arrays. Thanks for your advices, tools, and links, I'll read carefully !
You're welcome but the main point is that this isn't an answer to the question you asked about how to read and then use values from a file (admin_settings.txt) that has a mix of key-value pairs and other text, it's a possible answer (but don't do it as it's buggy) to a much simpler problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.