Scripting

From Wikifications
Revision as of 18:22, 20 February 2006 by Dre (Talk | contribs)

Jump to: navigation, search

Theory

Redirection

>      Redirect standard output
2>     Redirect standard error
2>&1   Redirect standard error to standard output
<      Redirect standard input
|      Pipe standard output to another command
>>     Append to standard output
2>&1|  Pipe standard output and standard error to another command
|&     Same as above

for loop

for file in `ls` 
do
mv $file $file.copy 
done

if statement

if [ 1 = 1 ] ; then echo "yes" ; else echo "no" ; fi

====tests==== (for cli args):

if ( ! [ $1 ] || [ ! $2 ] ) ; then echo "yup" ; else echo "nope" ; fi

Practice

Shell scripts

====Disk Stats==== (zsh, all one line)

x=0 ; df -t hfs,afpfs | grep "/" | awk '{print $2}' | sed 's/G//g' | while read line ;\
do x=($line + $x); done ; echo "($x) / 1024 / 1024 / 2" | bc

Get console idle time

echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))

Sort all processes in ascending order by process priority

ps auxwwl | tr -s ' ' | cut -d' ' -f14,17-30 | sort -n

Conditional line printing

The following prints only users with UID greater than 500

dscl . -list /users uid UniqueID | awk '$2 > 500 { print $1 }'

Or the same thing with perl, using the autosplit feature, -a

dscl . -list /users UniqueID | perl -nae 'print "$F[0]\n" if $F[1] > 500;'

Profile elements

The following can be used in a .zshrc or equivalent

Dynamic Terminal Window Titles
case $TERM in 
        vt100*) 
        precmd () {print -Pn "\e]0;%n@%m: %~\a"} 
        ;; 
esac
Fancy prompt
PS1="$(print '%{\e[;31m%}%n%{\e[;0m%}@%{\e[;30m%}%m%{\e[0;37m%}[%~]%{\e[0m%}% ')"
Fancy aliases
alias yak="yak -Pn \"\e]0;.o0 yak 0o.\a\" ; ssh yak"

AppleScript

Droplet

on open some_items 
       
 display dialog "Where to, hoss?" buttons {"docroot", "bits", "public_html"} default button "bits" 
 set theButton to the button returned of the result 
       
 if theButton = "bits" then 
  set dest to "andre@dreness.com:/home/websites/dreness/bits" 
 else if theButton = "docroot" then 
  set dest to "andre@dreness.com:/home/websites/dreness" 
 else if theButton = "public_html" then 
  set dest to "andre@dreness.com:~/public_html" 
 else 
  display dialog "no valid choices!" 
 end if 
       
 repeat with this_item in some_items 
  try 
   do shell script "scp " & quoted form of the POSIX path of this_item & " " & quoted form of dest 
  end try 
 end repeat 
end open

Perl

QTSS Stats

#!/usr/bin/perl -w 
# This is a tiny script to parse QTSS's server_status to provide a running output of 
# statistics that is extremely low overhead. Refresh is every 10 seconds, 
# since that is how often the server_status file is updated by the server 
# 8/2/03, dre@mac.com 
#full path of server_status file 
$statsfile = "/Library/QuickTimeStreaming/Logs/server_status"; 
while (1) { 
open(STATS, "$statsfile");  
while ($line = <STATS>) {
if ($line =~ /<key>(.*?)<\/key>/) { 
$storein = "$1"; 
} elsif ($storein) { 
$line =~ /<string>(.*?)<\/string>/; 
$dss_stats{"$storein"} = "$1"; 
undef $storein; 
}; 
}; 
print `date`; 
print "Total RTP connections: $dss_stats{qtssRTPSvrTotalConn}\n"; 
print "Current RTSP / HTTP connections: $dss_stats{qtssRTSPHTTPCurrentSessionCount}\n"; 
print "Current RTP connections: $dss_stats{qtssRTPSvrCurConn}\n"; 
$rtp_kbits = sprintf("%.2f", ($dss_stats{qtssRTPSvrCurBandwidth} / 1000)); 
$rtp_mbits = sprintf("%.2f", ($rtp_kbits / 1000)); 
print "Current RTP bandwidth $rtp_kbits Kbit/s ($rtp_mbits Mbit/s)\n"; 
print "Current RTP packets: $dss_stats{qtssRTPSvrCurPackets}\n"; 
$rtp_bytes = $dss_stats{qtssRTPSvrTotalBytes}; 
$rtp_megs = ($dss_stats{qtssRTPSvrTotalBytes} / 1024 / 1024); 
$rtp_megs = sprintf("%.2f", $rtp_megs); 
$rtp_gigs = ($rtp_megs / 1024); 
$rtp_gigs = sprintf("%.2f", $rtp_gigs); 
print "Total RTP bytes transfered: $rtp_bytes ($rtp_megs MB, $rtp_gigs GB)\n"; 
close(STATS); 
print "\n"; 
sleep 10; 
};

SSH Tunnel Watcher

#!/usr/bin/perl
if ( `ps x | grep "ssh -L 2501:local:25" | grep -v grep` ) { 
        print "local mail ssh tunnel running okay! n"; 
        } else { 
        print "reloading... n"; 
        system("ssh -L 2501:local:25 -f -N local"); 
};
if ( `ps x | grep "ssh -L 2500:local:2501" | grep -v grep` ) { 
        print "public mail ssh tunnel running okay! n"; 
        } else { 
        print "reloading... n"; 
        system("ssh -L 2500:local:2501 -f -N -g local"); 
};