I am writing a script which accepts two arguments:
#! /bin/bash
eval for i in {$1..$2}; do echo $i; done
I run it like:
$ ./myscript 0002 0010
syntax error near unexpected token `do'
Why is the error?
I though it might be because the looping should be grouped. But by replacing eval for i in {$1..$2}; do echo $i; done
with eval { for i in {$1..$2}; do echo $i; done; }
, the error remains.
Note:
I hope to perform parameter expansion before brace expansion by using eval
.
The desired output of my example is 0002 0003 0004 0005 0006 0007 0008 0009 0010
. (See Perform parameter expansion before brace expansion?)
seq
can accomplish the same thing without the security issues ofeval
which, in this case can be harmlessly demonstrated by running the script with two carefully chosen arguments like./myscript.sh 02 '0010}; do echo "Whats for lunch?"; done; touch newfile; for i in {1..2'
– John1024 Apr 23 '16 at 02:19seq
isn't the right solution here either. The right thing is to use a numerical loop:for ((i=$1; i<=$2; i++))
in bash ori=$1; while [ "$i" -e "$2" ]
in sh. – Gilles 'SO- stop being evil' Apr 23 '16 at 14:31