Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8812753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:40:35+00:00 2026-06-14T03:40:35+00:00

I tried to create a shell script, which sum the given numbers. If there

  • 0

I tried to create a shell script, which sum the given numbers. If there is no given parameter, then it tries to read the pipe output, but I get an error.

#!/bin/sh

sum=0

if [ $# -eq 0 ]
then
  while read data
  do
    sum=`expr $sum + $data`
  done
else
  for ((  i = 1 ;  i <= $#;  i++  ))
  do
    sum=`expr $sum + ${!i}`
  done
fi

echo $sum

This works: sum 10 12 13
But this one doesn’t: echo 10 12 13| sum

Thanks in advance,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:40:36+00:00Added an answer on June 14, 2026 at 3:40 am

    Here you go (assuming bash, not sh):

    #!/bin/bash
    
    sum=0
    
    if (( $# == 0 )); then
      # Read line by line
      # But each line might consist of separate numbers to be added
      # So read each line as an array!
      while read -a data; do
        # Now data is an array... but if empty, continue
        (( ${#data[@]} )) || continue
        # Convert this array into a string s, with elements separated by a +
        printf -v s "%s+" ${data[@]}
        # Append 0 to s (observe that s ended with a +)
        s="${s}0"
        # Add these numbers to sum
        (( sum += s ))
      done
    else
        # If elements come from argument line, do the same!
        printf -v s "%s+" $@
        # Append 0 to s (observe that s ended with a +)
        s="${s}0"
        # Add these numbers to obtain sum
        (( sum = s ))
    fi
    
    echo $sum
    

    You can invoke it thus:

    $ echo 10 12 13 | ./sum
    35
    $ ./sum 10 12 13
    35
    $ # With several lines and possibly empty lines:
    $ { echo 10 12 13; echo; echo 42 22; } | ./sum
    99
    

    Hope this helps!

    Edit. You might also be interested in learning cool stuff about IFS. I’ve noticed that people tend to confuse @ and * in bash. If you don’t know what I’m talking about, then you should use @ instead of *, also for array subscripts! In the bash manual, you’ll find that when double quoted, $* (or ${array[*]}) expands to all the elements of the array separated by the value of the IFS. This can be useful in our case:

    #!/bin/bash
    
    sum=0
    
    if (( $# == 0 )); then
      # Read line by line
      # But each line might consist of separate numbers to be added
      # So read each line as an array!
      while read -a data; do
        # Now data is an array... but if empty, continue
        (( ${#data[@]} )) || continue
        # Setting IFS=+ (just for the sum) will yield exactly what I want!
        IFS=+ sum=$(( sum + ${data[*]} ))
      done
    else
        # If elements come from argument line, do the same!
        # Setting IFS=+ (just for the sum) will yield exactly what I want!
        IFS=+ sum=$(( $* ))
    fi
    
    echo $sum
    

    Gniourf now exits from teacher mode. :-)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to create a script to list the contents of a directory: #!/bin/bash
I have a shell script which I want to run without using the sh
I have a shell script that creates Firefox profiles and then uses them to
Below is my shell script from which I am trying to invoke few hive
I need to create an ASP page (classic, not ASP.NET) which runs remote shell
I want to sell online. and I tried to create a Paypal account and
I have tried create this table, but nothing I have tried works from FKs.
Tried to create a mobile navigation using dataview or outline and the view renders
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
I tried to create jar file using command line that I inserted into java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.