0

I have a script that takes two numbers both are 6 digit e.g 220210 and 220221.

All I want is a loop to write all numbers between 220210 and 220221 and these 2 numbers into a file.

I know its probably dead simple but its been doing my head in.

HP-UX 11.23 bash

Steve
  • 41
  • 2
  • 1
    `seq 220210 220201` to get numbers, writing to a file is left as exercice ;) – Archemar Sep 10 '20 at 13:01
  • You're missing `-1` to `seq` when first number is the bigger one. Alternative. `printf '%s\n' {220210..220201}` – pLumo Sep 10 '20 at 13:14
  • I get bash: seq: command not found. – Steve Sep 10 '20 at 13:15
  • it should have been 220210 and 220221 so I'm hoping for 220210 220211 220212 220213 220214 220215 220216 220217 220218 220219 220220 220221 all on separate lines – Steve Sep 10 '20 at 13:24
  • Related: [Portable POSIX shell alternative to GNU seq(1)?](https://unix.stackexchange.com/questions/298199/portable-posix-shell-alternative-to-gnu-seq1) – steeldriver Sep 10 '20 at 15:57

5 Answers5

0

Not sure if this will work in your bash version, since it doesn't seem the seq command is available, but your could try with a for range like

#!/bin/bash
for n in {220210..220221}
 do
  echo $n >> numberfile.txt 
done
0

You can do this using the desk calculator dc utility. Load the two range numbers in any order. Only remember to change the sign of a negative number as underscore instead of dash. So fir example, -32 will go in as _32.

dc <<EOF
220201 220210
[larsa]sb
[lap1+sadla<+]s+
sadla>bla
sal+xp
EOF

Results:

220201
220202
220203
220204
220205
220206
220207
220208
220209
220210

Yet another method is to use the bc binary calculator utility. As you can see, load the numbers in any order the bc will flip them if required and start looping from smaller to larger in steps of 1.

echo "a=7;b=0;if(a>b){t=a;a=b;b=t;};for(i=a; i<=b; i++) {i;}" | bc
Rakesh Sharma
  • 1,102
  • 1
  • 4
  • 3
0

To write out all the integers in the range [220210,220221] to a file using the bash shell (or any shell that has brace expansions):

printf '%s\n' {220210..220221} >file

What's happening here is that the shell will expand the brace expansion {220210..220221} to the list of all the integers in the range. These will then be used as arguments to the printf command.

The printf command outputs its arguments in accordance with its format string (the 1st argument). The format string %s\n tells printf to output each argument as a string with a trailing newline. Since you're outputting integers, you could also have used %d\n, but it would have made no difference.

The redirection >file tells the shell to put all output of the printf command into the file called file. The file will be created or, if it already exists, it will first be truncated (emptied).


If you're using a shell that does not understand brace expansions, then the above would just print {220210..220221} on a line.

In such a shell, you may want to do an explicit loop:

n=220210
while [ "$n" -le 220221 ]; do
    printf '%s\n' "$n"
    n=$(( n + 1 ))
done

This creates a variable n that we print and increment in each iteration. The initial value of the variable is set to 220210 before the loop and the loop terminates whenever $n reaches 220222 (that value won't be printed).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
0

The proper way to do this would be to use seq or brace expansion as other answers have shown. If, for some reason, you cannot and need to do it in a simple, POSIX shell, you could try:

#!/bin/sh

i=$1;

while [ $i -le $2 ]; do
  echo "$i"
  i=$((i+1))
done

A slightly more modern (but less portable, non-POSIX) syntax would be:

#!/bin/bash

for((i=$1;i<=$2;i++)); do
  echo "$i"
done
terdon
  • 234,489
  • 66
  • 447
  • 667
0

awk is in almost every distro these last 30 years, and this is basic K&R awk.

echo 220210 220221 | awk '{ for (j = $1; j <= $2; j++) print j }'
Paul_Pedant
  • 8,228
  • 2
  • 18
  • 26