Wednesday, December 28, 2016

[Bash Challenge] Can You Solve This Bash Scripting Exercise?

If you follow It’s FOSS on Facebook, you might be aware of the weekly Bash Challenge. It is a joint effort by Yes I Know It and It’s FOSS to give you a Bash script exercise to test your Linux skills.

We are bringing this Bash Challenge from Facebook to a wider audience on the regular web. This is the 5th installment of this series. The first 4 challenges can be found on our Facebook pages.

Bash Challenge 5

We will show you a terminal screenshot, and ask you to explain why the result is not the one we were expecting. Of course, the most amusing, and most creative, part of the challenge will be to find how to fix the command(s) displayed on the screen to obtain the correct result.

Ready to play? So here is this week’s challenge:

My Bash don’t know how to count [Difficulty level 1]

Bash Challenge 5: Bash Scripting exercise

This week, I have some data file containing integer numbers, one on each line:

cat sample.data
102
071
210
153

And I want to compute the sum of all those numbers:

declare -i SUM=0
while read X ; do
    SUM+=$X
done < sample.data
echo "Sum is: $SUM"

Unfortunately, the result I obtain is wrong (the expected result was 536) :

Sum is: 522

Challenge

Your challenge is to find :

  • Why that result was wrong?
  • How to fix my commands to obtain the correct result?

★ Bonus unicorn point if you can find a solution using only Bash internal commands and/or shell substitutions.

We’re looking forward to read your solutions in the comment section below! Don’t forget to be creative.

Few details

To create this challenge, I used:

  • GNU Bash, version 4.4.5 (x86_64-pc-linux-gnu)
  • Debian 4.8.7-1 (amd64)
  • All commands are those shipped with a standard Debian distribution
  • No command was aliased

Solution

The answer will be published next week. Meanwhile, you can put your skills to use and figure out the problem here. The comment section is all yours.



from It's F.O.S.S. http://ift.tt/2iEd8Ze
via IFTTT

No comments:

Post a Comment

Playing Grand Theft Auto Inside A Neural Network’s Hallucination? It’s Possible!

Ever imagined what a Neural Network's hallucination would look like? The post Playing Grand Theft Auto Inside A Neural Network’s Halluc...