Bash programming

top and ps

ps shows running processes and is helpful to check if your code is running or not.

ps -u praveen # show processes owned by user praveen
ps -ef        # gives lot more processess

top shows the processes taking most resources and is again useful to see whats going on in your computer.

Running programs in background

When a program prints a lot of stuff to the screen, it can slow down the code. It is always best to run it in background and direct the output to a file. For example

./a.out > log.txt &

If there is some error message, then you can capture them in the same file like this

./a.out > log.txt 2>&1 &

To ensure that the program does not get killed when you close your terminal or when you are running on a remote computer via ssh, use nohup like this

nohup ./a.out > log.txt 2>&1 &

.bashrc file

Set a simple command line prompt so you dont waste space

export PS1="$ "

Display hostname and current path in your terminal window bar

export PROMPT_COMMAND='echo -ne "\033]0;@$HOSTNAME:${PWD/#${HOME}/\~}\007"'

Conditional operators

^  operator  ^  produces true if...  ^  no. of operands  ^
|  -n   | operand has non zero length                                                      |  1  |
|  -z   | operand has zero length                                                          |  1  |
|  -d   | there exists a directory whose name is operand                                   |  1  |
|  -f   | there exists a file whose name is operand                                        |  1  |
|  -eq  | the operands are integers and they are equal                                     |  2  |
|  -neq | the opposite of -eq                                                              |  2  |
|  =    | the operands are equal (as strings)                                              |  2  |
|  !=   | opposite of =                                                                    |  2  |
|  -lt  | operand1 is strictly less than operand2 (both operands should be integers)       |  2  |
|  -gt  | operand1 is strictly greater than operand2 (both operands should be integers)    |  2  |
|  -ge  | operand1 is greater than or equal to operand2 (both operands should be integers) |  2  |
|  -le  | operand1 is less than or equal to operand2 (both operands should be integers)    |  2  |

For loops

A loop over integers:

for ((i=1;i<=10;i+=1)); do
echo $i
done

While loops

myvar=0
while [ $myvar -le 10 ]
do
    echo $myvar
    myvar=$(( $myvar + 1 ))
done