Posts

Showing posts with the label bash

Thoughts on Random Number Generation - Shell Scripts

Image
This blog was first published in 2019, but the site has since been shut down. One of the first exercises given to me as a mathematics student was to write a random number generator (RNG). This turned out to be not so easy. Test sequences cycled quickly or were too predictable or were not evenly distributed. Typically when we talk of RNG’s we are describing  pseudorandom  number generators. Nowadays, we have a many programs that will generate  pseudorandom  numbers. Where are random numbers used? As a developer they were rarely required. Recently, however we’ve seen them appear in more and more places - it seems they are  everywhere ! In DevOps, I’ve used RNG’s for creating message payloads of arbitrary size, and for file or directory names. These values are often created using scripts written in  bash . This first article will explore three simple RNG’s that can be run from bash. It is not an exhaustive list, as there are others such as  jot  that...

Replacing CRLF from files

Image
The following is a number of different methods you can use to strip ^M from files. This was more of an exercise than a feature I need regularly. However, there was a recent incident at work where someone had checked-in such an abomination. Some tools just don't like Windows line endings in files, i.e. CRLF, Carriage Return Line Feed . So, as an exercise, here is a collation of the numerous ways to fix these aberrations ... Note: Most editors now have a quick way of doing this. file You can see whether a file has ^M using the file command: Example Consider a file containing ^M, file reports: $ file test.txt test.txt: ASCII text, with CRLF, LF line terminators After stripping ^M, we have: $ file test-fixed.txt test-fixed.txt: ASCII text dos2unix Probably the simplest way is to use the dos2unix command. Example Show file has ^M line endings: $ dos2unix -i test.txt 3 16 0 no_bom text test.txt Now fix: $ dos2unix test.txt Proof that file has been fixed...

Bash arrays - what is the difference between array[*] and array[@]?

I've just been updating some Bash scripts and noticed that there are two ways to refer to array elements: one using the "*" the other using "@". So what is the difference? According to the manual The "@" variable allows word splitting within quotes (extracts variables separated by whitespace). This corresponds to the behaviour of "$@" and "$*" in positional parameters. So you get two different outcomes as demonstrated below: $ ARRAY = ( 0 1 2 3 4 5 6 7 8 9 )   # first using "@" inside quotes, # you get the same output with no quotes ... $ for i in " ${ARRAY[@]} " ; do echo " $i " ; done 0 1 2 3 4 5 6 7 8 9   # same as above but using "*" ... $ for i in " ${ARRAY[*]} " ; do echo " $i " ; done 0 1 2 3 4 5 6 7 8 9   # but without quotes ... $ for i in ${ARRAY[*]} ; do echo " $i " ; done 0 1 2 3 4 5 6 7 8 ...