How do I get a variable value that is stored in a string in a file? - bash

I have a file with strings that include variables. I want to retrieve the string with the value of the variable being represented in the string. I have tried ${!var} without success.
A file called test.line with the string and a variable:
$ cat test.line
Hello $var3
A bash script called test.sh:
$ cat test.sh
#!/bin/bash
var1="hello"
var2="goodbye"
var3="again"
## works
str="$var1 and $var2"
echo $str
## does not work--prints $var3 instead of "again"
str="$(cat test.line)"
echo $str
Output of test.sh when run:
$ ./test.sh
hello and goodbye
Hello $var3
Desired output of test.sh when run:
$ ./test.sh
hello and goodbye
Hello again

$ cat test.sh
#!/bin/bash
var1="hello"
var2="goodbye"
var3="again"
## works
str="$var1 and $var2"
echo $str
## SOLVED -- Does work
str="$(cat test.line)"
eval echo $str # This is the line that SOLVED it
Output of test.sh when run:
$ ./test.sh
hello and goodbye
Hello again ##This is what I want (SOLVED)

Related

bash run script from here doc [duplicate]

This question already has answers here:
Using variables inside a bash heredoc
(3 answers)
Closed 3 years ago.
In the code below, variable X is output normally.
# cat a.sh
X=world
echo 'hello' $X
# cat a.sh | bash
hello world
But, using here doc, variable X is not displayed.
# cat <<EOF | bash
> X=world
> echo 'hello' $X
> EOF
hello
# bash -s <<EOF
> X=world
> echo 'hello' $X
> EOF
hello
What made this difference?
You can see what happens when you remove the |bash
X=oldvalue
cat <<EOF
X=world
echo "hello $X"
EOF
The $X is replaced before piping it to bash.
You can check the following
X=oldvalue
cat <<"EOF"
X=world
echo "hello $X"
EOF
This is what you want to execute:
cat <<"EOF" | bash
X=world
echo "hello $X"
EOF

Exporting dependant variable failed in shell script

I have three shell script files, one global variable named "VER" and its value is "2017.4"
1. variable.sh
2. function.sh
3. main.sh
variable.sh
var1=/home
var2=/home/${VER}_version
function.sh
Contain a function named export_function which takes one variable as argument, perform grep operation to find that variable from variable.sh file and export the grep output
export_function () {
var=`grep "$1=" variable.sh | sed -e "s/"$1="//g"`
export $1=$var
}
main.sh
source function.sh
export_function var2
echo "$var2"
When I run the main.sh, get output: /home/${VER}_version instead of /home/2017.4_version
Note: echo $VER in main.sh and function.sh shows value 2017.4
Constrains:
variable.sh is a read-only file
source variable.sh is not allowed
I've rewritten your scripts like that, and it works:
[sahaquiel#sahaquiel-PC ~]$ cat variable.sh
#!/bin/bash
var1=/home
var2="/home/${VAR22}_version"
[sahaquiel#sahaquiel-PC ~]$ cat function.sh
#!/bin/bash
export_function () {
source variable.sh
var=$(echo "var2=${var2}" | grep "$1=" | sed -e "s/"$1="//g")
export $1=$var
}
[sahaquiel#sahaquiel-PC ~]$ cat main.sh
#!/bin/bash
source function.sh
export_function var2
echo "$var2"
[sahaquiel#sahaquiel-PC ~]$ bash main.sh
/home/2017.4_version
[sahaquiel#sahaquiel-PC ~]$ echo $VAR22
2017.4

writing file using cat with here-document

$ cat > out.txt <<EOF
> Hello world
> EOF
$
How do I do this in single statement?
Somethig like 'echo' in following statement
$ for i in {1..5}; do echo "Hello world" > out_$i.txt; done
You can use a here-string, which is a shortcut for short here documents in bash.
cat <<< "Hello world" > out.txt

Creating flag for print the output of shell script

I am creating a shell script. Now, I want to create a flag to print the output of script on the screen if flag is ON otherwise script will not print the output if flag is OFF
Thanks
This might work for you:
#!/bin/bash
# assuming your first argument is the printing flag
[[ "${1}" = "ON" ]] && OUTPUT="/dev/stdout" || OUTPUT="/dev/null"
# from now on:
echo "Something" > $OUTPUT
# will work as expected...
test.sh code below:
#!/bin/sh
while IFS= read -r line
do
cat "$line"
done < $1
Test it:
$ ls
myflags testfile0 testfile1 testfile2 test.py test.sh
$ cat myflags
testfile0
testfile1
test.py
$ cat testfile0
some test
$ sh test.sh myflags
some test
#!/usr/bin/python
import sys
if sys.version_info[0] == 2:
sys.stdout.write("ls -l")
$

reading variables from input file bash

I have an input file that looks like:
VAR1=1
VAR2=2
VAR3=3
VAR4=.T.
I'd like to read in these variables and define them as such. I've tried
while read line
do
exec $line
done < "Master.inp"
i've tried just
$line
instead, but that didn't work either. Is there a way to run the string as if I had just typed out the string in the bash file?
You can do it just sourcing the code:
. file
this will let you use the vars $VAR1, $VAR2, ...
Test
$ cat a
VAR1=1
VAR2=2
VAR3=3
VAR4=.T.
$ cat b
. a
echo $VAR1
$ ./b
1

Resources