This lovely item came across my feed today and I realized there is so much that I do poorly when it comes to shell scripting it is absurd 🙁
Anyhow I am extremely thankful for this long and detailed post and wanted to pass it along to my readership.
I also wanted to crowd-source a bit of information from you all.
I am familiar with the idea of a code repository but have never really used one. I have had a lot of people suggest I use GIT. I am curious if that is the general consensus or if anyone else has other suggestions? I have a project that consists of some pretty crazy scripts (well, crazy for me, 1000+ lines). Trying to keep track of versions and changes in a script that large is difficult to say the least. I am looking for something easy to use and quick to deploy. Thoughts welcome.
A lot of my bash scripting experience has been, in one sense, relatively simple. I have several scripts that span several hundred lines and do fairly complex things across multiple systems. From that perspective they aren’t necessarily simple. However it wasn’t until recently that I had to really starting thinking about managing when scripts run and particularly keeping them from “stepping all over each other” when multiple instances of the same script must be run… enter the topic of “Job Control” or “Controlled Execution.”
A common scenario is that your bash script is written to access some shared resource. A few examples of such shared resources:
-An executable file that can only have one running instance at any given time
-A log file that must be written to in a certain order
-Sensitive system files (such as the interfaces file).
What happens if a bash script gets executed once, and then before the first instance finishes running a second instance is fired off? The short answer is typically unexpected/bad stuff that tends to break things.
So the solution is to introduce some job control logic into your scripts. And to that end I want to talk about two methods of controlling job execution that I have started to employ heavily for one of my projects: Simple Lock Files, and the more involved FLOCK application built into most newer Linux distributions. For reference, most of this article is based on a system running Debian Jessie. (more…)
I am working on a project where I need to generate a random value and assign it to a variable…
Initially I was doing this:
var=$(date +%s | sha512sum | base64 | head -c 12 ; echo)
Which was okay until I started executing my script more than once in a short period of time. Suddenly my random variable wasn’t so random and I was getting the same value multiple times in a row. This is a flaw of this method in that it is a hash of the value of the date (represented as an epoch value) at a given second.
UPDATE – 6/28/2016 – Someone pointed out to me that the OpenSSL method below also relies on the value of the date and time. I am not sure why it doesn’t experience the same issues as just using a sha512sum and, as I have a working method, am not going to investigate it further. I just wanted to clarify for the sake of accuracy. I am not an expert with this stuff.
So I had to can that method and that led me to trying this next:
var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 12 | head -n 1)
This worked perfectly from the shell. Worked great when I executed a test script from the shell using it. However the PHP script that fired off the bash script would freeze whenever I used this. (more…)
If you run a custom theme for your Owncloud distribution, one of the things you know is that whenever you run an update of your Owncloud server it will automatically disable your theme as part of the update process. This is to keep things from blowing up in the event you have made a modification to a file that will break the updated version of Owncloud.
Themes can be used for more than just visual changes. For example, if you want to disable the “change password” button for users, this can be done by modifying some PHP files and dropping them in your theme. In my case, I added Piwik tracking code in several of my Owncloud files. I want to keep those modifications but I also want the updated code.
This has historically been a pain point during every Owncloud update. Every time I have run an update it has been a laborious process of comparing my theme files to the new stock files and looking for changes and then merging appropriately. So today, I decided to script part of that comparison process… (more…)
I came across a real life-saver today…
All of my backup scripts are based on using RSYNC. This is because I am used to working between linux systems on which I always have root/sudo access and also have SSH. However I was recently tasked with a “fun” project that involved me needing to script the syncing of some directories between a linux server and a windows server running third-party SFTP software… that doesn’t allow SSH, SCP, etc. (more…)