The TrainMe page, referring only to terminal programs.

Note: Syntax is this page is relevant to sh and bash.

Paragraphs in this page are:

a) Simplest command syntax
b) Shell - Programs intercommunication
c) Slightly more advanced command syntax
d) Simple programming with scripts



a) Simplest command syntax

cd /usr
ls
can be also:
cd /usr;ls -l With the ";" we separate commands inside a single line.



b) Shell - Programs intercommunication

1st pipes

With the help of pipes we can redirect program output to another program's input
cat /etc/fstab | grep swap
ls /home | tail -n 3

2nd second shell
       
We can also pass output of a program as a parameter to the next

grep -l samba $(find /etc -name '*.conf')

echo $((3*5))

3rd redirection

We can redirect an output to a file

cd ~ ; lsmod > modules.txt ; cat modules.txt ; rm modules.txt

cd ~ ; echo Hi I am a simple user > hi.txt ; cat hi.txt ; rm hi.txt

In these two simple lines we:
changed directory to home (just a precaution)
redirected the output (a file was created)
concatenated (typed) the contents of the file to the standard output
safely removed the file (no longer needed)

4th  named pipes (FIFOs), which works like the pipes communication.


Don't leave trash behind. Always delete files you created for testing.

       

c) Slightly more advanced command syntax

The ";" symbol is very versatile for making a script in a single command line But:

What if the previous command exits with no success?
If root, logout and relogin as a user.
 cd /home ; lsmod > modules.txt ; cat modules.txt

as a user you do not have permissions in /home. So the last command would return error.
       
Look at this:
cd ; lsmod > modules.txt && less modules.txt ; rm modules.txt

cat will operate Only If lsmod exits successfully. cd applied alone gets us to our home dir.

This is useful when configuring and compiling programs.
configure ; make IS WRONG while
./configure && make is right, because make will only compile if configure exits successfully.

Study carefully all the combinations between grep and find
grep -l %string $(find -name '%criteria'). For example:
cd /etc ; grep -l ifconfig $(find -name 'rc*')




d) Simple programming with scripts

1st Know exactly what you want

A script generally works with programs in a not interactive mode.
If you have made a wrong decision. do not expect from the program
to ask you as a human would do:
"what? remove /home/jane? But she is a user!"
The script would follow your instructions to the letter.

2nd Always use the right command

Let us say that we deleted some files inside a dir and now
we want the dir also to be deleted, if empty.

It would be wrong to rm -rf a directory that might have other data in it.
remember rmdir? It removes only empty directories.
So using this is safer and simpler.
Of course, we can check conditions, too.

After visiting the TipMe and ShellMe pages, go to ScriptMe page