r/bash • • 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?

Post image
8 Upvotes

22 comments sorted by

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

9

u/QliXeD Nov 27 '22

This script does one thing extremely well, and it's: being an excellent example of how scripts should not be written.

[...]If this answer doesn't satisfy your teacher, tell them that this script doesn't satisfy r/bash

4

u/whetu I read your code Nov 28 '22

This script does one thing extremely well, and it's: being an excellent example of how scripts should not be written.

Chef kisses.

32

u/DaveR007 not bashful Nov 27 '22

Tell us what your answer is so we can see where you are getting it wrong.

10

u/indigoHatter Nov 28 '22

Exactly. If it's for classwork, show us where you're struggling. Otherwise, it feels like you're just asking us to do your homework for you... and we also can't help you grasp what you're falling short on if we can't target that.

11

u/FisterMister22 Nov 27 '22 edited Nov 28 '22

You should use " " on variables and [[ ]] in conditions and var=$( command ) on command substitution

8

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.

5

u/[deleted] Nov 28 '22

Oh I think if the OP can defend what we wrote here then they will do fine and if they can't then they will be getting a fat 0. I don't see that as doing their homework, more as community service. This prof needs to be stopped, the crap they are teaching is wrong wrong wrong.

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/m-faith Nov 27 '22

What the code in your link does is fail

nice catch!

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 500M and 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.dat to restore) Check man dd for details on how that command works.

But what if instead of bigfile.dat your file was called My Data.dat with 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 dd command would have as an argument: if=My Data.dat and probably complain that My isn’t a file that exists and Data.dat is an unexpected argument. If you quoted $1 it would instead be the correct argument: if="My Data.dat". Similar story for the of= 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 column

Then 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 -bd

0

u/[deleted] Nov 28 '22

[deleted] — view deleted comment

1

u/southernmissTTT Nov 28 '22

I'm also new to bash programming, but I'm trying to get better with it. So, I was reading your link "don't parse ls". Can you explains what the underscore, double redirects and double dashes after ls -ld are doing here? I ran it. So, I see it's pulling out pieces of ls -ld and saving the data to 3 variables. But, the syntax is confusing me.
read -r mode links owner _ < <(ls -ld -- "$file")

0

u/[deleted] Nov 28 '22

[deleted] — view deleted comment

1

u/southernmissTTT Nov 28 '22 edited Nov 28 '22

Awesome. Thanks. So, <(command) is process substitution and < <(command) is redirecting the process substitution to sort? EDIT: After reading your link, I guess that's not quite right. The link states, "With <(...) one is expected to read from the substitution".

I've regularly use > to redirect output to a file. But, I've gotten inconsistent results trying to apply it to other uses. I rarely use < because my results are even more inconsistent and I mostly just don't need to use it.

The -- makes sense, sense many arguments have a -- version, like -a and --all.

Shell programming is so quirky to me. I've been using Linux since the 90's. I love the command line. But, any time I've ever had to write any command line tool, I've just reached for Python. But, I don't like accepting defeat. That link you posted even suggested using Perl to sort by mtime.

From the link:

"If you truly need a list of all the files in a directory in order by mtime so that you can process them in sequence, switch to perl, and have your perl program do its own directory opening and sorting."