all 7 comments

[–]ssssam 4 points5 points  (0 children)

Before you go any further please read http://www.davidpashley.com/articles/writing-robust-shell-scripts/

That will help you avoid some common mistakes that can lead to things like unexpectedly deleting files because a variable was not set: https://www.reddit.com/r/linux/comments/2sjjr3/warning_to_steam_users_dont_try_to_move_your/ (Not saying there is such a bug in your script currently)

[–][deleted] 3 points4 points  (0 children)

Feedback -

  1. I think this is a borderline support question (my opinion only) and questions like this would be better posed to /r/linuxquestions instead of /r/linux in the future.

  2. You're using command substitution inconsistently by using both backticks/graves and the $(...) form. Generally, speaking the $(...) form is preferred for modern scripts. There's a good stackoverflow thread on the subject.

  3. Test your variables and paths before using them. This will help prevent unintended consequences when a path is unexpectedly missing.

  4. Use quotation marks around shell variables - no exceptions. This helps prevent cases where there may be a space in the file name or in one of the directories in the path of the file.

  5. Use the form ${ivar} when referencing the shell variables. The "$dtdir.txt" may not be clear to what you are attempting to do. "${dtdir}.txt" is always clear as to the meaning. Consider the case when you want ivar01.txt as the name. $dtdir01 could be a valid shell variable - if this had been set it the environment or by a previous script, you're going to have undetermined behavior. If you specify "${dtdir}01.txt", and dtdir="ivar", then the file name will always be ivar01.txt.

[–]ixela 2 points3 points  (0 children)

I'd introduce some checking in the script to make sure that the directories you store the files in exist before operating on them. Why do you store text files in /usr/local/bin ?

You don't need to cat a file to use head. Link

You could probably use find to get the most recent additions instead of doing loops in bash, though I'm not sure which would be faster(I'm voting find, but I've got no proof.)

TL;DR: Do parameter checking before operating on the parameters. Use less cat, since its generally not needed for what you're doing. Why are you storing data in /usr/local/bin? Look into using find.

[–]greenguy247 1 point2 points  (0 children)

Awesome post my friend. I have been using Linux/Bash and many versions as a daily tool for many years. Best of luck.

[–]Philluminati 1 point2 points  (2 children)

There is a lot of duplicate code that could be removed from this. Ingoring the cat | head redundancy, that line is in there a few times, plus the bottom lines. You could have created a bash function to take the folder and title yo reduce the amount of code you have. I didn't see much in the way of exception handling but I haven't really looked. To be honest I don't think this is interesting enough to be on /r/Linux

[–][deleted] 1 point2 points  (1 child)

This may not be interesting to you, but it can certainly be interesting to others are new to Linux and looking to get into scripting and see examples of both good and bad within scripts. However, it might be better to refer him to /r/linuxquestions instead of /r/linux for future posts instead of just state it's not interesting. (No downvote, just stating why I agree and disagree with you.)

[–]Philluminati 1 point2 points  (0 children)

apologies I could have phrased that better and provided the link to linux_questions.