1834137 Members
2315 Online
110064 Solutions
New Discussion

simple script

 
SOLVED
Go to solution
Inter_1
Frequent Advisor

simple script

Hi

I want to create such a script script.sh that for every two different numbers X and Y when X>Y it can do
X
X+1
X+2
...
Y

up to X=Y

e.g.
./script 5 9
it should returns
5
6
7
8
9

That should happen for every 2 different numbers.

Thanks
Andy
I like to fix things.
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: simple script

Hi Andy:

# awk 'END {for (i=X; i<=Y; i++) {print i}}' X=5 Y=9 /dev/null

...specify any value of 'X' and 'Y' you want.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: simple script


perl -le 'print for (shift..shift)' 5 9

-l : print a linefeed after each print
-e : command text coming
print : prints default variable $_
for (x..y) : load default variable $_ with values from x through y in a loop
shift : grab next command line argument

Enjoy,
Hein.

A. Clay Stephenson
Acclaimed Contributor

Re: simple script

and if you want a pure shell solution:

#!/usr/bin/sh

if [[ ${#} -lt 2 ]]
then
echo "Usage: ${0} lo hi" >&2
exit 255
fi
typeset -i LO=${1}
typeset -i HI=${2}
shift 2
while [[ ${LO} -le ${HI} ]]
do
echo "${LO}"
((LO += 1))
done
exit 0

If it ain't broke, I can fix that.
Sandman!
Honored Contributor
Solution

Re: simple script

Yet another shell script solution:

#!/usr/bin/sh

echo $* | awk '{for(i=$1;i<=$NF;++i) print i}'
Arturo Galbiati
Esteemed Contributor

Re: simple script

Hi Andy,
use the function range:
function range
{
#######################################################
# Shows all numbers from start_range to end_range
[[ $# -lt 1 || $# -gt 2 ]] && echo "Usage: range start_range and_range" && return
lo=$1
hi=$2
while [ $lo -le $hi ]
do
echo "$lo \c"
lo=$(expr $lo + 1)
done
echo
unset lo hi
}

range 1 5 9
HTH,
Art
Inter_1
Frequent Advisor

Re: simple script

Thanks guys
I like to fix things.