Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Thursday, September 10, 2009

Disabling Macbook Pro Touchpad

We may want to disable Macbook Pro (or any other Laptop) Touchpad, such cases are:
(i) We have connected a USB mouse, so we do not want to use touchpad for now.
(ii) While typing, there is no use of touchpad, if touchpad is too sensitive, the cursor keeps on jumping due to slight touch of palm or finger.

I have written a script to disable/enable touchpad (which is using synaptics driver).





#!/bin/bash

## Disable touchpad if USB Mouse is attached

SYNAPTICS=`which synclient`

if [[ "$SYNAPTICS" == "" ]]
then
echo "$0: please install synaptics touchpad driver."
echo "Also make sure that 'Option \"SHMConfig\" \"on\"'"
echo " is added in Touchpad device Section in /etc/X11/xorg.conf"
exit
fi

USB_mouse_present=`grep -ic "usb.*mouse" /proc/bus/input/devices`
# if no USB Mouse; enable touchpad
if [ $USB_mouse_present -eq 0 ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi

# if any parameter [on|off] is given, override previous command
if [ $# -ge 1 ]
then
if [ "$1" = "on" ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi
fi

exit 0



Sample Run:
Turn on touchpad

$ ./touchpad.sh on

Turn off touchpad

$ ./touchpad.sh off

If we have plugged USB mouse, then just give following command

$ ./touchpad.sh

On removing USB mouse, give following, the touchpad will be enabled automatically. :)

$ ./touchpad.sh


PS: The configuration for synaptics driver can be referred from here or here.

Friday, October 3, 2008

Shell Session Recording

In order to record the commands and their output in a shell session,
you can use script command. For example:

[mitesh@linuxbox:370:~]$ script
Script started, file is typescript
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
mitesh
[mitesh@linuxbox:373:~]$ exit
exit

Script done, file is typescript

You can view the output file typescript file using your favorite editor.
$ cat typescript
Script started on Friday 03 October 2008 04:51:27 PM IST
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
mitesh
[mitesh@linuxbox:373:~]$ exit
exit

Script done on Friday 03 October 2008 04:51:54 PM IST

PS: you can save the output to any file, you want instead of typescript in
current directory. For example
$ script shell_session_20081003.log
...
...
$ exit
$ cat shell_session_20081003.log

Wednesday, April 16, 2008

Group By Counts in a File (Generally csv files)

You can use following snippet to get GROUP BY - COUNT as in SQL
query. Like

If you want number of students in a department:

SELECT department, COUNT(*) FROM Student GROUP BY department;

Suppose that this file contains:

Mitesh Singh Jat,CSA
Mahesh Singh Sonal,CSA
Paneendra B A,SSA
Shrikant Joshi,SERC
Rupesh Bajaj,CSA
Chandrakant,SSA

1 #!/bin/bash
2
3 if [[ $# <> 4 ]]
4 then
5 echo "Usage: $0 file delim field_pos
6 exit
7 fi
8
9 CMD=`echo $1 | sed 's/.*\.gz/zcat/'`
10
11 if [[ $CMD != "zcat" ]]
12 then
13 CMD="cat"
14 fi
15
16 echo "========================"
17 echo "Buckets for $1"
18 echo "========================"
19 echo "Bucket_id Count"
20 echo "------------------------"
21 $CMD $1 | cut -d$2 -f$3 | awk '{sum[$1]++} END {for (x in sum) {out+=sum[x]; print x" \t"sum[x];} print "========================"; print "Total records : " out;}'
22 echo "========================"

$ ./group_by_count.sh Student , 2
========================
Buckets for Student
========================
Bucket_id Count
------------------------
SSA 2
SERC 1
CSA 3
========================
Total records: 6
========================