Tag Archive for Shell

Remove beeping from linux

sudo modprobe -r pcspkr

source

shell script menu

#!/bin/sh
echo "Use one of the following options"
echo "	d : run date command"
echo "	<?> : <another thing to do ...>"
echo "	q: quit this menu"
echo "enter your choice or type "q" to quit : c"
read option
while [ "$option" != "q" ]
do
case $option in
d ) date
shift
echo "enter your choice or type "q" to quit : c"
read option
;;
<?> ) <another command...>
shift
echo "enter your choice or type "q" to quit : c"
read option
;;

#                       ...

* )
echo "invalid choice!"
shift
echo "enter your choice or type "q" to quit : c"
read option
;;
q ) exit 0
;;
esac
done
exit 0

source

Post to Twitter from Shell – Python Version

#!/usr/bin/python
# tweet.py
# usage:
# tweet.py message
#
# inpired by:

import sys
from os import popen

def tweet( message, user, password ):
print 'posting %s for %s' % (message, user)

url = 'http://twitter.com/statuses/update.xml'
curl = 'curl -s -u %s:%s -d status="%s" %s' % (user,password,message,url)

pipe = popen(curl, 'r')

if __name__ == '__main__':
if len(sys.argv) != 2:
print "Usage: tweet.py <message>"
sys.exit()

message = sys.argv[1]
if len(message) > 140:
print "Message too long"
sys.exit()

user = raw_input('Username: ')
password = raw_input('Password: ')

tweet(message, user, password)

source

Remove Many File from Shell

find . -name 'FILE*' | xargs rm

source

simple init.d script for django using fastcgi

#!/bin/sh
#
# chkconfig:	345 85 15
# description:	FastCGI server for Django

# Source function library
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

PROJ=myproject					# Django project name
PROJSPATH=/home/user/www			# The folder Django project is in
SOCKSPATH=/tmp					# Where you put UNIX socket file
PIDSPATH=/var/run				# Where you put .pid file
RUNAS=root					# Set a different user to run the FastCGI server

SRVNAME=fcgi-$PROJ				# Process name

# Maximum requests for a child to service before expiring
#MAXREQ=
# Spawning method - prefork or threaded
#METHOD=
# Maximum number of children to have idle
#MAXSPARE=
# Minimum number of children to have idle
#MINSPARE=
# Maximum number of children to spawn
#MAXCHILDREN=

start () {
# Check if the service is already running?
if [ ! -f $PIDSPATH/$SRVNAME.pid ]; then
echo -n $"Starting $SRVNAME..."
daemon --user $RUNAS $PROJSPATH/$PROJ/manage.py runfcgi pidfile=$PIDSPATH/$SRVNAME.pid
socket=$SOCKSPATH/$SRVNAME.sock
${MAXREQ:+maxrequests=$MAXREQ}
${METHOD:+method=$METHOD}
${MAXSPARE:+maxspare=$MAXSPARE}
${MINSPARE:+minspare=$MINSPARE}
${MAXCHILDREN:+maxchildren=$MAXCHILDREN}
${DAEMONISE:+damonize=True}
echo
chmod 777 $SOCKSPATH/$SRVNAME.sock
RETVAL=$?
else
echo $"$SRVNAME is already running."
fi
}

stop() {
# Stop daemons.
if [ -f $PIDSPATH/$SRVNAME.pid ]; then
echo -n $"Stopping $SRVNAME..."
killproc -p "$PIDSPATH/$SRVNAME.pid" -d 60 $SRVNAME
echo
# Delete pidfile only when Django was called successfully
if [ $? -eq 0 ]; then
rm -f $PIDSPATH/$SRVNAME.pid "$SRVNAME.pid"  >/dev/null 2>&1
fi
else
echo $"$SRVNAME is NOT running."
fi

}

RETVAL=0

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p "$PIDSPATH/$SRVNAME.pid" $SRVNAME
RETVAL=$?
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 3
;;
esac

exit $RETVAL

source

Linux shell commands from php script

$output=shell_exec('command 2>&1');

source

Command line args parser

<?php

function getArgs($args) {
$out = array();
$last_arg = null;
for($i = 1, $il = sizeof($args); $i < $il; $i++) {
if( (bool)preg_match("/^--(.+)/", $args[$i], $match) ) {
$parts = explode("=", $match[1]);
$key = preg_replace("/[^a-z0-9]+/", "", $parts[0]);
if(isset($parts[1])) {
$out[$key] = $parts[1];
}
else {
$out[$key] = true;
}
$last_arg = $key;
}
else if( (bool)preg_match("/^-([a-zA-Z0-9]+)/", $args[$i], $match) ) {
for( $j = 0, $jl = strlen($match[1]); $j < $jl; $j++ ) {
$key = $match[1]{$j};
$out[$key] = true;
}
$last_arg = $key;
}
else if($last_arg !== null) {
$out[$last_arg] = $args[$i];
}
}
return $out;
}

/*
sample

php file.php --foo=bar -abc -AB 'hello world' --baz

produces:

Array
(
[foo] => bar
[a] => true
[b] => true
[c] => true
[A] => true
[B] => hello world
[baz] => true
)

*/

?>

source

Shell script to make lists of certain file types in a web site file system

mkdir file_lists
find . -name '*.pdf' -o -name '*.PDF' > ./file_lists/all_pdf_files.txt
find . -name '*.pdf' -o -name '*.PDF' -mtime +365 > ./file_lists/pdf_files_mtime_gt_1yr.txt

find . -name '*.jpg' -o -name '*.JPG' -o -name '*.gif' -o -name '*.GIF' -o -name '*.png' -o -name '*.PNG' > ./file_lists/all_image_files.txt

find . -name '*.log' -o -name '*.LOG' > ./file_lists/all_log_files.txt

find . -name '*.css' -o -name '*.CSS' -o -name '*.js' -o -name '*.JS' > ./file_lists/all_design_assets_files.txt

find . -name '*.ram' -o -name '*.RAM' -o -name '*.aif' -o -name '*.AIF' -o -name '*.mp3' -o -name '*.MP3' > ./file_lists/all_audio_files.txt
find . -name '*.ram' -o -name '*.RAM' -o -name '*.aif' -o -name '*.AIF' -o -name '*.mp3' -o -name '*.MP3' -mtime +365 > ./file_lists/audio_files_mtime_gt_1yr.txt

find . -name '*.html' -o -name '*.HTML' -o -name '*.htm' -o -name '*.HTM' > ./file_lists/all_html_files.txt
find . -name '*.html' -o -name '*.HTML' -o -name '*.htm' -o -name '*.HTM' -mtime +365 > ./file_lists/html_files_mtime_gt_1yr.txt

source

Run Shell Command

for i in os.popen('dir'):
print i

source

Determine which number out of a list is being processed at the moment

Note that this works for a shell script that runs a perl script with a parameter (see related tip)

batch_name is the name of the perl script that is being run. This can be abbreviated to whatever will uniquely identify it in grep
param_file is the name of the file that parameters are taken from. This needs to be the full name (e.g. param_file.txt)

ps -ef |grep batch_name |awk '{print "grep -n " $10 " param_file"}'|sh

source