Computing.Net > Forums > Unix > Squaring Shell Script Problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Squaring Shell Script Problem

Reply to Message Icon

Name: Jen Cheng
Date: May 28, 2003 at 03:24:42 Pacific
OS: Win XP Pro SP1
CPU/Ram: P4 2.53 GHz / 512 MB DDR
Comment:

For reference please see post:
http://www.computing.net/linux/wwwboard/forum/18683.html

The problem was:

Write a shell script that accepts a number from the user and displays the squares of all numbers from 1 up to that number as follows:
1 square=________
2 square=________
:
:
n square=________
(n is the number entered)

The accepted answer to this problem was:

#!/bin/ksh
if [ $# -gt 0 ] ; then
i=1
square_cnt=$1
while [ $i -le $square_cnt ] ; do
((square = $i * $i ))
echo "$i square = $square"
((i = i + 1))
done
else
echo "ERROR. Please enter an integer"
fi

My questions are:
1) Could someone please explain what each line of code is doing?
2) Is there another way of doing this, maybe not better, but easier to understand, geared towards a newbie?

Thanks,
JC



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: May 28, 2003 at 04:23:29 Pacific
Reply:

#!/bin/ksh
# shell declaration line. Use the shell interpreter located at /bin/ksh
if [ $# -gt 0 ] ; then
# check for the number of command line arguements being passed to this script. Proceed if it is greater than 0
i=1
# assign the value of 1 to the variable $i
square_cnt=$1
# assign the value of the first command line parameter to the variable $square_cnt
while [ $i -le $square_cnt ] ; do
# do something while $i is less than the value of $square_cnt
((square = $i * $i ))
# assign to the variable $square the results of $i x $i
echo "$i square = $square"
# print the results to the screen
((i = i + 1))
# add 1 to the value of $i
done
# close the while loop
else
# do this if the command line parameter count is not greater than 0
echo "ERROR. Please enter an integer"
fi
# end if



0

Response Number 2
Name: WilliamRobertson
Date: May 28, 2003 at 04:25:03 Pacific
Reply:

$# gives the number of command-line arguments ($1, $2, $3 etc). For example if you run:

myscript banana piano 42

then within the script,
$0 = /some/path/myscript
$1 = "banana"
$2 = "piano"
$3 = "42"
$* = "banana piano 42"
$# = 3

Expressions in single square brackets [] are Bourne shell test expressions. Among the available operators are -gt, -lt and -le meaning "greater than", "less than" and "less than or equal to" respectively. For the full list type "man test".

In Korn shell you can declare integer variables and use the special double round bracket (()) syntax to perform integer arithmetic. Within arithmetic expressions names are interpreted as references to variables so you can skip the "$" prefix. (Actually these days most of this is also available in Bourne shell, but I just like ksh.)

In my ksh version I have moved the argument count validation up front.

#!/bin/ksh

integer i=1 square_cnt=$1

if (( $# > 1 )); then
echo "ERROR. Please enter an integer"
exit 1
fi

while (( i <= square_cnt )); do
(( square = $i * $i ))
print -u2 "$i square = $square"
(( i += 1 ))
done


0

Response Number 3
Name: WilliamRobertson
Date: May 28, 2003 at 04:48:26 Pacific
Reply:

Oops,

> print -u2 "$i square = $square"

should have been

> print "$i square = $square"

print is the ksh equivalent of echo. One of its useful features is the -u2 option which sends output to standard error, i.e. still displayed to the screen, but can be captured separately for error logging etc. (Strictly speaking it is -un, where n represents a file descriptor, but file descriptors higher than 2 get complicated and in my experience are rarely useful.) Therefore I use print for normal output and print -u2 for error messages. In Bourne shell you would have to use "echo Error Message >&2"



0

Response Number 4
Name: Jen Cheng
Date: June 3, 2003 at 08:12:01 Pacific
Reply:

Okay here are the steps I've taken:

I write clicked on Desktop and created a new text file named "script"

I then filled script with the following text:
#!/bin/ksh
if [ $# -gt 0 ] ; then
i=1
square_cnt=$1
while [ $i -le $square_cnt ] ; do
((square = $i * $i ))
echo "$i square = $square"
((i = i + 1))
done
else
echo "ERROR. Please enter an integer"
fi

Now, how do I run this. I tried by opening up a shell and going to Desktop and typing "script 13" so it would square up to 13, but all it did was say: "Script started, file is 13" What am I doing wrong? Also, how would I make this a bash script?

Thanks,
JC


0

Response Number 5
Name: Jen Cheng
Date: June 3, 2003 at 10:57:25 Pacific
Reply:

I figured it out, open a command window and type: ksh script #, works like a charm the final code I went with was:

# shell declaration line. Use the shell interpreter located at
# /bin/ksh
#!/bin/ksh

# assign the variables values
integer i=1
square_cnt=$1

# check to see if the variable is greater than zero, if not
# output error message
if (($# > 1)); then
echo “ERROR: Please enter an integer.”
exit 1
fi

# 1. do something while $i is less than the value of
# $square_cnt
# 2. assign the variable $square the results of $i * $i
# 3. print the results to the screen
# 4. add 1 to value of $1
# 5. close the while loop
while (( i = square_cnt )); do
(( square = $i * $i ))
echo “$i square = $square”
(( i = i + 1 ))
done


0

Related Posts

See More



Response Number 6
Name: Jen Cheng
Date: June 3, 2003 at 11:20:38 Pacific
Reply:

2 things:

1) The error message is not working, when I run something like -2 nothing happens.
2) How would I do this in bash?


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Squaring Shell Script Problem

Shell script problem www.computing.net/answers/unix/shell-script-problem/4434.html

korn shell script problem (using awk ? ) www.computing.net/answers/unix/korn-shell-script-problem-using-awk-/3547.html

Shell script problem www.computing.net/answers/unix/shell-script-problem/2479.html