Bash re-assignment of value to variable "command not found" [duplicate] - bash

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 8 years ago.
Any ideas why this is happening? Why do I have to manually explicitly reassign the variable but can't do it if I have another variable in the name of the variable?
SCRIPT:
#!/bin/bash
a_1=1
a_2=1
for temp in 1 2
do
a_$temp="2"
echo $((a_$temp))
done
a_1=2
a_2=2
echo $a_1
echo $a_2
OUTPUT:
[dgupta#della4 Rates_Of_Quenching]$ ./test.sh
./test.sh: line 8: a_1=2: command not found
1
./test.sh: line 8: a_2=2: command not found
1
2
2

Instead of:
a_$temp="2"
Use:
declare a_$temp="2"
to create variable with dynamic name.

As far as bash is concerned, you are trying to execute the command 'a_1=2', rather than perform an assignment. You can get around this by using declare, or its synonym typeset:
'a_1=2' # bash: a_1=2: command not found
typeset 'a_1=2'
echo $a_1 # 2
declare 'a_1=3'
echo $a_1 # 3
While it is possible to use declare, you might want to take advantage of bash arrays (which have been around since bash version 2) rather than using variables with numerical suffixes:
a=(1 1)
echo ${a[0]} # 1
echo ${a[1]} # 1
for i in 0 1; do a[i]=2; done
echo ${a[0]} # 2
echo ${a[1]} # 2

Related

using command line arguments in loop shell [duplicate]

This question already has answers here:
Brace expansion with a Bash variable - {0..$foo}
(5 answers)
Closed 3 years ago.
I am trying to use command line arguments for arithmetic but cant seem to find any documentation explaining how to do this. As an example if I use:
for i in {$1..$2}
do
echo $i
done
and call
test.sh 1 20
the following output is produced:
{1..20}
instead of
1
2
3
..
20
The following will also work:
declare -a ary='({'$1..$2'})'
for i in "${ary[#]}"; do
echo "$i"
done
Note that declare is as harmful as eval.
You need to check and sanitize the arguments before use.
There's no way to do this properly without the evil eval() with brace expansion in bash.
You can use seq instead :
for i in $(seq $1 $2); do

How to receive command line argument in shell script for loop [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 3 years ago.
How to receive command line argument in shell script for loop? e.g
vim batch_echo
for i in {1..$1}; do echo $i; done
sh batch_echo 3
{1..3}
but if change to seq, it's ok
for i in `seq 1 $1`; do echo $i; done
sh batch_echo 3
1
2
3
so why {1..$1} cannot work?
Just like bash, I think it's because brace expansion occurs before expansion of variables.
I recommend you to go check: http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion . Let me know if that helped or not.
Hope I could help.

Bash conditional returning an error of bash: =: command not found [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
if [[ "$PROXY_URL"==https* ]]; then
echo "Woohoo"
else
echo "Woohoo"
fi
Running $PROXY_URL = "https://yolo" ; ./proxyEnv.sh gives me output of:
bash: =: command not found
Woohoo
What does the "bash: =: command not found" refer to?
Your string comparison should have spaces around the comparator:
if [[ "$PROXY_URL" == https* ]]; then
echo "Woohoo https"
else
echo "Woohoo no https"
fi
Also, that's not how you pass environment variables to bash scripts. You have two options:
PROXY_URL="https://yolo" ./proxyEnv.sh
or
export PROXY_URL="https://yolo"; ./proxyEnv.sh
The first option assigns (without the $) the value to the symbol and then uses that environment for the script (without the ; separating them). It only exists for the script.
The second option exports that symbol to the current environment, which the script inherits.

Bash: including variables in command-line arguments [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 6 years ago.
I have the following bash script, which is supposed to spider a range of IP addresses.
#!/bin/bash
a = 0
for i in `seq 1 255`;
do
a = a + 1
echo $i
wget -r --spider -D --header="Accept: text/html" --user-agent="Order Of The Mouse: Stress Tester" 139.162.246.$a:80
done
However, at the moment it doesn't include the variable a. How do I properly include a variable in a command line argument when writing a bash script?
Current output looking like this:
/root/burningWood/scripts/StressTest/tester.sh: line 5: a: command not found
254
Spider mode enabled. Check if remote file exists.
--2016-08-28 13:23:10-- http://139.162.246./
Resolving 139.162.246. (139.162.246.)... failed: Name or service not known.
wget: unable to resolve host address ‘139.162.246.’
Found no broken links.
I've made a couple of changes to your code:
#!/bin/bash
for i in {1..255} # <-- use brace expansion instead of seq, no semicolon needed
do
# a = a + 1 <-- variable $a is redundant, just use $i
wget -r --spider -D --header="Accept: text/html" \
--user-agent="Order Of The Mouse: Stress Tester" "139.162.246.$i:80"
done
I moved part of the call to wget onto a new line so you could see the change more clearly.
Note that there are no spaces around an assignment, so if you wanted to use the variable a, you would assign to it like a=$(( a + 1 )).
bash math needs special syntax - here's one way to do it
a=$((a + 1))

Combining bash aliases and arguments/parameters [duplicate]

This question already has answers here:
Make a Bash alias that takes a parameter?
(24 answers)
Closed 1 year ago.
Let's say I have a bash script called script.sh and I want to create an alias runscript. Now, I know how to create an alias in my bash_profile or bashrc.
However - if I want to run parameters and do the following
$ runscript param1 param2
Is there something special I need to write in the script or in the alias that allows me to run the alias and the use parameters as well?
Based on your other question - "Accessing Shell parameters inside functions" - here is an example:
$ ls script
script
$ cat script
#!/usr/bin/env bash
_aFunction() {
echo "Parameter 1: ${1}"
echo "Parameter 2: ${2}"
}
_aFunction
_aFunction "$1" "$2"
_aFunction One Two
$ alias my_alias="./script"
$ my_alias 1 2
Parameter 1:
Parameter 2:
Parameter 1: 1
Parameter 2: 2
Parameter 1: One
Parameter 2: Two

Resources