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

Monday, January 03, 2011

Google Maps Hack for Bash Scripts

Here's a fun little hack. This lets you query Google maps from a bash shell script:
J="chicago"; \
K=`echo $J | sed "s/ /_/g"`; \
elinks --source http://maps.google.com/maps?q=$K | \
sed "s/}/\n/g" | grep "hnear\|latlng:" | \
sed -e "s/,+/+/g" -e "s/[,={\"]/\n/g" | \
grep "\+\|l..:[0-9\-]"

Chicago+Cook+Illinois
lat:41.878113999999997
lng:-87.629797999999994
Feed it "dakar" by setting $J, and you get:
Dakar+Region+Dakar+Senegal
lat:14.75
lng:-17.333333
What good is it? None, I'm sure.

Wednesday, January 27, 2010

BASH Press Enter Countdown Timer

Here's a little script snippet that displays a countdown timer and determines if the user pressed Enter. If the user does nothing, this returns 1, if the user presses Enter, it returns 2. Ultimately, we would fork based upon the action.
#!/bin/bash

for J in `seq 10 -1 1`; do K=1; echo -n "$J ";
  read -t 1 K
  if [ "$K" != 1 ]; then
    K=2; break
  fi
done

echo $K
Alternately, the user could press [Ctrl][C], in which case the script terminates.

Friday, April 10, 2009

Physical Layer Monitor

Here's a little script I whipped up to solve an argument as to whether the server was going to sleep or no packets were being routed into the box. The echo line could be tee'd for historical reference:
#/bin/sh
TRUE=1

while [ $TRUE = 1 ]; do
  killall tcpdump 2> /dev/null
  mv /dev/shm/netwatch.txt /dev/shm/netwatch-b.txt
    tcpdump > /dev/shm/netwatch.txt 2> /dev/null &
    IP=`grep -c " IP " /dev/shm/netwatch-b.txt`
    ARP=`grep -c " arp " /dev/shm/netwatch-b.txt`
    echo "`date +%T` packets=$IP arps=$ARP"
  sleep 10
done

Wednesday, June 13, 2007

Solaris Shell Scripting: Part 1

Shell scripting is suppose to be universal if you use #!/bin/sh. Yet, some if my scripting tricks are not working. A few notes:

Suppressing carriage return (CR):
Linux: echo -n "Hello "; echo "World"
Solaris: echo "Hello \c"; echo "World"

Capturing keyboard input:
Linux: prompt -p "Input: " VAR
Solaris: echo "Input: \c"; prompt VAR

Single command conditionals:
Linux: if [ -f /etc/passwd ]; then echo "Go"
Solaris: if [ -f /etc/passwd ]; then echo "Go"; fi

Finite count loop:
Linux:for X in {1..5}; do echo $Z; done
Solaris: X=0; while [ $X -lt 5 ]; do X=`expr $X + 1`; echo $X; done