0

I would like to a code snippet that reads a properties file and defines variables in multiple other scripts, to avoid coding the same in each of my scripts.

For example, I want to be able to include the execution like below, in many scripts, after some initial work from each of the scripts, that have no relationship with each other

    while IFS='=' read -r key value
    do
      echo "key ${key}"
      if [[ ${key} =~ ^# ]]; then
        echo "comment line ${key},  skipping....."
        continue
      fi
      eval ${key}=\${value}
      echo "key ${key} :  value ${value}"
    done < kafka-env-parameters.txt

Since the parameter file and the defined variables will be same for multiple scripts, I want to avoid adding this snippet in every shellscript.

Is it possible to do so, in shell script? If so, how?

Thanks

4
  • Could you just do something like set -x; source kafka-env-parameters.txt; set +x? Commented Mar 29, 2022 at 19:36
  • 2
    Define a function, put it in a lib file and source it in each script? Commented Mar 29, 2022 at 19:54
  • If you don't need the variables key and value to persist after the code snippet has been executed, put it into a separate shell script file and run it as a command. Commented Mar 30, 2022 at 7:10
  • I do need it to persist - it defines the variables from the properties file - that would be common for many scripts - it defines kafka server info and and that info is used in the kafka command line commands that will be executed from each script - for example - one script is CREATE TOPICS, the grant ACLs etc. I am just trying to avoid adding same code to multiple scripts, if I could avoid it Commented Mar 30, 2022 at 14:40

1 Answer 1

0

You could create a function with this code, place it within a lib file, let's say readParams.lib[^1].

     readParamFile () {
      while IFS='=' read -r key value
      ....
      ....
      done < $1
    }

Then source (include) the function code with source readParams.lib and invoke it from your different scripts, passing the path of the file to be read as argument to the function: readParamFile kafka-env-parameters.txt

    <some code>   
    source readParams.lib 
    readParamFile kafka-env-parameters.txt 
    <some code>
    <some code>

[^1]: Thanks to Charles Duffy for the correction about functions declaration syntax.

Sign up to request clarification or add additional context in comments.

4 Comments

And I would call it like this readParamFile kafka-env-parameters.txt from inside my shell script ? Thanks
Note that function funcname() { merges the ksh function syntax function funcname { and the POSIX-standard function syntax funcname() { in a way that makes your code incompatible with both ksh and POSIX, while not providing the special behavior that function enabled in legacy ksh. See also wiki.bash-hackers.org/scripting/obsolete (there are references in both first and third tables).
@adbdkb, correct, after sourcing the file, you can call any function that file defines. (But... really, do take the function keyword off; it makes the code gratuitously incompatible while adding no benefit. source libraryname can also be replaced with . libraryname for an additional compatibility benefit, but at least source is more readable; function buys no advantage at all).
This worked. I was able to put the function in a separate file and was able to source it in multiple scripts. Thanks. Thanks also to @CharlesDuffy for giving detailed information about function keyword usage while defining a function in bash script

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.