function f-tutorial.6 () { bold ; cyan clear echo echo "In Which We Spy and Dig" white cat << EORECAP We now have some idea how to find information in the system with "locate" and "grep". We also saw how useful it can be to string commands together with a pipe. Here we look at what is going on behind the scenes, and learn some simple ways to find information. We also touch on the beginnings of scripting, without becoming too advanced on the subject. EORECAP cyan echo "Who is Who" white cat << EOWHO GNU/Linux is designed as a multi-user system. A lot of the time, if you are using the system as an individual, for example on a home system, this will not be obvious - and might even seem irrelevant. The concept of a "user" is not quite what you might expect, though. Users may or may not be actual humans! You are likely to be the only "user" currently running INX. How does the operating system see things, though? At the prompt below, type EOWHO yellow echo "who" white echo echo "As usual, exit or CTRL+D to return..." unbold echo /bin/bash echo bold cat << EOEXPLAIN The system thinks there are several of you! From its viewpoint, this is of course perfectly correct. The user $USER is logged in on several tty devices. If, for example, this was a remotely accessible system, or a server for a number of users, you would see the various user names in the output of "who". This is handy if you feel nasty and want to kick someone off the system ... Funnily enough, you can get more useful information with a one letter command. At the prompt below, type EOEXPLAIN yellow echo "w" white ; unbold ; echo /bin/bash bold echo echo "Now you know what those miscreants are up to." echo "The \"what\" column on the right tells you the latest silly thing each user has" echo "tried to tell the machine to do!" f-tutescape bold ; cyan ; echo echo "Processes" white cat << EOPROCESS The machine keeps track of what is happening using process numbers. These are known as "Process ID" or "PID" for short. Knowing this is often useful. If something is not quite right, we can often identify the culprit using commands that show us the PID, and more. The simplest commands of this kind are EOPROCESS yellow ; bold echo "pidof" echo echo "pgrep" white echo echo "The second one should look vaguely familiar..." echo echo "We can use these to find out the pid for particular running processes." echo "For example here is the current output of \"pgrep menu\"" echo "(if it's blank then \"menu\" is not running)" echo pgrep menu echo echo "If we needed to stop that process, we could type \"kill \"" echo echo "A quicker way, using a bit of Bash magic, would be" echo ; yellow echo "kill \$(pgrep menu)" white echo echo "Here we are using Bash to substitute the result of the command." echo "The same result can be achieved by using \"backticks\" instead of the \$( ) syntax:" yellow ; echo echo "kill \`pgrep menu\`" white ; echo echo "But of course, there's yet another way... as so often in UNIX and GNU/Linux." yellow ; echo echo "pkill menu" white ; echo f-tutescape echo bold ; cyan ; echo echo "More Spying Methods." white cat << EOSPY While we are on the subject of cloak-and-dagger commands like "kill"... It's not quite as bad as it sounds - actually the "kill" command is a way to send "signals" to processes. Not all of these signals actually terminate processes. To know more have a look at EOSPY yellow echo "man signal" white echo echo "To find manual pages related to a particular command, you can type" yellow ; echo echo "apropos " white ; echo echo "or" echo ; yellow echo "man -k " white ; echo echo "I prefer \"apropos\" - it has a ring to it! Either will identify relevant man pages." echo echo "In the shell below, try typing " yellow ; echo echo "apropos kill" white ; echo ; unbold /bin/bash bold ; echo echo "The point here is not that you should immediately read and understand the manual, but that this is a useful way to find a manual..." echo echo "The most important command for spying on what the machine is up to is probably" echo ; yellow echo "ps" white ; echo echo "Now this little two-letter command looks innocuous enough, but its options are many." echo echo "We'll look at a few of them, because \"ps\" is so often useful." f-tutescape echo cyan ; bold ; echo echo "Forests and Trees." white ; echo cat << EOPSBASIC The command "ps" tells us what processes are running on the current tty ( virtual terminal ). Digression: Virtual terminals are designated "tty" because originally they were actual "TeleTYpes" So here is the current output of "ps" on this "tty"... EOPSBASIC unbold ps bold cat << EOPSBASIC1 As you can see, not much appears to be happening... but this is an illusion, because there are potentially five other tty devices also running... So, we add the option "a" ... Now we can see rather more - all the other ttys, and users. EOPSBASIC1 yellow echo "ps a" ; echo white ; echo echo "Hit to see the output." f-tutescape unbold echo ps a bold ; echo echo "Now we add x, so the command becomes" yellow ; echo echo "ps ax" white echo echo "We are going to need to pipe this one through \"less\" - as you will see" yellow ; echo echo "ps ax | less" echo ; white echo "Hit to see the new output (q to quit from \"less\", of course..." f-tutescape unbold ps ax | less echo ; bold echo "Rather a lot of information here..." echo echo "So what does the \"x\" do? It includes all the processes that are running as other \"users\", and those that have no associated tty. So now we see all the stuff that is propping the system up and letting us run our programs." echo echo "We probably would like to know which user is running which process. So we add \"u\" . Notice that ps is unlike most commands in that these options do not have a leading \" - \". The command *can* use such options, but that is something you might want to explore by typing" echo ; yellow ; bold echo "ps -help" white ; bold ; echo f-tutescape bold ; cyan ; echo echo "Not Seeing the Forest for the Trees." white ; echo echo "The output of "ps aux" is too long to be useful, most of the time. Usually we want to know about a particular program or user. We can apply what we have learnt about grep and pipes to the problem. So let's say we are only interested in the program \"init\"..." echo echo "We can type" yellow ; echo echo "ps aux | egrep \"(init|USER)\" | grep -v grep" white ; echo echo "Ah, you haven't seen egrep before! egrep rather usefully lets us look for several things at once, as above. Notice the quotes around \"(init|USER)\" - remember special bash characters?" echo echo "We add in \"USER\" only to get the headings - we could use any pattern from the heading." echo echo "Here is the output..." echo ps aux | egrep "(USER|init)" | grep -v grep echo echo "Most of the time we won't bother with the headings, and we don't really need to get rid of the \"grep\" since we know we are running it, so we just run" echo ; yellow ; echo echo "ps aux | grep init" white ; echo echo "You notice that \"init\" has a PID of 1. It is the process from which all others spring." echo echo "Rather than singling out one process, we can look at the way this forest of information branches from one process to another. There is a --forest option to \"ps\" ! Let's have a look... This is the output of" echo ; yellow echo "ps ax --forest | less" white ; echo echo "Hit to see..." f-tutescape unbold ps ax --forest | less white ; bold ; echo echo "The output is a bit difficult to read - you can use a simpler command" echo ; yellow echo "pstree" echo ; white echo "Again, if we pipe through \"less\" we can page up and down if needed." echo echo "So, try typing below:" echo ; yellow ; echo echo "pstree | less" echo ; white ; unbold /bin/bash echo ; bold echo "Perhaps all this text is starting to bore you... well, here's a colourful alternative to explore... it updates in real time. In the shell below, type " yellow ; echo echo "htop" white ; echo echo "q to quit... CTRL+D to return. Have a look at the F-key options in this program..." unbold ; echo /bin/bash clear ; bold ; cyan ; echo echo "Finding Where Things Live." white ; echo echo "A common question asked by beginning users is \"Where does the program go when installed?" echo echo "The glib answer is \"Where it's supposed to.\"" echo echo "The reason for the glib answer is that, at least in Debian-based distributions of GNU/Linux like Debian, Ubuntu, and of course INX, (which is really Ubuntu underneath), the installation system sees to this. Unlike some operating systems, Linux-based systems put files for a program in a number of places. The configuration files tend to go in /etc, and most \"user\" programs have their executable binaries in /usr/bin . " echo echo "If you need to know where the executable command is, you can find it using these commands:" echo yellow echo "type" echo echo "which" white ; echo echo "An example would be" yellow ; echo echo "which htop" echo ; white echo "Try it here:" echo ; unbold /bin/bash echo ; bold echo "Now try" yellow ; bold ; echo echo "which cd" white ; echo ; unbold /bin/bash echo ; bold echo "What happened?" echo echo "\"cd\" is a shell built-in. The \"which\" command searches your PATH, which is a list of directories from which you are permitted to run executable programs. So it doesn't find \"cd\" ." echo echo "Now try" yellow ; echo echo "type cd" white ; unbold ; echo /bin/bash echo ; bold echo "The \"type\" command is more general - it will find shell built-ins and commands in your PATH as well." echo "These tricks can be useful as shorthand - for example rather then typing" echo ; yellow echo "ls -l /usr/local/bin/playinx" echo ; white echo "We can type " echo ; yellow echo "ls -l \`which playinx\` " echo ; white echo "You will recall that backticks make bash substitute the result of the command within them." echo "Give it a try:" unbold ; echo /bin/bash echo ; bold echo "Final screens coming up... aliases and very basic scripting..." f-tutescape bold ; cyan ; echo echo "Also Known As..." echo ; white cat <