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?
Related
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:
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
This question already has answers here:
Why should eval be avoided in Bash, and what should I use instead?
(3 answers)
Variables as commands in Bash scripts
(5 answers)
Closed 5 years ago.
Command="tr ' ' '\n' < /location/to/file.xml | sed -e '/\/Name/, $d' | grep Apple | wc -l"
New_command=`$Command`
The error I am getting:
tr: extra operand \'\\n\'' Trytr --help' for more information.
What is wrong with what I am doing? Trying to assign a string variable to 'New_Command' so I can execute it and get the output.
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