1

I have a task for university to create 5 folders, 10 files inside of each of them, then randomly generate 10 numbers inside all the files and finally calculate the sum of numbers in files of each folder and output them to a separate file. We only learned the basics of basics and it's really hard for me to understand what to do next. I have managed to create the folders and files with random numbers inside but I don't know how to go through every folder and sum the numbers in each file. Can someone please help me with that?

#!/bin/bash
mkdir Katalogas1
mkdir Katalogas2
mkdir Katalogas3
mkdir Katalogas4
mkdir Katalogas5
for x in {1..10}
for x in {1..10}
do 
touch "$x.txt"
shuf -i1-1000 -n 1 >Katalogas1/$x.txt
done
paste 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt >bendras1.txt
cat bendras1.txt | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,($1+$2+$3+$4+$5+$6+$7+$8+$9+$10)}' >katalogas1.txt
cd ..
cd Katalogas2
for x in {1..10}
do 
touch "$x.txt"
shuf -i1-1000 -n 1 >Katalogas2/$x.txt
done
paste 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt >bendras2.txt
cat bendras2.txt | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,($1+$2+$3+$4+$5+$6+$7+$8+$9+$10)}' >katalogas2.txt
cd ..
cd Katalogas3
for x in {1..10}
do 
touch "$x.txt"
shuf -i1-1000 -n 1 >Katalogas3/$x.txt
done
paste 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt >bendras3.txt
cat bendras3.txt | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,($1+$2+$3+$4+$5+$6+$7+$8+$9+$10)}' >katalogas3.txt
cd ..
cd Katalogas4
for x in {1..10}
do 
touch "$x.txt"
shuf -i1-1000 -n 1 >Katalogas4/$x.txt
done
paste 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt >bendras4.txt
cat bendras4.txt | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,($1+$2+$3+$4+$5+$6+$7+$8+$9+$10)}' >katalogas4.txt
cd ..
cd Katalogas5
for x in {1..10}
do 
touch "$x.txt"
shuf -i1-1000 -n 1 >Katalogas5/$x.txt
done
paste 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt >bendras5.txt
cat bendras5.txt | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,($1+$2+$3+$4+$5+$6+$7+$8+$9+$10)}' >katalogas5.txt
  • and you decided that you let us make your homework. I help you a bit, but you must try more, you must make some functions in bash you can create functions. –  Dec 11 '19 at 13:15
  • 2
    Recommendation for your existing code: Use a loop `for dir in Katalogas{1..5}; do mkdir "$dir"; ...; done` instead of repeating the same code over and over again. – Socowi Dec 11 '19 at 13:18
  • 1
    Combine the answers from [How can I quickly sum all numbers in a file?](https://stackoverflow.com/questions/2702564/how-can-i-quickly-sum-all-numbers-in-a-file) or [Shell command to sum integers, one per line?](https://stackoverflow.com/q/450799/6770384) with `cat Katalogas*/* | someCmdThatSumsUpStdin` – Socowi Dec 11 '19 at 13:19
  • actually I did not decide that, I wrote that I don't know how to go through every folder and sum all the numbers inside of the files. I was hoping that someone will be able to explain that to me. I'm aware that (probably) 'for' cycle is required but I'm not sure how to use it in this case. – xdarkestshadow Dec 11 '19 at 13:20
  • 1
    you do not write to the files at all, your code just creates 10 files. –  Dec 11 '19 at 13:21
  • I edited it, now that part should be fine – xdarkestshadow Dec 11 '19 at 13:29

2 Answers2

0

As Max Muster said, we will not do your homework but for sure we will help you!

I think you have to re-do everything from scratch.

First, I suggest you to re-think how this could be done. "what the easier way to achieve this?" Is it: 1. Create folder, create empty file, generate 1 number, rename one file with that number, create 2nd empty file, generate 2nd num, rename, etc. or 2. blabla or 3. blabla

Actually, there are a lot of possibilities. But you have to choose one. Once done, write your code with English word (or you native language, I'm French actually) like:

create folder then
  generate num 1
    create file named 1
  generate num 2
etc

Then post it and we will tell you if it's the best approach for a junior

Hope I was helpful Iswaren

Iswaren
  • 3
  • 4
  • I edited it, added 'awk' and 'paste' functions, but I'm not sure if I did it okay? – xdarkestshadow Dec 11 '19 at 13:40
  • Max gave you the answer. Actually, I wanted you to think and produce what Max wrote. But a tip, when you program, think about all possibilities. I might sound dumb but it will design your brain for programming. – Iswaren Dec 11 '19 at 14:23
0

I told you to use functions

#!/bin/bash

k=("Katalogas1" "Katalogas2" "Katalogas3" "Katalogas4" "Katalogas5")

# one function to make the folders

mfiles()  {
 cd $1
 for x in {1..10}
  do
  touch "$x.txt"
  shuf -i1-1000 -n 1 > $x.txt
 done
 cd ..
}

# one function to go the same way and count the beans 
# and write it to a file in your folders

summ() {
 cd $1
 touch "sum.txt"
 for x in {1..10}
  do
  ((sum += `cat $x.txt`))
 done
 echo "$sum" > "sum.txt"
 cd ..
}

# finally this is your main program

for i in "${k[@]}"; do
    mkdir "$i"
    mfiles "$i"
    summ "$i"
done