Next Previous Contents

## 8. `CLI` only

## 8.1 What is `CLI`?

`CLI` is a shorthand to the Comand Line Interface. When you are installing Linux on your computer without X, you will work in `CLI`-mode! Perhaps you will shout "oh... that's horrible", but your computer will shout "yeah... I have more %CPU and %mem to work and to play!".

## 8.2 Why does one talk about `CLI` here?

Some wearables may have problems with graphics chipsets, disk and memory space and battery-life. If you work in text-mode, you will save battery-life and disk usage as well as lot of memory and CPU Cycles. And if you don't have to install graphic interfaces, you will save a little disk-space too. Consequently, you gain some space for your data. But you may feel that in text-mode, nothing can be done. As you will see the same things can be done in text-mode and graphic environment. Only things are thought differently.

## 8.3 What can be done in text-mode?

We have to think with what we have few programs who can communicate between them by input/output canals. This type of environment implies that we must use all our fingers to work, we can even get rid of the mouse. As in `X`, you have editors (`Vi`, `Emacs`, `Jed`...), games ( BTW wearables are the game by themselves ), viewers/browsers ( `?less`, `?more`, `lynx`, `links` ...), file managers ( `mc`...) and more. Also, some people may believe that `CLI` is cool but it's difficult to learn all configurations and options of all commands. The learning curve is acutally steeper, but when you have learnt that, you will work faster and the faster the work is done the better it is with a wearable . We'll see examples which accelerate our personal work.

## 8.4 Bunch of utilities

### Shell and script-language

Bases of `UNIX` are its powerful shells. With shells you can do more than the poor batch-language of Microsoft. `UNIX` gives a lot of powerful shells (`tcsh`, `ksh`, `bash`...), but I always work with `sh`. I know it is old and less featured than its big brothers but it is on every Unices. In `sh`, there are often used functions/commands (`echo`, `test`). Why do I say that? You can notice that `GNU` gives a program `echo` and `test` and I say: "if we can eliminate these programs, we can free disk-space... ok, not too much but about 20k.". And some versions of `sh` are very economical. The language of shell (script) is like a small programming language: you can used loops (`for`, `while`), user interactions (`read`), I/O (< >)... To learn scripting, you just have to type: `man sh` (or `tcsh`.... but more complex...). Stupid example of a little script: `for i in * .[^.]*; do echo $i; done` (simple `ls`).

### Must I learn `sed` and `AWK`?

In the Unix's world, we hear a lot about `AWK` and `sed`. These programs are generic and can be used for a lot of things. `GNU` gives a bunch of utilities that can replace `sed` and `AWK` (`dd`, `cut`, `seq`, ...). Why `dd` will you ask ?

`dd` have a little function that is fine: conversion low/up case. An example:

There are names in this directory that are in uppercase but you want to change them to lowercase. With `AWK`, you must type: `for i in *; do mv "$i" "`echo $i | awk '{print tolower($0)}'`"; done`; with `sed` you must enumerate all letters; with dd, it's very easy, I think: `for i in *; do mv "$i" "`echo $i | dd conv=lcase`"; done`

`cut` is a program to print columns of a text. Also, if you must print different columns of a line, you can use `cut`. `cut` performs better than `AWK` in this case if you want the job to be done fastly and efficiently because `cut` is dedicated to this work. For the same task, you may use the shell's internal commands too (you can, if you assign a value to the `IFS` variable). Here is an example in `AWK`, `cut` and `sh`. We want only to display a list with `login : identity` fields:

- in AWK:

      awk -F: '{ print $1" : "$5}' /etc/passwd

- with `cut`:

      while read line; do echo "`echo $line | cut -d: -f 1` : `echo $line | cut -d: -f 5`"; done < /etc/passwd

- only with sh:

      IFS=':'; while read a b c d e f; do echo "$a : $e"; done < /etc/passwd; IFS=' '

Generally, you haven't to learn `AWK`. I think that you can always do things without `AWK`. (OK, sometimes, `AWK` is easier.)

About `sed`, the drawback is that you must work with temporary files. If you want to save disk-space and to edit files in command-line, you can use `ex`, the script version of `vi`. Also, `sed` can be used but not necessarily.

### Redundancies in utilities?

If disk-space is very important, you can delete certain programs which perform task that can be done by others programs. For example: if you have to use `dd`, you don't need `cat`, if you have `vi`, you don't need `ed` (help me to find other examples...).

## 8.5 Aliases or scripts?

Scripts are more powerful than aliases. But scripts eat disk-space and are loaded each time they are used. Aliases eat memory-space and if you are in `CLI`, you have all the memory for you! Aliases are faster than scripts because they are loaded from memory and not from disk.

Generally, shells offer you another alternative for aliases/scripts: functions. Functions have power of scripts with the convenience to eat only memory-space. To learn aliases and functions, you can look at the manpages.

Next Previous Contents