Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.
# 1  
Old 11-10-2011
Data Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.

Code:
for (( i=1; i<=3; i++ )); do
for (( j=1; j<=3; j++ )); do
for (( k=1; k<=3; k++ )); do
echo $i$j$k
done
done
done

Will the above code work on a BOURNE shell?

As far as my understanding is, if I am writing the above code in a file..say lol.sh and then running it through the terminal using the command

sh lol.sh

then I am using the Bourne Shell. Am I right with this concept?

If yes, then the above code won't run (as I tried to run it) BUT it runs when i run it as...

bash lol.sh

So my question is that :

1. Am I correct that sh lol.sh will run it using bourne shell ?

2. And can for loop be written in the way it's written in the code above in the bourne shell or
Code:
for i in 1 2 3

is the only possible syntax of for loop in the bourne shell?
# 2  
Old 11-10-2011
No, that will not work in a generic Bourne shell. Generic Bourne shells don't even have math, you have to use the expr external.

The latter is what should be used for a generic bourne shell, yes. Or something like

Code:
I=1
while [ $I -le 3 ]
do
        I=`expr $I + 1`
done

# 3  
Old 11-10-2011
If bourne shells doesn't have math then how am I able to execute statements like
Code:
 var=$(($1-1))

And is it possible to find x to the power of y using expr function (or in any possible way in bourne shell except "for" loop) where x and y are two numbers read from the terminal?
# 4  
Old 11-10-2011
Quote:
Originally Posted by navienavnav
If bourne shells doesn't have math then how am I able to execute statements like
Code:
 var=$(($1-1))

By using the expr external, like I said.

Code:
VAR=`expr $VAR - 1`

Quote:
And is it possible to find x to the power of y using expr function (or in any possible way in bourne shell except "for" loop) where x and y are two numbers read from the terminal?
Certainly, by running a loop which multiplies x against itself y times.

Is this homework?
# 5  
Old 11-10-2011
Thank you for your time but if you would read my last post carefully and not skim over it... you'd see that I said "except for loop" which implies that I already know how to do it with a for loop but I was asking you for some other method "if it exits".

Second thing, I KNOW that it can be done by the expr command but I asked you that WHY I am getting away with using a command like $((...mathematical expression...)) if I am working in the bourne shell WHICH implies that I ALREADY understood that expr can be used.
I do understand if "...am i able to..." couldn't really pass my message along clearly but I think "....except for loop...." was mentioned pretty neatly.

Again, thank you for your time and effort but kindly re read the post before tagging my genuine problem as "homework".

Last edited by navienavnav; 11-10-2011 at 11:59 AM..
# 6  
Old 11-10-2011
On some systems Bourne shell (sh) is actually Bash (bash).
Compare the sizes/links of /usr/bin/sh and /usr/bin/bash

As far as power is concerned:
Code:
$ nawk 'BEGIN{ print 2^3}'
8


Last edited by vgersh99; 11-10-2011 at 12:33 PM..
# 7  
Old 11-10-2011
A simple "no" would have sufficed. I didn't "tag" your post as homework, I asked.

Most of the time I ask, the answer is "yes". Your question looked contrived, as homework frequently is, because calculating X^Y isn't something that makes much sense to do in a shell. If it was homework, I'd have been breaking the rules by giving you an answer. I've been burned several times by blatant cheaters, too.

If I ask someone what their shell is, and they tell me "Bourne", I ask "which Bourne" because "bourne" means it supports at least the Bourne shell features, not that it's the One and Only True Original Bourne Shell. Some systems -- particularly Linux ones -- use Bash as their Bourne shell, many others use KSH, and a few poor Solaris folks are stuck using an actual ancient pre-POSIX Bourne shell which hasn't been improved since the dawn of UNIX.

If $(( )) works, you're either in bash or ksh.

I don't know a simple syntax for taking a number to a power in either. If I had to do so in basic Bourne, I'd probably pipe "$x^$y" into bc. If you try and do that in $(( )), it's a bitwise exclusive-or, not x to a power, since $(( )) mostly emulates the C expression syntax, and C has no operator for power.

Last edited by Corona688; 11-10-2011 at 12:36 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop in bourne shell is not working

I have a loop with cases I am working on Bourne shell for file in *.${Today}*.csv *.${Today}*.txt\ do case ${file} in sun_detail) do something ;; sum) do something ;; mod) do something ;; *) do something ;; (5 Replies)
Discussion started by: digioleg54
5 Replies

2. Shell Programming and Scripting

Bash Shell loop - Help !

Dear all Linux lover, I am a new learner to Bash Shell script and I would like to writing a script to to repeat my script. This mean I would like to have multiple same of result after running the .sh. ####### TIMES_NO=0 echo -n "Please enter the number for times to repeat ?" read... (10 Replies)
Discussion started by: Rocky888
10 Replies

3. UNIX for Dummies Questions & Answers

For loop in bash shell

Hi, I am using a for loop to manipulate files data_1.txt through data_100.txt. The for-loop is set up like this: for i in {1..100}; do cut -f1 data_$i.txt > output$i.txt I get the following error message when I run the code: cannot open `data.txt' for reading: No such file or directory... (4 Replies)
Discussion started by: evelibertine
4 Replies

4. Shell Programming and Scripting

Bash (Ubuntu server): Syntax error: "|" unexpected in While-loop

Hello forum, I hope my problem is easy to solve for someone in here! My main task is to copy a large amount of imap-accounts from one server to another. There is a tool (Perl) called imapsync which does the job exellent. Unfortunately I'm only able to run it on one account at a time. After... (3 Replies)
Discussion started by: primaxx
3 Replies

5. UNIX for Dummies Questions & Answers

If Then ElseIf Script - Confusion Around Expression's Syntax

Hello, I am relatively new to UNIX scripting and am learning a lot. I have already tried several searches on this website and have tried various syntax options suggested to no avail. I am obviously not writing the script correctly. I really do appreciate any and all the help. Below is an... (8 Replies)
Discussion started by: dqrgk0
8 Replies

6. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

7. Shell Programming and Scripting

Help on "for" loop in bourne shell

Hello Everyone.... I am trying to print a number sequence in following format using for loop. I am using a bourne shell. I tried following for loop condition but it is bash syntax. for (( i=0; i<=5; i++ )) It is giving syntax error. Kindly help with the syntax of "for"... (7 Replies)
Discussion started by: EmbedUX
7 Replies

8. Shell Programming and Scripting

I need to understand the differences between the bash shell and the Bourne shell

I do not claim to be an expert, but I have done things with scripts that whole teams of folks have said can not be done. Of course they should have said we do not have the intestinal fortitude to git-r-done. I have been using UNIX actually HPUX since 1992. Unfortunately my old computer died and... (7 Replies)
Discussion started by: awk_sed_hello
7 Replies

9. Shell Programming and Scripting

problem with while loop in BASH shell

I have file named script1 as follows: #!/bin/bash count="0" echo "hello" echo "$count" while do echo "$count" count=`expr $count + 1` done ----------- when I run it, I get ./script1: line 9: syntax error near unexpected token `done' ./script1: line 9: `done' I... (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

if loop problem in bourne shell

how to use if-loop in bourne shell with multiple conditions like follows if then commands fi it gives me an error test: ] missing then i put if ] it gives me an error [[ not found kindly i need the syntex for the bourne shell (5 Replies)
Discussion started by: ahmad.diab
5 Replies
Login or Register to Ask a Question