Go Back   The UNIX and Linux Forums > Homework and Emergencies > Homework & Coursework Questions


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-23-2012
Registered User
 
Join Date: Jun 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Filter command arguments

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

Execute command with integer list arguments such as myScript 8 7 2 3.
Take those arguments and square all of them one by one, each iteration print out the result. At the end print out the total sum of the squared values.

2. Relevant commands, code, scripts, algorithms:

Code:
# Error handling if the argument list is empty or contains string instead of integers.

if [ $# = 0 ]
then
        echo "Usage $0 -integer-list"
        exit 1
elif [ $@ = [^9-0] ]
then
        echo "No Strings are allowed."
fi

I want to output an error message when characters are found inside the argument list.


3. The attempts at a solution (include all code and scripts):

Code:
#!/bin/sh
# File:                         power
# Author:                      
# Student ID:                   
# Created:                      6/23/2012
# Purpose:                      
# Description:                  This program uses the integer list provides
#                               as command arguments and evaluates the square 
#                               numbers of all of them. At the end of the 
#                               script the sum of all the squared numbers is 
#                               printed to the screen
#
#
#
#


# Error handling if the argument list is empty or contains string instead of integers.

if [ $# = 0 ]
then
        echo "Usage $0 -integer-list"
        exit 1
elif [ $@ = [^9-0] ]
then
        echo "No Strings are allowed."
fi

total=0
power=0

# Function used for summation of the squared number list.
sum()
{
        total=`expr $total + $power`    # Sum the total value.
}

# Iterates through the argument list.
for num in $@
do
        power=`expr $num \* $num`               # Evaluate the square for the current argument.
        echo "$num squared equals $power"       # Print to screen the square of the argument.
        sum             # Call the sum function to sum the square values togehter.      
done

# Prints the result of the sum function
echo "Total of squared numbers = $total"

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Athabasca University, Athabasca, Alberta, Canada, Prof. Mamoud Abaza, COMP325

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Sponsored Links
    #2  
Old 06-25-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,292
Thanks: 27
Thanked 450 Times in 351 Posts
Congrats! You are almost there and you lack only small things.

Here are some pointers:

1. You haven't said anything about the shell you are using but somehow i doubt you are using a Bourne shell. More likely "/bin/sh" is a POSIX shell, a bash (Bourne Again Shell) or a ksh (Korn Shell). All these shells have an arithmetic facility and while what you do is syntactically correct using "expr" like you do is ancient, antiquated, deprecated and ultimately discouraged. See the "(( ... ))" in the man page of your shell and you will understand why.

2. The same goes for backticks: modern shells understand these only for purposes of backwards compatibility and using them has no advantages but a lot of disadvantages. Have a look in the man page for "$( ... )" as a device to execute a command in a subshell.

3. Your handling of the "num" variable: All usual shells allow variables to be used without having to declare them (unlike languages like C or PASCAL), but it is still good style to declare properly what you use prior to using it. Again, what you do is syntactically correct, but .... Using a line like


Code:
typeset -i num=0              # buffer to loop through arguments
typeset -i total=0            # buffer to sum up results

at the start of the script allows you to: give the variables a proper type, a sensible starting value and - even more important - to document their usage. If you look later at your code you will immediately know what you did by simply reading the comments.

OK, lets talk about this, then go on as you correct these errors. I will expand on this later.

I hope this helps.

bakunin
Sponsored Links
    #3  
Old 07-10-2012
Registered User
 
Join Date: Jun 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
..All these shells have an arithmetic facility and while what you do is syntactically correct using "expr" like you do is ancient, antiquated, deprecated and ultimately discouraged. See the "(( ... ))" in the man page of your shell and you will understand why.
The reason I use the expr command is because I have to; that is part of the assignment.

All I want to do is to use reg expressions to check whether the user typed in strings instead of integers. It seems that I don't get the regular expression to work in my control flow statements. Thats all I want to do
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using tail with the filter command hbell221 Homework & Coursework Questions 8 05-05-2012 06:27 AM
filter grep command LRoberts Shell Programming and Scripting 7 10-24-2009 01:59 AM
Help in Find command filter test files TonySolarisAdmi UNIX for Dummies Questions & Answers 4 08-22-2008 02:55 PM
how to filter `last` command for yesterday only skully Shell Programming and Scripting 1 06-26-2008 04:30 AM
get whole command arguments in ps -ef? usfrog HP-UX 17 04-04-2007 12:11 PM



All times are GMT -4. The time now is 08:45 AM.