Register now and start sharing your code snippets.
-->

Appending strings in bash without the line feed.

Shell Script (Bash) posted 5 months ago by marko

To append to a stream in bash without the trailing line feed use printf to format the output before appending it to the stream.

   1  i=42
   2  FILE=/tmp/atm
   3  printf "%s" "${i}" > $FILE

Tagged bash, printf, streams, linux

How to install sar on Debian

Shell Script (Bash) posted 6 months ago by christian

First install the sysstat package which includes sar:

   1  apt-get install sysstat

Then run crontab -e and add the following to collect stats:

   1  # Collect measurements at 10-minute intervals
   2  0,10,20,30,40,50   * * * *   /usr/lib/sa/sa1
   3  # Create daily reports and purge old files
   4  0                  0 * * *   /usr/lib/sa/sa2 -A

You now have access to IO and CPU usage history.

Tagged io, cpu, sar, usage, report, debian, linux

Compiling xvidcap for making screencasts in wmii

Shell Script (Bash) posted 7 months ago by marko

I was unable to make screencasts in wmii using Istanbul. So here’s how I installed xvidcap. Unfortunately I cannot come up with a way for wget retrieval of the sourceball, so some manual labour is needed. Retrieve the sourceball and put it in /tmp. Then follow the instructions below. Also see the snippet for an alias for the make command first.

Note that I put the binaries in /usr/local/stow/xvidcap because the package comes from outside package management. I then use xstow to manage the symlinks in /usr/local/bin.

   1  sudo apt-get install libxml-parser-perl libxmu-dev libglade2-dev libgtk2.0-dev
   2  cd /tmp
   3  tar zxvf xvidcap*.tar.gz
   4  cd xvidcap*
   5  ./configure --prefix /usr/local/stow/xvidcap
   6  make
   7  sudo make install
   8  cd /usr/local/stow
   9  sudo xstow xvidcap

To remove xvidcap follow these steps

   1  cd /usr/local/xstow
   2  xstow -D xvidcap
   3  sudo rm -rf /usr/local/stow/xvidcap

Tagged linux, xvidcap, xstow, screencasts in wmii

Linux here documents

Shell Script (Bash) posted 8 months ago by marko

Simple usage of a here document in Linux.

   1  marko@x61s:~$ cat << EOF > /tmp/hubbabubba
   2  > hubba
   3  > EOF
   4  marko@x61s:~$ cat /tmp/hubbabubba 
   5  hubba

Tagged here document, linux

Linux NAT with 3g PPP connection

Shell Script (Bash) posted 9 months ago by marko

I run this nat.sh script whenever I need to share my 3g connection to other computers in the switch I’m plugged into. Please note that it resets existing iptables rules.

   1  #!/bin/bash
   2  iptables --flush            
   3  iptables --table nat --flush
   4  iptables --delete-chain
   5  iptables --table nat --delete-chain
   6  
   7  echo 1 > /proc/sys/net/ipv4/ip_forward
   8  
   9  internal=eth0
  10  external=ppp0
  11  /sbin/iptables -t nat -A POSTROUTING -o ${external} -j MASQUERADE
  12  /sbin/iptables -A FORWARD -i ${external} -o ${internal} -m state --state RELATED,ESTABLISHED -j ACCEPT
  13  /sbin/iptables -A FORWARD -i ${internal} -o ${external} -j ACCEPT

Tagged nat, linux, iptables