Bash variable of variable and assign to a variable [duplicate] - bash

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Create dynamic variable name bash and get value
(3 answers)
Closed 1 year ago.
I have a scenario as follows:
testing1=acs
testing2=rcs
testing3=mes
testing4=gcp
i need to print the values: acs rcs mes gcp
I am using following for loop:
TotalOutputString=''
for current_number in {1..${max_number}}
do
TotalOutputString="${TotalOutputString} ${testing$current_number} "
done
echo $TotalOutputString
But this is not giving proper output. Its only printing numbers.

You use ${!key) to indirectly access the variable specified by key. In your case:
TotalOutputString=''
for((i=1; 1<=$max_number; i++)) {
key="testing$current_number"
TotalOutputString="${TotalOutputString} ${!key} "
}
echo $TotalOutputString

Related

Bash, reading string into variable results in reading command into variable with used logically [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 9 months ago.
I have a file that contains a number. I want to read this number into a variable and then use it in an if statement. I am using the following command to populate the variable:
step='cat ./update_step'
The file, update_step has a single number stored in it. The number can be 0, 1, 2, 3, 4, or 5. For the sake of this example, the file contains the number "0".
if I check the variable as so:
$step
Then I get "0" as a return; which is expected.
But then if I try to use $step in an if statement like:
if [ $step -eq 0 ]
then
echo "this is an integer"
fi
I get a, "too many arguments" error.
If I check the variable with echo:
echo "$step"
Then the variable returns "cat ./update_step"
How do I read in the number that is stored in update_step as an integer (honestly it could even be a string at this point) so that I can use it with an if statement?
Try
step=$(cat ./update_step)
echo $step

error when store value in list in variable in bash [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed last year.
i try to run that bash code below to get the value in list and store it in variable then get basename and concatenate with it "lv-" :
lv_LST=("/usr/sap" "/sapmnt")
lv_size_as_bash=("4" "3")
((L=1))
for n in $(seq 0 $L)
do
lv_mount = ${lv_LST[$n]};
lv_size = ${lv_size_as_bash[$n]};
lvname1=$(sudo basename $lv_mount);
lvname2='lv-';
lvname=$lvname2-$lvname1;
but i get that error:
lv_mount: command not found
failed to run commands: exit status 127
You have extra spaces
Change
lv_mount = ${lv_LST[$n]};
lv_size = ${lv_size_as_bash[$n]};
To
lv_mount=${lv_LST[$n]};
lv_size=${lv_size_as_bash[$n]};

What does forwardslash mean between two variables in an IF statement in shell? [duplicate]

This question already has an answer here:
What is the meaning of `//` in Bash parameter expansions?
(1 answer)
Closed 1 year ago.
I am trying to understand what the forward slash between the two variables here does?
"${lsof_line/$c2}"
Below is context. (c2_iocs is a text file)
lsof_output=$(lsof -i)
for lsof_line in ${lsof_output}; do
for c2 in "${c2_iocs[#]}"; do
# echo "$lsof_line - $c2"
if [ "${lsof_line/$c2}" != "$lsof_line" ]; then
log warning "[!] C2 server found in lsof output SERVER: $c2 LSOF_LINE: $lsof_line"
fi
done
done
It's a string replacement operation. When no replacement text is provided, the / following the pattern is optional. ${lsof_line/$c2} is equivalent to ${lsof_line/$c2/}, and both delete the string contained in $c2 from $lsof_line.

Why does echo not echo variables [duplicate]

This question already has answers here:
Why is no string output with 'echo %var%' after using 'set var = text' command in cmd? [duplicate]
(2 answers)
Closed 2 years ago.
#echo off
set a[0] = 1
echo %a[0]%
When I try to run this, all I get is: "ECHO is off" and not a[0]. When I try to run it with echo on, all it returns is that echo is on. What do I need to change so that it only echoes the value of a[0]?
Remove the white spaces around the assignment.
Instead of ,
set a[0] = 1
Use
set a[0]=1
The script would look like .,
#echo off
set a[0]=1
echo %a[0]%

Command not found on bash function call [duplicate]

This question already has answers here:
Bash script returning "Not found" error
(2 answers)
Closed 9 years ago.
Hi i'm new to bash scripting but cannot understand why i get the command not found error when i try to assign to a local variable the result of this function call with parameters 20120920 5.
#!/bin/bash
function nDaysAgo () #date # daysago
{
date -d "${1} - ${2} days" +%Y%m%d;
}
so the script name is ndaysago, i'm first invoking the script with . ndaysago and then assigning the value like this:
newdate= nDaysAgo 20120910 5
it prints: 20120905: command not found
Meaning that the date execution is made but then tries to use the output as command, thats not what i would expect.
i have also tried assigning the new value to a variable within the function like so:
#!/bin/bash
function nDaysAgo () #date # daysago
{
var=$(date -d "${1} - ${2} days" +%Y%m%d)
}
but still nothing, mmmmmm
Spaces are not allowed around the = when assigning a variable. To invoke a function you should use the $(...) syntax which is called command substitution.
Change to:
newdate=$(nDaysAgo 20120910 5)

Resources