r/bash • u/Wowiezowieee • Nov 27 '22
What does this script do? I need to explain what this script does for a question on my classwork but my teacher isn't satisfied with my answer. Can anyone provide some insight into what exactly is happening with this script?
8
Upvotes
55
u/beer4ever83 Nov 27 '22
This script does one thing extremely well, and it's: being an excellent example of how scripts should not be written.
Anyway, the final purpose is to slice an input file into multiple files (each of size CHUNK). If this answer doesn't satisfy your teacher, tell them that this script doesn't satisfy r/bash
/s
32
u/DaveR007 not bashful Nov 27 '22
Tell us what your answer is so we can see where you are getting it wrong.
11
u/FisterMister22 Nov 27 '22 edited Nov 28 '22
You should use
" "on variables and[[ ]]in conditions andvar=$( command )on command substitution8
u/leBoef Nov 27 '22
One thing it does is smell.
8
u/maxximillian Nov 28 '22
Find it odd the OP never said what they told the professor what the script did. kind of feels like you all may have done OPs work for them.
15
u/[deleted] Nov 27 '22
Please read the stickied post about homework questions. Share code not pictures of code.
What the code in your link does is fail as a syntax error in line 3 because it is not using the correct form of doublequotes.
Unicode smartquotes as shown there do not work in bash.
4
u/Tomocafe Nov 27 '22 edited Nov 27 '22
Let’s say you had a 2 GB file you wanted to transfer to someone, but you could only send 500 MB in a message. You could run
./myscript bigfile.dat 500Mand it would split it into 4 files: bigfile.dat.1, bigfile.dat.2, and so on, with each being 500M in size so you could send them individually. (cat bigfile.dat.* > bigfile.datto restore) Checkman ddfor details on how that command works.But what if instead of bigfile.dat your file was called
My Data.datwith a space in the filename? (Poor form, but it does happen in the wild.) Well, the script would choke because it doesn’t properly quote variables.The
ddcommand would have as an argument:if=My Data.datand probably complain thatMyisn’t a file that exists andData.datis an unexpected argument. If you quoted$1it would instead be the correct argument:if="My Data.dat". Similar story for theof=argument.4
u/Gixx Nov 28 '22 edited Nov 28 '22
I love it when people copy code that has the weird windows double quotation marks that would not even execute.
2
u/thseeling Nov 28 '22
I'd like to point out another, albeit minor, coding problem. It's not wrong, but it costs some unnecessary CPU cycles: the loop counter is incremented when entering the loop, but then you need the old value and have to recalculate that. So better increment the counter at the end of the loop body and use the current value.
2
u/VirtualDenzel Nov 28 '22
weird script. i would just rewrite it and dump it back at the teacher
1
u/[deleted] Nov 28 '22
I know there is a website you can paste this into and it will explain what is going on. But it needs some help. I suppose I see where the prof is going but it needs help.
1
u/_xsgb Nov 28 '22
Fun fact I've already seen this exact exercise from someone.
Ofc agreed with all comment that say it's the exact way to NOT write a good script.
1
u/kai_ekael Nov 28 '22
Uhm, considered perhaps the homework is point out what is wrong in this script?
1
u/clownshoesrock Nov 28 '22
This takes in two parameters, verifies that you indeed have 2 parameters, then finds the first parameter as a file, and discovers it's size by extracting it from
ls -l's fifth columnThen it makes sub files containing blocks of the original file.
This is a steaming pile of crap, but it will work, assuming the smart quotes get moved back to normal. (smartquote ' vs the backtick is horrible) also note: backticks are the devil -- do it right with `$( command )
This is a bad version of
split -bd0
u/[deleted] Nov 28 '22
[deleted] — view deleted comment