Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bourne Shell For i in (seq)

I want to write a loop in Bourne shell which iterates a specific set of numbers. Normally I would use seq:

for i in `seq 1 10 15 20`
   #do stuff
loop

But seemingly on this Solaris box seq does not exist. Can anyone help by providing another solution to iterating a list of numbers?

like image 801
Chris Kannon Avatar asked Jan 20 '10 15:01

Chris Kannon


People also ask

What is Bourne shell used for?

The Bourne shell is used for scripting. It provides command-based programming to interpret and execute user commands. As a user types a command, the shell interprets it so the operating system can take action, such as automating a task. The Bourne shell was the default shell for Unix version 7.

What is SEQ in shell?

seq command in Linux is used to generate numbers from FIRST to LAST in steps of INCREMENT. It is a very useful command where we had to generate list of numbers in while, for, until loop.

What is Bourne shell syntax?

The "shbang" line is the very first line of the script and lets the kernel know what shell will be interpreting the lines in the script. The shbang line consists of a #! followed by the full pathname to the shell, and can be followed by options to control the behavior of the shell.

What is Bourne shell command interpreter?

The Bourne shell is an interactive command interpreter and command programming language. The bsh command runs the Bourne shell. The Bourne shell can be run either as a login shell or as a subshell under the login shell. Only the login command can call the Bourne shell as a login shell.


1 Answers

try

for i in 1 10 15 20
do
   echo "do something with $i"
done

else if you have recent Solaris, there is bash 3 at least. for example this give range from 1 to 10 and 15 to 20

for i in {1..10} {15..20}
do
  echo "$i"
done

OR use tool like nawk

for i in `nawk 'BEGIN{ for(i=1;i<=10;i++) print i}'`
do
  echo $i
done

OR even the while loop

while [ "$s" -lt 10 ]; do s=`echo $s+1|bc`; echo $s; done
like image 50
ghostdog74 Avatar answered Sep 23 '22 05:09

ghostdog74