Linux Blog

Random Numbers in Bash

Filed under: Shell Script Sundays — Owen at 8:00 am on Sunday, February 22, 2015

Going back to last weeks article on running things continuously, part of the use case was generating random numbers.

While using echo $RANDOM works, but it doesn’t work very well for having a minimum and max value. There are techniques to do this, but what I’ve found easier is to use another language of your choice and run that separately using expansion ( $() or “). I’m sure there is a way to do it in Ruby and Python and whatever language but since I’m familiar with PHP, I’ll use that in this example first:

for i in `seq 1 10`; do
echo `php -r 'echo rand(1, 100);'`

This example iterates the loop 10 times, and echo’s a random number between 1 and 100. here is more information on PHP’s random function Note that this does require having php5-cli package installed.

If you don’t have PHP, but have perl you can use:

for i in `seq 1 10`; do
echo `perl 'print rand(100);'`

That’s great and all, but perl uses what looks like floating numbers. To get an int, you have to cast to an int which adds additional typing:

for i in `seq 1 10`; do
echo `perl 'print int(rand(100));'`

To me, this is easier but, if you don’t have PHP, Perl, Ruby, Python or whatever your choice, you could use bash (I always forget how, but here it is for reference:)

echo $[ 1 + $[ RANDOM % 100 ]]

Or if you have complex requirements, you could write a quick binary that does the same thing. Another alternative is to use another shell that you do have access to that happens to have a more robust RNG.

Compiz Config

Filed under: General Linux,Linux Software — at 8:00 am on Wednesday, February 18, 2015

One of the great things about Linux is that if you don’t like something, you have the power to change it. You can add features, or remove them to suit your needs. One of the cool features that has been added to modern distributions is the OpenGL compositing manager called Compiz. A lot of distributions come with Compiz as standard, but some don’t. If it does and you have no need for it you can remove or tweak features and functionality it provides. Personally I’m not a fan of every feature or all of the flashiness, so sometimes I tweak it. Follow the jump for more information.
(Read on …)

Bash Continuous Loop

Filed under: Quick Linux Tutorials,Shell Script Sundays — Owen at 8:00 am on Sunday, February 15, 2015

I came across a use case for running something continuously until breaking, rather than pick a large number and a for loop (for i in `seq 1 10000`) you can use a while loop with something that returns true, my first instinct was to use while echo, but that returns a blank line in-between each iteration, which could be useful, but if you don’t want any kind of spacing or notifications you can use while true as follows:

while true; do 
echo "STUFF"; 

This technique could be used with sleeps to run something every x seconds, although when you start getting into that you’d probably be better off using cron.

DIY Monitor Stand

Filed under: Linux Hardware — at 7:00 am on Wednesday, February 11, 2015

At work, we have these pretty sweet monitor mounts. I used to have a wall mounted one at home, although it was not nearly as nice. The reason I no longer used it was, my laundry room is behind my monitor, and anytime the washing machine was on it would vibrate which was really annoying. It got thrown in storage, not to be seen again for a while – until recently. I decided to have a go at making my own, similar to the ones we use at work except for a single monitor and a fraction of the cost. I had some black iron from another project, a piece of L tubing from a bed frame otherwise known as “poor mans angle-iron” and the mount. The idea was to take my VESA mount and mount it to the pole, use the bed frame to attach it to the desk with c-clamps. After welding the black iron to the bed frame I chopped up an old bicycle seat post which happened to be the perfect inner diameter to fit over the black iron, I welded two bolts to the bike seat post pieces and welded a captive nut for a hex bolt to the bottom one to make it height adjustable. I put my monitor on the VESA mount, attached the mount to each of the slides, and placed it over the black iron tube. It works great, I can adjust it from desk height to about 20″ and move the monitor from left to right. The monitor does “tilt” a little when it extended further to the left or right, but it is hardly ever fully extended.

This was a pretty decent turnout for about an hour with my angle grinder and welder. It seemed like the primer took longer to dry than it took to fabricate it. I never did get around to painting it black. Please ignore the dusty shots, I didn’t bother to clean it for these pictures, and the flash causes every spec to show up. Yea, that’s it – the flash.

Auto Clean-up Downloaded Files – Part IV

Filed under: Shell Script Sundays — at 8:00 am on Sunday, February 8, 2015

In order to avoid the complex task of file comparisons on unknown files and types for what should be a simple task, I’ve made an executive decision to handle statistics. Hopefully I will not regret this should I decide to tackle file comparisons. For the cleaning up of Downloaded files there are really only a few statistics that I can think of that are meaningful to the task of deleting multiple files.

The first being counts, this could be the count of files in the folder, the number that matches the (?) find pattern and the total count of deleted files.
For the second “metric” disk space is a good one, but could be tricky to calculate given different file size types (byte, kilobyte, megabyte, etc.)
Timing is another option. We’ll skip how long I spent on this, as it is useless. I’d rather spend my time writing something that can be reused rather than wasting time pointing and clicking – although it would be interesting to calculate how much time was spent writing vs. the total run time we wont cover that. What we will cover is how long did it take to discover and delete the files? A fun number if for nothing else bug giggles.

Fortunately for us, utilities exist for all of these items and can be added fairly simply. We’ll start with the first and work our way down, regardless of how I feel about the last two items.
(Read on …)

Create your First Shell Script

Filed under: Shell Script Sundays — at 8:00 am on Sunday, February 1, 2015

Shell scripts is a really useful skill to have. Creating a script to do avoid repeating a task can save you time in the long run. What a lot of people don’t know is that shell scripting is not hard, especially if you have some Linux command line experience. You can pretty much do anything you want with a script, and they are great for automating tasks. To create a shell script from a one-liner all you really have to do is:

echo "[your one-liner here]" > [your-script-name-here]

That will create your file, which you can then change the permissions on and move to your ~/bin/ directory. It would be wise to add $!/bin/bash as the first line if bash is your shell of choice, if you choose to distribute it. Once you’ve translated your one-liner into a file that can be executed you can start adding functionality to the script to ease use for future use.

Here are some explanations of basic functionality you can add to your scripts:

If-then-else statements If then else’s can be used to control flow or make decisions, they are very useful indeed.

read can be used to get input from the user, when dealing with an unknown or a variable

functions are great to use to store a particular set of instructions that can be called repeatedly without having to re-write the script.

loops do exactly as it sounds, loop. That is re-iterate over a variable, or repeat an instruction. You could use a loop to call a function over and over until a clause is met.

getopt’s is an instruction that can be used to read input from when the script is called. Often this is used to change functionality or display usage information.

redirection techniques are used throughout shell scripts and is one of the fundamentals of shell scripting. Master this and you’ll be piping and redirecting output to files and other programs in no time.

error checking and handling, often overlooked but shouldn’t be underestimated. Checking for errors before they happen can save time, and undesired results.

Many of these techniques are covered throughout this blog (feel free to browse or search), and there are many great online resources and books available (both free and paid) to help you with your journey. A good place to start for most of the techniques is in my Shell Scripting 101 article. Good luck!