After I recorded two notes on the guitar - F⯠and E â - I began to think how to randomly select one of them and play it so that you can listen and then look at the screen.
At first I decided that it was worth writing a small program in Python or Ruby. But then I thought that you could just write it in bash.
Using the "
set --
" command you can set positional parameters. The first parameter passed after the "
--
" will be $ 1, the second $ 2, and so on.
')
Let's set as parameters two file names - one file with a note in F-sharp, and the other with E-flat.
set -- "e_flat.wav" "f_sharp.wav"
By the way, using â
set -- *
â you can set all the files and folders in the current directory as positional parameters.
Now we will set a random number. This can be done with the shuf command.
shuf -i 1-2 -n 1
That is, one (-n 1) random number from 1 to 2 (-i 1-2).
To use the result of a command as a value in bash â if we want to output it or, as now, write it into a variable â you just need to put the command in parentheses by putting a dollar sign ($) in front of them.
random=$(shuf -i 1-2 -n 1)
The $ {! Var} construct allows you to get a variable whose name is contained in the variable var (bash indirect reference). If you run test = PATH and then echo $ {! Test}, then the contents of the $ PATH variable are displayed.
Thus, $ {! Random} is the contents of a variable whose name is written in the variable random. And in our case, either 1 or 2 is written there â that is, the contents of either the $ 1 variable or $ 2 will be displayed.
Well, here we all wrote. It remains to run.
set -- "e_flat.wav" "f_sharp.wav" ; random=$(shuf -i 1-2 -n 1) ; mplayer ${!random}
That is, we
once again made sure that you can write a lot of different things on bash, and often, if there is some specific situation in which it is necessary for the computer to do something, then bash is ideal for this.