Pages

Saturday, March 31, 2012

sed: delete all lines before or after string match

delete all lines before string match including matching line


sed -i '1,//d' $FILENAME

delete all lines after string match including matching line

sed  -i  '//,$d' $FILENAME

-i option to put changes back into source file

Friday, March 30, 2012

error while loading shared libraries linux ldd

when you run a binary and get this kind of error, try this command:

ldd

this will show out put as

 bin]# ldd /usr/bin/curl
    linux-gate.so.1 =>  not found
    libcurl.so.4 => /usr/lib/libcurl.so.4 (0x00dd0000)
    libz.so.1 => /usr/lib/libz.so.1 (0x00be6000)
    libssh2.so.1 => /usr/lib/libssh2.so.1 (0x00110000)
    libc.so.6 => /lib/libc.so.6 (0x00a41000)
    librt.so.1 => /lib/librt.so.1 (0x00415000)

try to build again by linking the missing library file.

Thursday, March 29, 2012

increment variable in shell script

var=$((var+1))

shell script redirect

There are 3 file descriptors, stdin, stdout and stderr (std=standard).

Basically you can:
  1. redirect stdout to a file
  2. redirect stderr to a file
  3. redirect stdout to a stderr
  4. redirect stderr to a stdout
  5. redirect stderr and stdout to a file
  6. redirect stderr and stdout to stdout
  7. redirect stderr and stdout to stderr
1 'represents' stdout and 2 stderr. A little note for seeing this things: with the less command you can view both stdout (which will remain on the buffer) and the stderr that will be printed on the screen, but erased as you try to 'browse' the buffer.

 Sample: stdout 2 file

This will cause the ouput of a program to be written to a file.
        ls -l > ls-l.txt

Here, a file called 'ls-l.txt' will be created and it will contain what you would see on the screen if you type the command 'ls -l' and execute it.

Sample: stderr 2 file

This will cause the stderr ouput of a program to be written to a file.
        grep da * 2> grep-errors.txt

Here, a file called 'grep-errors.txt' will be created and it will contain what you would see the stderr portion of the output of the 'grep da *' command.

Sample: stdout 2 stderr

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.
        grep da * 1>&2 

Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways.

Sample: stderr 2 stdout

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.
        grep * 2>&1

Here, the stderr portion of the command is sent to stdout, if you pipe to less, you'll see that lines that normally 'dissapear' (as they are written to stderr) are being kept now (because they're on stdout).

Sample: stderr and stdout 2 file

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.
        rm -f $(find / -name core) &> /dev/null 

This (thinking on the cron entry) will delete every file called 'core' in any directory. Notice that you should be pretty sure of what a command is doing if you are going to wipe it's output.

set timeout in shell script

http://xpt.sourceforge.net/techdocs/nix/sch/sch07-Timeout/single/

shell script: to execute a string in variable as shell command

temp="ip=1.1.1.$p"
i wanted to extract ip address into a variable

solution:

eval ${temp}
myVar=$ip

Wednesday, March 28, 2012

ubuntu upgrade: restore applications collections

you can save a list of installed packages on the old machine with the command dpkg --get-selections > ~/packages and then restore it on the new one with sudo dpkg --set-selections < ~/packages && apt-get dselect-upgrade.

cat /proc/stat, cpu usage statistics

$ cat /proc/stat
cpu  24520180 352244 4425876 7951926999 3224192 100881 459528 0
cpu0 119971 3303 115696 498811501 11968 1 727 0
cpu1 156720 3887 74144 498757056 66536 2695 2103 0
cpu2 263824 60442 191283 498342148 157819 40037 7597 0
cpu3 220047 5331 54459 498698411 82321 1038 1524 0


 intr 1353911813 696186424 3 0 4 4 0 0 0 1 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ctxt 1255715782
btime 1327970777
processes 15808890
procs_running 2
procs_blocked 0

==>

first row columns values are sum of corresponding values in rest of the columns for all cpu cores.

in subsequent rows, first column has core id. rest columns hold,
  • user: normal processes executing in user mode
  • nice: niced processes executing in user mode
  • system: processes executing in kernel mode
  • idle: twiddling thumbs
  • iowait: waiting for I/O to complete
  • irq: servicing interrupts
  • softirq: servicing softirqs

The "intr" line gives counts of interrupts serviced since boot time, for each
of the possible system interrupts. The first column is the total of all interrupts serviced; each subsequent column is the total for that particular interrupt.


The "ctxt" line gives the total number of context switches across all CPUs.



The "btime" line gives the time at which the system booted, in seconds since
the Unix epoch.



The "processes" line gives the number of processes and threads created, which includes (but is not limited to) those created by calls to the fork() and clone() system calls.




The "procs_running" line gives the number of processes currently running on CPUs.



The "procs_blocked" line gives the number of processes currently blocked, waiting for I/O to complete.
copied from the kernel documentation of the /proc filesystem


Note:
The 8th column is called steal_time. It counts the ticks spent
executing other virtual hosts (in virtualised environments like Xen)
Note2:
With Linux 2.6.24 there is 9th column for (virtual) guest systems. See man 5 proc.


vimdiff commands

do - Get changes from other window into the current window.

dp – Put the changes from current window into the other window.

]c – Jump to the next change.

[c - Jump to the previous change.

Ctrl W + Ctrl W – Switch to the other split window.

Monday, March 26, 2012

virtualbox on Ubuntu 11.10 - guest windows 7 resolution problem - copying text to clipboard

1. When i installed win 7 as guest in virtualbox on my ubuntu 11.10, i was not able to set proper resolution, either it was too small or it was too large, that was not fitting the whole screen properly.

2. i was not able to copy text from virtual machine to clipboard

solution: install guest additions

how to : launch your virtual windows 7. on top menu, under devices, click "install guest additions"

this will first try to find VbGuestAddtions iso under your linux file systems. if it fails, it tries to fetch from web. if that also fails it will throw error that it is not able to find the iso file.

solution: do "sudo apt-get install virtualbox-guest-additions-iso" this will install manually.
then go to virtual win 7. and click install guest additions.

restart your vm. Now you should be able to toggle between full screen and minimized screen properly.

Also now you should be able to copy text between your VM and host machine properly.

Wednesday, March 21, 2012

cisco AnyConnect "wrong ELF class: ELFCLASS64" error

scenario: install cisco anyConnect to setup vpn connection on your laptop to connect to secured lan

problem: installation error: wrong ELF class: ELFCLASS64 when ./vpn_install.sh is run

cause: 32 bit library dependency

solution:
1. download getlibs-all.deb from net.
2.sudo dpkg -i getlibs-all.deb
3.getlibs libfuse.so.2
4.sudo ./vpn_install.sh

that should fix your problem.