In Shell script process variables in text [duplicate] - shell

This question already has answers here:
bash replace variable name in string with variable value
(2 answers)
Closed 2 years ago.
I have a file 'test.txt', the contents is "SomeText_$(date '+%Y%m%d')".
When reading this into a variable with:
txt=`cat test.txt`
Then I try to print with
echo $txt
This prints: "SomeText_$(date '+%Y%m%d')"
How do I print this so I receive "SomeText_20200904"

The posted echo will display the content from $txt but not execute anything else.
The second line here with eval will read and process what follows then execute the result as a shell command
txt=`cat test.txt`
eval echo $txt

Related

echo and read with pipe issue [duplicate]

This question already has answers here:
Read values into a shell variable from a pipe
(17 answers)
Why variable values are lost after terminating the loop in bash? [duplicate]
(1 answer)
Closed 17 days ago.
First of all, I am sorry, I am learning the bash and I am a newbie.
Please find the below script.
grep "error" /var/log/syslog | while read line
do
echo $line
done
If I am not wrong,The above script will grep the keyword "error" in /var/log/syslog and will send it inside the while loop as STDIN and output will be displayed.
Also please loop the below script.
echo "hello" | read hi
echo $hi
So when I run this script I am not getting any output, why is that?
should I use any loop? only then I will get output?

Store into var the result of grep command shell script [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 3 years ago.
I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var.
$text var got inumerous tags, one of them having the string "isDone", thats the line i want:
code:
short_str="isDone"
echo "$text" | grep "$short_str"
output:
< s:key name="isDone">1</s:key >
now i want to store that output from grep into a file, and then extract the value (on this case is 1)
what have i tried:
store="$("$text" | grep "$short_str")"
echo "$store"
but that outputs all the file, what am i doing wrong?

How to echo string with variable substitution in bash? [duplicate]

This question already has answers here:
Forcing bash to expand variables in a string loaded from a file
(13 answers)
Closed 3 years ago.
I have a string like ${REPOSITORY}/company/api:${API_VERSION}. $REPOSITORY and $API_VERSION are shell variables.
$ echo ${DATA_API_VERSION}
latest
$ echo ${REPOSITORY}
com.company.repo
I want to get the interpolated string that shows the values of these variables and assign it to another variable.
This is what I get:
$ echo "$image"
${REPOSITORY}/company/api:${API_VERSION}
I want this:
com.company.repo/company/api:latest
You could use sed to search and replace the two variables.
#!/bin/bash
DATA_API_VERSION="latest"
REPOSITORY="com.company.repo"
image='${REPOSITORY}/company/api:${DATA_API_VERSION}'
sed -e "
s/\${REPOSITORY}/$REPOSITORY/g
s/\${DATA_API_VERSION}/$DATA_API_VERSION/g
" <<< "$image"

How assign rev command to a variable in shell [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I wanted to direct the output of rev command to a variable, I tried different methods and didn't work.
read -p "Enter the number: " n
echo $n | rev
echo "new n is: $p"
I want to assign the output of line 2 to p. How?
Thank you,
To store the output of a command in a variable use a $(...) command substitution:
p=$(echo $n | rev)
For further reference, you can check this link

shell variable not keeping value [duplicate]

This question already has answers here:
Variables getting reset after the while read loop that reads from a pipeline
(3 answers)
Closed 7 years ago.
I have 3 lines in file /root/backuplist.txt.
The first echo prints perfectly, but the last one prints an empty line; I'm not sure why. Somehow, the $DIRS value is getting unset.
#!/bin/bash
cat /root/backuplist.txt |
while read line
do
DIRS+="$line "
echo $DIRS
done
echo $DIRS
Problem is use of pipe here, which is forking a sub-shell for your while loop and thus changes to DIRS are being made in the child shell that are not visible in the parent shell. Besides cat is unnecessary here.
Have it this way:
#!/bin/bash
while read -r line
do
DIRS+="$line "
echo "$DIRS"
done < /root/backuplist.txt
echo "$DIRS"

Resources