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

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))

Related

Variables declared in shell script not being recognized as a variable and instead a command [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 9 months ago.
Here is the script that I wrote:
#!/bin/bash
directory1 = ~/path/to/directory/
directory2 = ~/path/to/directory2/
diff -r $directory1 $directory2 || echo "files are different"
And here is the output/error message that appears:
./compare.sh: line 2: directory1: command not found
./compare.sh: line 3: directory2: command not found
diff: missing operand after `-r'
diff: Try `diff --help' for more information.
files are different
I know that there is a problem in a way that I defined directory1 and directory2, but I don't exactly know what is wrong. Any help would be appreciated. Thanks!
Spaces are used as delimiters when assigning variables in bash; you should remove them, otherwise your variables don't exist (i.e. are empty) so the line
directory1 = ~/path/to/directory/
actually means "call program directory1 with arguments = and ~/path/to/directory, hence the " command not found".
Similarly, your call to
diff -r $directory1 $directory
is equivalent to
diff -r
which is indeed missing parameters.
You might also want to quote path parameters to correctly handle spaces:
#!/bin/bash
directory1=~/path/to/directory/
directory2=~/path/to/directory2/
diff -r "$directory1" "$directory2" || echo "files are different"

Extracting a json value to a variable in zsh shell script [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
I'm trying to fetch the value of a currency from remote api, so I can later run it through some calculation. However, I cannot assign this value to a variable.
#!/usr/bin/env zsh
result = $(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')
echo $result
This results in :
> ./script.sh:5: command not found: result
You can't have spaces around an equals sign in a Bourne-like shell (e.g., sh, bash, zsh). What happens is that when zsh sees this line:
result = $(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')
it thinks that result is the name of a command and that the equals sign and what follows it are the arguments of that command. To avoid this, just do this:
result=$(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')
ETA: I do notice that the line number for the error is 5, not 3 as I would expect. I don't know if that's because of CR/LF line ending issues, if there's something missing from the script that you showed us, or whatnot.

Can't manage to give two arguments from a fil to bash script : command not found [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I'm new to bash script, it is interesting, but somehow I'm struggling with everything.
I have a file, separated by tab "\t" with 2 infos : a string and a number.
I'd like to use both info on each line into a bash script in order to look into a file for those infos.
I'm not even there, I'm struggling to give the arguments from the two columns as two arguments for bash.
#/!bin/bash
FILE="${1}"
while read -r line
do
READ_ID_WH= "echo ${line} | cut -f 1"
POS_HOTSPOT= echo '${line} | cut -f 2'
echo "read id is : ${READ_ID_WH} with position ${POS_HOTSPOT}"
done < ${FILE}
and my file is :
ABCD\t1120
ABC\t1121
I'm launching my command with
./script.sh file_of_data.tsv
What I finally get is :
script.sh: line 8: echo ABCD 1120 | cut -f 1: command not found
I tried a lot of possibilities by browsing SO, and I can't make it to divide my line into two arguments to be used separately in my script :(
Hope you can help me :)
Best,
The quotes cause the shell to look for a command whose name is the string between the quotes.
Apparently you are looking for
while IFS=$'\t' read -r id hotspot; do
echo "read id is: $id with position $hotspot"
done <"$1"
You generally want to avoid capturing things into variables you only use once, but the syntax for that is
id=$(echo "$line" | cut -f1)
See also Correct Bash and shell script variable capitalization and When to wrap quotes around a shell variable?. You can never have whitespace on either side of the = assignment operator (or rather, incorrect whitespace changes the semantics to something you probably don't want).
You have a space after the equals sign on lines 5 and 6, so it thinks you are looking for an executable file named echo ABCD 1120 | cut -f 1 and asking to execute it while assigning the variable READ_ID_WH to the empty string, rather than assigning the string you want to the variable.

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 can I list a range of files in a Bash script? [duplicate]

This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 7 years ago.
I've got several files in a folder, with the following names:
FILE_201.txt
FILE_206.txt
FILE_215.txt
FILE_223.txt
FILE_229.txt
FILE_232.txt
I want to select files between two values (e.g. between "210" and "230").
From the command line, running
ls -1 FILE_{210..230}.txt
lists the desired files; no problem. However, if I run the following Bash script,
#!/bin/bash
ls -1 FILE_{$1..$2}.txt
passing 210 and 230 as arguments to it, it doesn't work.
What am I doing wrong?
The problem isn't about running the command at the command line or from a script. You can check that for yourself, by running the following at the command line:
$ set 210 230 # assigns variables $1 and $2 the values "210" and "230", resp.
$ ls -1 FILE_{$1..$2}.txt
ls: FILE_{210..230}.txt: No such file or directory
As you can see, you get the same error as the one raised by your script file. The problem is that brace expansion happens before variable expansion.
In this case, by the time, $1 and $2 get expanded to 210 and 230, it's too late for the range to be interpreted as such; the { .. } part simply sticks around, without any special meaning attached to it.
Anyway, using ls here is a bad idea, because you're going to get an error for each filename in the range that doesn't match an existing file. You'd be better off using a combination of a for loop and seq, as in this answer:
#!/bin/bash
# test.sh
for n in $(seq $1 $2); do
f="FILE_$n.txt"
if [ -e $f ]; then
printf $s\\n "$f"
fi
done
Example:
$ touch FILE_209.txt
$ touch FILE_213.txt
$ touch FILE_224.txt
$ touch FILE_232.txt
$ bash test.sh 210 230
FILE_213.txt
FILE_224.txt
In case anyone is interested, you need to "eval" your line. i.e.
eval "ls FILE_{$1..$2}.txt

Resources