Personal tools
You are here: Home wiki bash scripting
Views

Edit history

Edit: -1 of 1
Time: 2007-11-08 15:20:04
Note: /copilotconsulting.com/wiki/BashScripting/PUT

changed:
-
Kill all processes with a certain name::

  ps -ef | grep "imap$" | awk '{print $2}'


Several different ways to do for loops. My preference::

  #!/bin/bash
  for i in `seq 1 10`;
  do
      echo $i
  done


Combine these::

  for pid in `ps -ef | grep "imap$" | awk '{print $2}'`;
  do
      kill $pid
  done


Fragment of my Amazon S3 backup script for my Amazon EC2 instance::

  # Make a new backup and upload it
  DATE=`/bin/date +%u`

  tar -zcvf amazon-$DATE.tar.gz --exclude /proc --exclude /sys  \
  --exclude /mnt/squid --exclude /mnt/tmp --exclude /mnt/log    \
  --exclude /mnt/mail-archives /

  split -b 1073741824 amazon-$DATE.tar.gz amazon-$DATE.tar.gz- 

  rm amazon-`/bin/date +%u`.tar.gz

  for file in /mnt/tmp/amazon*; do
      ./s3cmd.rb --progress put copilot-amazon:$file $file && rm $file;
  done


Features: 

- Use backticks with date command

- Use variables to remove repetition

- Use for loop to iterate over list

- Escape carriage return at end of long lines


Handy procmail mail sorting uses shell-like tricks also. Date example::

  :0
  * ^List-Id: Main Discussion List for KPLUG <kplug-list.kernel-panic.org>
  .lists.kplug_`/bin/date +%m%y`/

Renaming files::

  for file in .*_1106; do
	  newfile=`echo $file | sed -e 's/_/./' | sed -e 's/^.lists.//'`
	  echo $newfile
	  mv $file $newfile
  done


Using find to manage IMAP folder subscriptions::

  find /home/treed/Maildir -name ".??*"                       \
  -maxdepth 1 \! -name .customflags \!  -name .subscriptions| \
  sed -e 's/\/home\/treed\/Maildir\///' > /home/treed/Maildir/.subscriptions


Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: