Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

I need to write a bash script that adds the current values and returns the average of the values. I am not sure why the script isn't printing the average to screen when I run it. Here's what I have written thus far:

Well strictly speaking cbtshare has shown the additionally requested detail, ie. how to get the average, but the answer to the simplest
way to get the sum of integers, ie the original question, would be:

Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Where did you learn this code? I'm sorry to say, but other than the basic for loop syntax, it's all wrong. It almost looks to me like you're conflating shell syntax with that of some other programming language.

This is actually an array variable expansion ( "${arrayname[index]}" ). The [] index brackets in this pattern do act in an arithmetic context, but all that the arithmetic expression above does is set five variables. The index expression as a whole expands nothing, so the array variable expansion also ends up as a null value. The for loop has nothing to work on.

But what's the point of the loop anyway? Even if it worked, it would only operate on a single value at a time (with that value being stored in the "$var" variable), but your subsequent lines indicate that you want to add all five values at once.

Code:

total= $sum(store1+store2+ store3+store4+store5)

Unlike the first line, there is no arithmetic context at work here. The variable total is set to the literal string. Or at least it would if the syntax were correct. "var=value" cannot have any unprotected spaces in it, outside of an arithmetic context.

Code:

echo $total / 5

Again, there's no arithmetic context involved, even if the "$total" variable did contain a valid integer, which it doesn't. echo would only print the literal string (more specifically, $total would be expanded first, then each word in the argument list would be printed as a separate string, with spaces between them).

As pointed out before, proper arithmetic operations are usually done inside $((..)) brackets, although there are other variations.