Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 120 total  RSS 

WWAN Dialer

This script dials the built-in Verizon WWAN (mobile broadband) modem on my Thinkpad T61.

#!/bin/bash
#
# Dial the WWAN
#

hang_up () {
    echo 
    echo 'WWAN deactivated'
    echo
    exit
}

trap "hang_up" 2

echo 'Activating WWAN'
sudo modprobe -r airprime usbserial
sudo modprobe usbserial vendor=0x1199 product=0x0220
sudo modprobe airprime
sleep 0.5
sudo wvdial

Howto resize multiple pictures, graphics, images

for k in $(ls *.jpg); do convert -resize 800 -quality 80 $k r800-$k; done

Bash title random Quote routine

#
#this is a script in my .bash_profile that gets a random
#quote .txt file and assigns it to the terminal title.
#

quote_path="/Users/glynch/Documents/quotes/*"
files=($quote_path)
N=${#files[@]}
((N=RANDOM%N))
randomfile=${files[$N]}
quote=""
while read line; do quote="$quote $line"; done < $randomfile
echo -n -e "\033]0;$quote\007"

One-line web server in Ruby


# From: http://www.ntecs.de/blog/articles/2008/02/09/the-worlds-smallest-webserver
# Author: Michael Neumann
# ... point your browser to http://localhost:3125/etc/motd

ruby -rsocket -e 's=TCPServer.new(5**5);loop{_=s.accept;_<<"HTTP/1.0 200 OK\r\n\r\n#{File.read(_.gets.split[1])rescue nil}";_.close}'

Sort last 1000 shell commands by popularity

  history 1000 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

Bashy Perlness for generating Favicon text for OpenSearches

echo -n '<Image width="16" height="16">data:image/xicon,' ; perl -ne 's/(.)/"%".unpack("H2",$1)/egs; print' ~/Desktop/favicon.ico ; echo '</Image>' 

Changing the Bash prompt

The following shell command changes the bash prompt from this "james@cryton:~/projects/pear_housekeeping2/housekeeping$" to this "james@cryton> "
export PS1="\n\u@\H>"


I tried the above example on Ubuntu 7.10.

Reference:
Tip: Prompt magic [ibm.com]
Bash Prompt HOWTO [tldp.org]

Batch re-size a collection of images from the command line

for img in $(ls *.png); do convert $img -resize 75% smaller-$img; done;

Block many users in a system v0.1.1

Block many users in a system using a text file as argument

#!/bin/bash

#******************************************************************************#
# BlockManyUsers.sh - Block many users in a system using a text file           #
#                       as argument                                            #
#   Copyright (C) 2008 - written by flynets - <flynets<at>autistici<dot>org>   #
#   BlockManyUsers is free software: you can redistribute it and/or modify     #
#   it under the terms of the GNU General Public License as published by       #
#   the Free Software Foundation, either version 3 of the License, or          #
#   any later version.                                                         #
#                                                                              #
#   BlockManyUsers is distributed in the hope that it will be useful,          #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of             #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               #
#   GNU General Public License for more details.                               #
#                                                                              #
#   You should have received a copy of the GNU General Public License          #
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.      #
#******************************************************************************#

# Checks if you have the right privileges
if [ "$USER" = "root" ];then
   # Checks if there is an argument
   [ $# -eq 0 ] && { echo >&2 ERROR: You may enter as an argument a text file containing users, one per line. ; exit 1; }
   # checks if there a regular file
   [ -f "$1" ] || { echo >&2 ERROR: The input file does not exists. ; exit 1; }
   TMPIN=$(mktemp)
   # Remove blank lines and delete duplicates
   sed '/^$/d' "$1"| sort -g | uniq > "$TMPIN"
   
   NOW=$(date +"%Y-%m-%d-%X")
   LOGFILE="BMU-log-$NOW.log"

   for user in $(more "$TMPIN"); do
      # Checks if the user already exists.
      cut -d: -f1 /etc/passwd | grep "$user" > /dev/null
      OUT=$?
      if [ $OUT -eq 0 ];then
         # block selected user
         /usr/sbin/usermod -L "$user"
         # save user info in a file
         echo The user \"$user\" has been blocked. >> "$LOGFILE"
      else
        echo >&2 Error the user account \"$user\" doesnt exists! >> "$LOGFILE"
      fi
   done
   rm -f $TMPIN
   exit 0
else
   echo >&2 ERROR: You must be a root user to execute this script.
   exit 1
fi
exit 0

Convert files to avi

Bash one-liner that converts all *.flvs in the current directory to .avis.

Note: To convert something else, just change the .flv extension.

for i in *.flv; do mencoder -ovc lavc -oac mp3lame -o "$i.avi" "$i"; done
« Newer Snippets
Older Snippets »
Showing 1-10 of 120 total  RSS