Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass var values from .js file to the variables in the bash file

I have a variables.js file that includes several variables with values like:

var var_1 = 1;
var var_2 = 'someText';

I need to pass these values to variables within the vars bash file:

VAR_1
VAR_2

That sounds like a decently simple task, however, having a lack of practical experience with bash scripts baffles me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Tried using source or reading line by line, however, it didn’t lead to a possible solution.

>Solution :

With GNU grep and a Perl-compatible regular expression (-P):

VAR_1=$(grep -Po '(?<=^var var_1 = ).*?(?=;)' variables.js)
VAR_2=$(grep -Po '(?<=^var var_2 = ).*?(?=;)' variables.js)

See: The Stack Overflow Regular Expressions FAQ -> Lookarounds

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading