File read not interpreting Environment variables [duplicate] - linux

This question already has an answer here:
Expand ENV variable of a string, run command and store in variable?
(1 answer)
Closed 6 years ago.
When I try to read a file which contains a environment variable HOSTNAME.
It is not interpreting its value while reading the file.
For example, if my hostname is linux1.com.
When I try to read a sample file(Test.txt) below
/var/log/$HOSTNAME
using the code below
while read line
do
ls -l $line
done < Test.txt
I am expecting it to interpet the $HOSTNAME variable and print it. But it is not working. It is directly doing ls -l /var/log/$HOSTNAME, instead of
ls -l /var/log/linux1.com
The same command is intrepreting the hostname when I run this command in shell.
Any leads on this is highly appreicated.
Thanks.

Parameter expansion is not recursive. When $line is expanded, its value is subject to pathname expansion (globbing) and word-splitting, but not further parameter expansions.

Related

Bad substitution in Bash [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed last month.
I have written this code in which m getting bad substitution error. Please help here.
#!/bin/bash
function detect_value(){
#some operation gives u a value
echo "ABCD"
}
ABCD_COUNT=26
echo "${"$(detect_value)"_COUNT}"
bash run.sh
run.sh: line 12: ${"$(detect_value)"_COUNT}: bad substitution
The name of a parameter has to be static, not produced by another expression. To do what you want, you need to use indirect parameter expansion, where the name of the parameter to expand is stored in another parameter.
t=$(detect_value)_COUNT
echo ${!t}
Depending on your use case, you might want to use an associative array instead.
declare -A counts
counts[ABCD]=26
detect_value () {
echo ABCD
}
echo "${counts[$(detect_value)]}"

How to source a key value paired file in bash escaping whitespace? [duplicate]

This question already has answers here:
Use key/value data from a file in a shell script
(1 answer)
Reading key/value parameters from a file into a shell script
(1 answer)
Closed 3 years ago.
$ cat foo.txt
a=1one
b=2two
c=3 three
d=4four
$ source foo.txt
bash: three: command not found...
Need to set all the variable listed in foo.txt, how to source this file by escaping the space character? foo.txt comes from other application, which I cannot control, or is there an alternative to source ?
If the output is so regular, you could try to preprocess the file using sed like this:
$ sed -e "s/=/='/;s/$/'/" < foo.txt >sourced.env
and then source sourced.env. This will add a ' just after the = and add an ending '.

bash: initiate command inside of a string. - using sed [duplicate]

This question already has answers here:
How to replace a value with the output of a command in a text file?
(2 answers)
Closed 5 years ago.
my sed input is as follows:
sed 's/ListenAddress=.*/ListenAddress= $hostname/' nodemanager.properties
I am trying to run this against a Linux server and replace ListenAddress={current_value} with ListenAddress={hostname_of_server}
I need to know how to run the hostname command and have that output be reflected at the end of ListenAddress=
Thanks
If you wish your bash variables to reflect inside the sed script use, double quotes. The same is valid for command substitution. So you should be doing
sed "s/ListenAddress=.*/ListenAddress= $(hostname)/" nodemanager.properties
Since thevariable expansion takes place, you need to be careful about certain situations where $ appear as a sed attribute. For example if you're applying the above command only to the last line of the file, then do
sed "\$s/ListenAddress=.*/ListenAddress= $(hostname)/" nodemanager.properties
Note the $ before s command is escaped meaning that it is literal $ supplied to the script.

Bash script doesn't evaluate variable in filename [duplicate]

This question already has answers here:
What is the difference between ${var}, "$var", and "${var}" in the Bash shell?
(7 answers)
Closed 5 years ago.
I have a bash script which creates a backup of my folder. It should name the tar gz file using a version number but it doesn't:
#!/bin/bash
ver='1.2.3'
tar czf $ver_myfolder.tar.gz .
Expected output:
1.2.3_myfolder.tar.gz
Actual output:
_myfolder.tar.gz
If I append the variable like this:
#!/bin/bash
ver='1.2.3'
tar czf myfolder-$ver.tar.gz .
It works
You should use ${var} here since you are appending underscore after it which is considered a valid character for variable names. Due to that fact shell doesn't know whether you're expanding variable name $ver or $ver_myfolder:
ver='1.2.3'
tar czf "${ver}_myfolder.tar.gz" .
Since $ver_myfolder is unset, you get an empty value.
Because the underscore is a valid character for a variable name, you should use braces to explicitly specify the range of your variable:
${ver}_myfolder.tar.gz
^ ^
Without braces, Bash will actually try to parse
${ver_myfolder}.tar.gz
For your edited question, it is because the dot is not a valid character for a variable name, so Bash will not attempt to parse the dot into the name lookup. Even if you put it into braces, a variable name containing a dot is still invalid:
$ echo ${ver.}
bash: ${ver.}: bad substitution
$ ver.=1.2.3
ver.=1.2.3: command not found

Capturing command output in a shell variable isn't working [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
I am new to shell scripting, and need to output a series of commands to a local variable in a shell script, but keep on failing. For instance, the output of grep -c to a variable that will be use in an if statement. If anyone can redirect me over to a source that explains the process, I will appreciate.
#!/bash/sh
myVar = ls ~/bin | grep -c $0
Posting your code at shellcheck.net gives you valuable pointers quickly:
myVar = ls ~/bin | grep -c $0
^-- SC2037: To assign the output of a command, use var=$(cmd) .
^-- SC1068: Don't put spaces around the = in assignments.
^-- SC2086: Double quote to prevent globbing and word splitting.
If we implement these pointers:
myVar=$(ls ~/bin | grep -c "$0")
Also note that your shebang line has an incorrect path - the #! must be followed by the full path to the executing shell's binary.
Resources for learning bash, the most widely used POSIX-compatible shell:
Introduction: http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html
Guide: http://mywiki.wooledge.org/BashGuide
Cheat sheet: http://mywiki.wooledge.org/BashSheet

Resources