|
|
|
Squaring Shell Script Problem
|
Original Message
|
Name: Jen Cheng
Date: May 28, 2003 at 03:24:42 Pacific
Subject: Squaring Shell Script ProblemOS: Win XP Pro SP1CPU/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
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: David Perry
Date: May 28, 2003 at 04:23:29 Pacific
|
Reply: (edit)#!/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
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
|
Reply: (edit)$# 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
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
|
Reply: (edit)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"
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Jen Cheng
Date: June 3, 2003 at 08:12:01 Pacific
|
Reply: (edit)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
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: Jen Cheng
Date: June 3, 2003 at 10:57:25 Pacific
|
Reply: (edit)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
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|