Pages

Friday, 13 April 2012

Bash: List of commands (Executing Commands Sequentially)


The bash shell has an interesting feature where one is able to execute a list of commands sequentially. A list refers to a sequence of one or more commands that are separated by the following operators; '&', '&&', '||', ';', or the new line character.

Operator Meaning Example
&
Sends processes to the background. This effectively creates a sub-shell for each command that ends with the & operator. One can view the list of background processes by typing the jobs command Command1 & Command2 & Command3 &
&&
This operator represents the AND option. What is essentially means is that a command will execute if and only if the/a previous command returns with a zero exit status/return value. Command1 && Command2 && Command3
||
This operator represents the OR option. A command will only be executed by the bash shell if and only if the previous command returned with a non-zero exit status. Command1 || Command2 || Command3
;
The shell executes commands separately this operator sequentially. This means that the shell waits for the previous command to complete executing and return control to the shell that it executes the next shell. This command is similar to separating commands using the new line. Command1; Command2; Command3
\n
This command represents the new line command. It is similar to entering a first command followed by pressing the “Enter” or “Return” keyboard key. Command1 \n Command2 \n Command3





Sunday, 20 November 2011

Vi Tip: Prepending Text to multiple lines


There are cases when you want (for many reasons), to add same text to the beginning of multiple lines. This may happen when you are coding (say python) and then you realise that you need to enclose your function within a class. So you add the class definition at the start of the module file. You then need to indent the function, as python requires. To indent you need to add four spaces (or your preferred number). In vi, you do this like so;

  1. Go to the starting line that you intend to prepend your desired character(s).
  2. Press Ctrl + V
  3. Press the down arrow or Enter/Return key to the last line that you intend to prepend your desired character(s)
  4. Press Shift + i (lowercase i )
  5. Enter the character(s). In our python example, this would be the desired number of spaces (I usually use four)
  6. Then press the Esc key one.
  7. Your entered characters should appear in a few seconds.