This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I am trying to write a shell script that uses wget to download files in bulk from urls that follow a certain numeric pattern.
Understandably, the url from the user input must contain the variable $i.
dl.sh http://some/url/$i/some/url 1 9
This yields repeated result from the final loop because $i will be expanded before passing down into the loop.
http://some/url/9/some/url
http://some/url/9/some/url
...
http://some/url/9/some/url
Is there a workaround to get this shell script working?
Source Code:
#!/bin/bash
# dl.sh url | index_from | index_to
for i in $(seq $2 $3)
do
echo ${1} # replace with wget for actual download.
done
Expected Result:
http://some/url/1/some/url
http://some/url/2/some/url
http://some/url/3/some/url
...
http://some/url/9/some/url
Related
This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
Closed 5 months ago.
I have asked way too many dumb questions here, but here we go.
Code:
#!/bin/bash
for i in {0..$#}; do
ascii ${!i} | grep "\`" | tail -c 3 | head -c 1;
done
echo
(This is to translate long binary input, like from those horror games that have binary at the end)
Error message:
./asciit: line 4: {0..2}: invalid variable name
Command run:
./asciit 01101000 01101001
Why?
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 8 months ago.
I have a script.sh
#!/bin/bash
for i in {1..$1}
do
echo $i
done
someone could explain me why if I try
./script.sh 10 my output is {1..10}?
expected
1
2
...
10
You're echoing the parameter that you're passing in instead of printing out the loop iteration. Try:
echo $i
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
How do i store the output of a bash command in a variable? [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to get the number of lines that was printed from the ps command, and save to a variable.
COUNT_PS= ps -C $NAME | wc -l)
the line above prints me 2, but COUNT_PS unfortunately still equals 0 (at the start of the script I've assigned COUNT_PS as 0).
It actually prints the value - but doesn't save it to COUNT_PS.
What am I doing wrong?
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Bash script store command output into variable
(2 answers)
Save output in variable? [duplicate]
(2 answers)
Store output of sed into a variable [duplicate]
(4 answers)
Assigning the output of a command to a variable [duplicate]
(3 answers)
Closed 4 years ago.
suppose in a file if I have " Route : 8888" , I want to get the string after the pattern "Route :"
I have used sed command, but it is printing 8888 but I want to store it into a variable and use for further processing
So store sed's output in a variable:
ROUTE_VALUE=$(sed ... <file.txt)
Where '...' is your sed command.
$ cat rt.sh
ROUTE=`sed -n 's/.*Route = \(.*\),.*/\1/p' data.txt`
echo $ROUTE
$ bash rt.sh
8888
$ source rt.sh
8888
This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop