Sponsored Content
Top Forums Shell Programming and Scripting checking variables + arithmetic operator help Post 302578520 by agama on Thursday 1st of December 2011 08:29:19 PM
Old 12-01-2011
The variable $* expands to each parameter from the command line. You want $# which is the number of parameters.

As for you example code, you don't need the echo nor the back ticks:

Code:
total=0
n=$#
while [[ -n $1 ]]
do
   total=$(( total + $1 ))
   shift
done
echo "total=$total  average=$(($total/$n))"

Bash cannot do floating point, but kshell can, so you can change the last line to the following if you want to use kshell and have a more percise average:

Code:
echo "total=$total  average=$(($total/$n.0))"

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking variables

Firstly - aplogies to Vino I need to check wether or not a variable called DATE is actually a date by doing the following - checking it is all numeric and secondly checking if it is 8 digits long. Any help appreciated (10 Replies)
Discussion started by: penfold
10 Replies

2. Shell Programming and Scripting

Checking if file exists using a NOT operator and shell variable

Hi, I'm new to UNIX, at least shell programming and am having trouble figuring out a problem i'm having. In one section in my nested if statement, i want the program to test if the file does not exist, based on an argument supplied at the command line by the user. What i have is elif ; then... (3 Replies)
Discussion started by: rowlf
3 Replies

3. Shell Programming and Scripting

Need help with arithmetic expression using variables

Hi, I am trying to do a percentage calculation in a Bourne shell script. The following works fine, but when I try to subsitute the values 153824 and 246000 for variables (e.g. used and limit), I get errors. calc() { awk 'BEGIN {OFMT="%f"; print '"$*"'; exit}' } calc '(153824 /... (3 Replies)
Discussion started by: shorehil
3 Replies

4. UNIX for Dummies Questions & Answers

syntax error: invalid arithmetic operator

hi, i have a bash script that i want to receive a a string from another bash file. But because the string has a dot in the middle it gives me an error. The error is in this line: let valor=$1 and the value passed is rules.txt the error is: let: valor=rules.txt: syntax error: invalid... (2 Replies)
Discussion started by: limadario
2 Replies

5. Shell Programming and Scripting

Help with checking that 2 variables contain matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario eg ... (3 Replies)
Discussion started by: lsecer
3 Replies

6. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

7. Shell Programming and Scripting

PAssing variables to awk arithmetic

Hi all, I am wanting to pass variables from a file to an awk arithmetic formula. When I use the formula with the value it works well. As soon as I make these variables I get an inf (infinity) response. I can certainly echo the variables back and they look correct. My googling for answers has... (3 Replies)
Discussion started by: gafoleyo73
3 Replies

8. UNIX for Dummies Questions & Answers

Invalid arithmetic operator

Hi experts, I'm facing trouble with the below bash script, any help is appreciated #!/usr/bin/bash .. if (( "${date1}" > "${l_date1}" )) then echo "success" else echo "nothing" fi the variable date1 contains 08/06/2013 and l_date1 contains 10/08/2013 this small script to... (6 Replies)
Discussion started by: parpaa
6 Replies

9. Shell Programming and Scripting

BC calculation for floating (invalid arithmetic operator )

Hi, I wish to compare the CPU LOAD 1 min with 5mins and 15mins. If 1 min's CPU LOAd spike 3% compare to 5 mins or 15 mins CPU Load, it is warning. If 1 min's CPU LOAd spike 5% compare to 5 mins or 15 mins CPU Load, it is critical. However I received following code error, I google it and... (10 Replies)
Discussion started by: alvintiow
10 Replies

10. UNIX for Beginners Questions & Answers

Invalid arithmetic operator on string concatenation

Hello. LEAP_VERSION="4.2" export ARRAY_MAIN_REPO_LEAP=('zypper_local' 'openSUSE-Leap-'"$LEAP_VERSION"'-Non-Oss' 'openSUSE-Leap-'"$LEAP_VERSION"'-Oss' 'openSUSE-Leap-'"$LEAP_VERSION"'-Update' 'openSUSE-Leap-'"$LEAP_VERSION"'-Update-Non-Oss')Seems that the - is interpreted as a numeric... (2 Replies)
Discussion started by: jcdole
2 Replies
LOCK_PROFILING(9)					   BSD Kernel Developer's Manual					 LOCK_PROFILING(9)

NAME
LOCK_PROFILING -- kernel lock profiling support SYNOPSIS
options LOCK_PROFILING DESCRIPTION
The LOCK_PROFILING kernel option adds support for measuring and reporting lock use and contention statistics. These statistics are collated by ``acquisition point''. Acquisition points are distinct places in the kernel source code (identified by source file name and line number) where a lock is acquired. For each acquisition point, the following statistics are accumulated: o The longest time the lock was ever continuously held after being acquired at this point. o The total time the lock was held after being acquired at this point. o The total time that threads have spent waiting to acquire the lock. o The total number of non-recursive acquisitions. o The total number of times the lock was already held by another thread when this point was reached, requiring a spin or a sleep. o The total number of times another thread tried to acquire the lock while it was held after having been acquired at this point. In addition, the average hold time and average wait time are derived from the total hold time and total wait time respectively and the number of acquisitions. The LOCK_PROFILING kernel option also adds the following sysctl(8) variables to control and monitor the profiling code: debug.lock.prof.enable Enable or disable the lock profiling code. This defaults to 0 (off). debug.lock.prof.reset Reset the current lock profiling buffers. debug.lock.prof.acquisitions The total number of lock acquisitions recorded. debug.lock.prof.records The total number of acquisition points recorded. Note that only active acquisition points (i.e., points that have been reached at least once) are counted. debug.lock.prof.maxrecords The maximum number of acquisition points the profiling code is capable of monitoring. Since it would not be possible to call malloc(9) from within the lock profiling code, this is a static limit. The number of records can be changed with the LPROF_BUFFERS kernel option. debug.lock.prof.rejected The number of acquisition points that were ignored after the table filled up. debug.lock.prof.hashsize The size of the hash table used to map acquisition points to statistics records. The hash size can be changed with the LPROF_HASH_SIZE kernel option. debug.lock.prof.collisions The number of hash collisions in the acquisition point hash table. debug.lock.prof.stats The actual profiling statistics in plain text. The columns are as follows, from left to right: max The longest continuous hold time in microseconds. wait_max The longest continuous wait time in microseconds. total The total (accumulated) hold time in microseconds. wait_total The total (accumulated) wait time in microseconds. count The total number of acquisitions. avg The average hold time in microseconds, derived from the total hold time and the number of acquisitions. wait_avg The average wait time in microseconds, derived from the total wait time and the number of acquisitions. cnt_hold The number of times the lock was held and another thread attempted to acquire the lock. cnt_lock The number of times the lock was already held when this point was reached. name The name of the acquisition point, derived from the source file name and line number, followed by the name of the lock in parentheses. SEE ALSO
sysctl(8), mutex(9) HISTORY
Mutex profiling support appeared in FreeBSD 5.0. Generalized lock profiling support appeared in FreeBSD 7.0. AUTHORS
The MUTEX_PROFILING code was written by Eivind Eklund <eivind@FreeBSD.org>, Dag-Erling Smorgrav <des@FreeBSD.org> and Robert Watson <rwatson@FreeBSD.org>. The LOCK_PROFILING code was written by Kip Macy <kmacy@FreeBSD.org>. This manual page was written by Dag-Erling Smorgrav <des@FreeBSD.org>. NOTES
The LOCK_PROFILING option increases the size of struct lock_object, so a kernel built with that option will not work with modules built with- out it. The LOCK_PROFILING option also prevents inlining of the mutex code, which can result in a fairly severe performance penalty. This is, how- ever, not always the case. LOCK_PROFILING can introduce a substantial performance overhead that is easily monitorable using other profiling tools, so combining profiling tools with LOCK_PROFILING is not recommended. Measurements are made and stored in nanoseconds using nanotime(9), (on architectures without a synchronized TSC) but are presented in microseconds. This should still be sufficient for the locks one would be most interested in profiling (those that are held long and/or acquired often). LOCK_PROFILING should generally not be used in combination with other debugging options, as the results may be strongly affected by interac- tions between the features. In particular, LOCK_PROFILING will report higher than normal uma(9) lock contention when run with INVARIANTS due to extra locking that occurs when INVARIANTS is present; likewise, using it in combination with WITNESS will lead to much higher lock hold times and contention in profiling output. BSD
November 11, 2006 BSD
All times are GMT -4. The time now is 06:57 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy