[ubuntu] Unlock /var/lib/dpkg/lock when you’re locked out

Tags

, , , , , , , , ,

You must have seen this (that’s why you’re here after all!)


E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

This often happens if you try to install a package (say, <package-name>) while you are installing another. As an example, this may happen when you try to run
$ sudo aptitude install <package-name>
in the terminal while you have Synaptic package manager open (or vice versa) or in another terminal you are installing another package . The easiest solution is just to wait for the other installation(s) to finish  and close the package manager if you are done with it. However, if the package manager is crashed in the middle some stuck-up  processes may still be using the lock (/var/lib/dpkg/lock).

In that case, use fuser to find out the runaway process(es); and while you’re at it, you may use -k flag which will kill the process that is still using /var/lib/dpkg/lock. Then configure (--configure) all the packages (-a) which are yet unpacked and unconfigured, using dpkg:


$ sudo fuser -vki /var/lib/dpkg/lock; sudo dpkg --configure -a

In the first command (fuser), the -i flag asks for user confirmation, and -v is for verbose mode.

After that, proceed to the usual installation step of the package that you want to install.

[bash] Introductory BASH scripting for scientists

Tags

, , , , , , , , , , , , , , ,

These are based on the type of work I am interested in, i.e. scientific programming: you won’t see much about character strings in this post since we are mostly interested in numbers!

These can all be directly typed in the BASH prompt. Semicolons basically indicate the start of a new statement: you may put them in separate lines; and in that case, do not put the semicolon between the statements typed in different lines. The dollar sign ($) at the beginning of the command line indicates the command prompt and it is not a part of the command!

1. The basic
Print the sum of two integers:

$ echo $((2+3))
The two pairs of parentheses interpret them as numbers. You can also use square brackets (just a single pair):

$ echo $[2+3].

For floating point numbers use bc.

Variable substitutions may be done as
$ num1=10; num2=20; echo $(($num1+$num2))
(no space between the equality sign and the variable/number)

2. Conditional block: arithmetic conditional operators are eq, lt, le, gt and ge:

$ i=10; j=20; if [ $i -lt $j ]; then echo "$i<$j? True"; fi
$ i=$RANDOM;j=$RANDOM; if [ $i -lt $j ]; then echo "i=$i is less than j=$j"; else echo "i=$i is NOT less than j=$j";fi
($RANDOM equals a random number at each invocation)

Notice the  spaces between the square brackets and the expression within it.

3. Array:
Assignment:

$ arr=(10 11 12)
which is the same as

$ arr[0]=10; arr[1]=11; arr[2]=12

Following constructs show how you can actually access the array (link)

$ echo ${arr[2]}  # The 3rd element of the array
$ echo ${arr[*]}  # All of the items in the array
$ echo ${!arr[*]} # All of the indexes in the array
$ echo ${#arr[*]} # Number of items in the array
$ echo ${#arr[0]} # Length of item zero

4. For loop

$ for (( c=1; c<=5; c++ )); do echo "c = $c"; done            # C-style
$ for c in 1 2 3 4 5; do echo "c = $c"; done                  # List
$ arr=(1 2 3 4 5); for c in ${arr[*]}; do echo "c = $c"; done # Explicit array
$ for c in {1..5}; do echo "c = $c"; done                     # 1 thru' 5 with unit intervals
$ for c in {0..10..2}; do echo "c = $c"; done                 # 0 thru' 10 with interval=2

5. While and until loops
$ c=0; while [ $c -lt 10 ]; do echo "c = $c"; let c++; done
You may replace “let c++” by “((c++))”.

$ c=20; until [ $c -lt 10 ]; do echo "c = $c"; let c--; done

6. User input
Use read command to read in user input, e.g.
$ echo "Enter two integers"; read num1 num2; echo "$num1+$num2=$((num1+num2))"
In the second echo statement, when num1 and num2 are not enclosed within two pairs of parentheses they are treated as character strings, whereas in the last part the double parentheses cause them to be treated as numbers.

N.B. For the above loops and blocks, a line break between do or then and the statement that immediately follows it is allowed.

Reference: link.

[awk] Standard deviation of a column of numbers in the command line

Tags

, , , , ,

Just picked this up from my favorite site: commandline-fu.

$ awk '{delta = $1 - avg; avg += delta / NR; mean2 += delta * ($1 - avg); } END { print sqrt(mean2 / NR); }'

Let’s test it by finding the standard deviation of 1, 2, 3, 4, and 5 whose standard deviation is sqrt(2): in BASH, pipe in the out put of the following (which just echoes out numbers from 1 through 5) to the above command to find that it is in fact sqrt(2) or 1.42421
$ for n in {1..5}; do echo $n; done

[cli] Reinovoke previous commands by changing a part of it

Tags

,

Replace foo by bar:
$ ^foo^bar

The above changes just one instance of foo by bar; to change multiple instances use:
$ !n:gs/foo/bar/
where n is the command number (replace it with “!” for previous command).

[awk] Print selective columns and watch for changes

Tags

, , , , , , , , , , ,

Print columns 1, 5 and then 2, put a colon, and then print column 10 of a line that matches pattern from the file columns.txt:

$ awk '/pattern/ {print $1, $5, $2, ": ", $10}' columns.txt

.
Protecting the special meaning of the single quotes and curly braces
I came across this situation when I was trying to watch the above command as well as two other commands (say, command1 and command2) at the same time. The correct way to do that is by properly protecting the meaning of the special characters in the above awk invocation:

$ watch -d -n 20 "command1; command2; awk '"'/pattern/ {print $1, $5, $2, ": ", $10}'"' columns.txt"

.
N.B. The “-d”flag highlights the changes, whereas the “-n 20” flag causes the above to watch every 20 seconds.

Just for the heck of it, let me put the full command that I was actually using:

$ watch -d -n 20 "ps aux | grep -i columbus | grep -v grep && echo; tail WORK/ciudgsm && echo && grep bond output.log | tail -13 && echo && awk '"'/state # 1/ {print $3,$4,$5,": ", $10}'"' output.log && echo; awk '"'/state # 2/ {print $3,$4,$5,": ", $10}'"' output.log && head curr_iter"

(suggestions for making the above shorter, other than by aliasing, are most welcome!).

Reference: here.

Design a site like this with WordPress.com
Get started