Why does echo not echo variables [duplicate] - windows

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]%

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]};

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

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

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.

How to give an empty array to function in bash? [duplicate]

This question already has answers here:
How to pass array as an argument to a function in Bash
(8 answers)
Closed 6 years ago.
I am learning bash.
Now I would like to give an empty array to a function. However, it does not work. Please refer to following,
function display_empty_array_func() {
local -a aug1=$1
local -a aug2=${2:-no input}
echo "array aug1 = ${aug1[#]}"
echo "array aug2 = ${aug2[#]}"
}
declare -a empty_array=()
display_empty_array_func "${empty_array[#]}" "1"
The outputs are following,
array aug1 = 1 # I expected it is empty
array aug2 = no input # I expected it is 1
In my understanding, quoting variable allow us to give an empty variable,
like following,
function display_empty_variable_func() {
local aug1=$1
local aug2=${2:-no input}
echo "variable aug1 = ${aug1}"
echo "variable aug2 = ${aug2}"
}
display_empty_variable_func "" "1"
And its outputs are following
variable aug1 = # it is empty as expected
variable aug2 = 1 # it is 1 as expected
I don't know what is the problem with passing am empty array.
Someone who knows mechanism or solutions. Please let me know it.
Thank you very much.
enter image description hereIf positional parameter is empty, shell script & shell function will not consider that value and instead it took the next non empty value as its value(positional parameter value).
If we want to take the empty value, must we have to put that value in quotes.
Ex: I'm having small script like
cat test_1.sh
#!/bin/bash
echo "First Parameter is :"$1;
echo "Second Parameter is :"$2;
case -1
If i executed this script as
sh test_1.sh
First Parameter is :
Second Parameter is :
Above two lines are empty because i have not given positional parameter values to script.
case-2
If i executed this script as
sh test_1.sh 1 2
First Parameter is :1
Second Parameter is :2
case-2
If i executed this script as
sh test_1.sh 2 **# here i given two spaces in between .sh and 2 and i thought 1st param is space and second param is 2 but**
First Parameter is :2
Second Parameter is :
The output looks like above. My first statement will apply here.
case-4
If i executed this script as
sh test_1.sh " " 2
First Parameter is :
Second Parameter is :2
Here i kept space in quotes. Now i'm able to access the space value.
**This will be help full for you. But for your requirement please use below code.**
In this you have **quote** the space value(""${empty_array[#]}"") which is coming from **an empty array**("${empty_array[#]}"). So you have to use additional quote to an empty array.
function display_empty_array_func() {
local -a aug1=$1
local -a aug2=${2:-no input}
echo "array aug1 = ${aug1[#]}"
echo "array aug2 = ${aug2[#]}"
}
declare -a empty_array=()
display_empty_array_func ""${empty_array[#]}"" "1"
The output is:
array aug1 =
array aug2 = 1

Resources