Learning the Unix Programming Environment

De Pontão Nós Digitais
Revisão de 15h03min de 2 de outubro de 2014 por V1z (discussão | contribs) (basic stuff up to apropos)
Ir para navegaçãoIr para pesquisar


This is a series of practical tutorials on Unix - mainly GNU/Linux and OSX - in the context of modern programming practice. It is an up-to-date learning supplement to the great UPE book, "The Unix Programming Environment" by Kernighan and Ritchie. In addition to summarizing the outstanding classic and adapting it for current environments, LUPE also gathers a number of valuable practical tips and pointers from experienced programmers.

This Page is Under Development

Lots-of-tiny-code-reddit191578.png

First: read UPE

Even though LUPE is reasonably self-contained and covers what is most immediately useful to you, reading the original masterpiece - UPE - is highly recommended. Specially the exercises. Reading UPE is the top way of learning Unix at a level most useful to programmers. Reding it will be the single thing that really makes you understand the tools, the idiosyncratic syntax, and why Unix's highly programmable interface works they way it does. Really understanding the practical tools and the power user side of the OS is what makes a highly efficent programmer and user. With the LUPE supplements you'll leverage that powerful knowledge into the broader context of today's practice of programming and advanced usage.


Background

Basics

Open a terminal (by launching the Terminal app). Play some commands and constructs of the Shell environment language:

List files in the current directory/foder:

ls

A common shortcut for ls is d, though it is not always available. Try it anyways:

d

In the aliases section we'll set this useful shortcut up in case you don't have it by default.

Also try these

ls -l

For which a useful short is

ll

Don't worry if you don't have ll - we'll set it up later.

Tell the computer to print "Hello, World":

echo hello, world

Then tell it to print this 10 times using a for loop in the bash language (just type it for now - we'll help you understand the syntax idiosyncrasies later):

for i in `seq 1 10`; do echo hello, world; done

Each command is an executable program, which is usually written in C or can be internally modeled with C's abstraction. As in C, each command has a return value and default/standard input and output handles (stdout/stdin).

The input and output can be redirected

Create a text file containing hello, world using the redirect operator

echo hello, world > a.txt

Print out the contents of the file

cat a.txt

You can use the redirect operator on any command construct that has an output, even on the for construct above:

for i in `seq 1 10`; do echo hello, world; done > a.txt

Count the number of words in the file

wc -l a.txt

The UPE book masterfully explains why you should prefer that over (try it)

cat a.txt | wc -l 

You'll understand what the pipe `|' means later, but it basically connects the standard output of a program to the standard input of another.

Command History

history

Gives a numbered list of your previous commands. You can ask for previous command number 17 using

!17

Instead of pressing UP multiple times. Another way is to search history by keyword as you type. Lets say you're trying to reverse search the above for statement. You can do this:

  1. type Control-R
  2. type any substring of the desired command, say for
  3. a command matching that substring is displayed
  4. press Enter to execute it, or keep pressing Control-R to search for other options

You can also refer to the previous command's 3rd argument on your current command:

wc -l a.txt
cat !!:2

Your history is located in your home directory, which can be referred to by '~'

cat ~/.history

On many setups this will contain the history up to a previous session. The current history is usually not stored in ~/.history until the session is over.

Further Basics

Pressing Control-C usually aborts any command that is running interactively through the shell. Other commands that are not blocking the shell can be ended using kill and killall:

killall firefox

Or kill, which requires a process ID associated to a running app:

ps -A|grep firefox
   553
kill 553

If it doesnt go away, use -9 or -KILL

killall -9 firefox
kill -KILL 553

A reference to any single command is provided by man:

man bc

The standard sections of the manual include:

 1      User Commands
 2      System Calls
 3      C Library Functions
 4      Devices and Special Files
 5      File Formats and Conventions
 6      Games et. Al.
 7      Miscellanea
 8      System Administration tools and Deamons

You can ask man to look up an entry on a determined manual. If you want the C function, you can skip shell commands by using

man 3 printf

To search where a given keyword, eg exec, appears in all the manuals

apropos exec


Links