Page 50 - Raspberry Pi as a Foundation for Boosting Computer and Technology Literacy
P. 50

3  Fundamentals of the Linux system and terminal usage





                Figure 3.26  The bash script for fetching random man pages


                  find /usr/share/man/man1 -type f | shuf | awk -F
                  ‘/’ ‘{print $6}’ | sed ‘s/.gz//g’ | head -1
                  First, we utilize the find command to search through a directory
                containing mostly user-focused tools at the path “/usr/share/man/
                man1” and include only files.
                  Then, we use shuf to mix them in a random order. Next, we clear
                the output through awk, which divides every line into individual fields
                (using ‘/’ as the separator) and returns the sixth field of every line.
                Then, we globally remove the unnecessary file extensions “.gz” by sub-
                stituting them with an empty space using sed (Figure 3.25). Finally,
                we select only the first line using the head.
                  We are using the variable  random_manpage to store the values
                generated by the aforementioned command, using the command sub-
                stitution expression $():

                  random_manpage=$(find /usr/share/man/man1 -type
                  f | shuf | awk -F ‘/’ ‘{print $6}’ | sed ‘s/.
                  gz//g’ | head -1)
                  To complete our bash script, we will pass the variable to the man,
                which will open the man page of the specified command (Figure 3.26):

                  man $random_manpage
                  Finally, save and exit the file, and make it executable for the current
                user via the chmod command:
                  chmod u+x randman
                  Now, the program can be executed from the home folder by running
                ./randman (“./” stands for the current directory).














                            50
   45   46   47   48   49   50   51   52   53   54   55