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
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 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.
This question already has answers here:
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
Closed 5 years ago.
I am using MacBook pro terminal to execute a shell script. It loops through a text file and create filenames based on each line in the file.
#!/bin/bash
year=2010
list=list_test.txt
mydir=thisdir
i=1 # counter
while read line
do
echo $i $line
file1=`echo $mydir/file_$year_$line_test.tif`
file2=`echo $mydir/file_$year_$line_test.tif`
echo $file1 $file2
i=$(($i+1))
done < $list
However, the output is peculiar:
1 17019
thisdir/file_.tif thisdir/file_.tif
2 17029
thisdir/file_.tif thisdir/file_.tif
3 17039
thisdir/file_.tif thisdir/file_.tif
Within the loop, bash does not recognize some variables, like "year" which is a global, and "line" which is read from the text file. The text file is as below:
17019
17029
17039
Another script with exactly the same manner works very well. This is mysterious to me now.
Any help or comments are extremely appreciated! Thanks very much!
_ is a valid character for an identifier, but you want to use it as a literal character in the file name. You need to use the full form of parameter expansion, ${x} instead of $x.
(Also, the command substitution isn't necessary.)
file1=$mydir/file_${year}_${line}_test.tif
file2=$mydir/file_${year}_${line}_test.tif
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
This question already has answers here:
reverse the order of characters in a string
(14 answers)
Closed 9 years ago.
I want to write a shell script that accepts a string as a command line argument and prints out what is entered in the reverse order with out using the rev command but rather reversing the letters one by one. how would I do that?
so like
if Flower is entered it will print out rewolF
Thanks
A good subject for bash parameter expansion :
#!/bin/bash
for ((i=${#1}; i>=0; i--)); do printf "${1:$i:1}"; done; echo
Example :
./script.sh foobar
Output :
raboof
s1='Flower'
a1=($(echo $s1|fold -w1 ))
for (( i=${#a1[#]}-1;i>=0;i--));do echo "$i=>${a1[i]}"; done
5=>r
4=>e
3=>w
2=>o
1=>l
0=>F