Linux/Shell Idioms

Bizen | Linux | Recent Changes | Preferences

OS

OS=`uname`

Linux () {
 echo Linux!
}

SunOS () {
 echo Solaris!
}

Darwin () {
 echo OSX!
}

${OS};

Unix Epoch to Human Time

 date -d @$EPOCH

Recursive chmod

Recursively chmod all directories or files to sane values:
 find . -type d -exec chmod 755 {} \;
 find . -type f -exec chmod 644 {} \;

Math

x=3
y=4

area=`bc -l << _EOF
$x*$y
_EOF
`

Iterating on a column

# convert a column to a space delimited list
foo=`grep $string $file | awk '{ORS=" "; print $1}'`
for arg in $foo;
do
echo $arg;
done;

Progress Bar

Wait and display a progress bar while the last backgrounded command runs:
while ps -p $! > /dev/null; do printf . ; sleep 1; done ; echo
But not if there's no terminal to write to:
## if connected to a terminal show a status tick. printf is posix.
tty -s && while ps -p $! > /dev/null; do printf . ; sleep 1; done ; echo
## if not, just idle until find returns.
tty -s || wait

Send Email

# /usr/ucb/mail for SunOS
echo $message | /bin/mail -s $subject $recipient

Filename Seperator

To change the filename seperator from " " (space) to nul:

 IFS=

find/xargs when the filenames may contain whitespace

# ls -l
total 5
-rw-r--r--  1 dkozak uucp  6 Sep  8 14:31 bar
-rw-r--r--  1 dkozak uucp  6 Sep  8 14:31 baz
-rw-r--r--  1 dkozak uucp 12 Sep  8 14:32 baz bar
-rw-r--r--  1 dkozak uucp  6 Sep  8 14:31 foo
-rw-r--r--  1 dkozak uucp 12 Sep  8 14:31 foo bar
# more *
::::::::::::::
bar
::::::::::::::
word2
::::::::::::::
baz
::::::::::::::
word3
::::::::::::::
baz bar
::::::::::::::
word3
word2
::::::::::::::
foo
::::::::::::::
word1
::::::::::::::
foo bar
::::::::::::::
word1
word2
# find . -print0 | xargs -0 grep word3
./baz:word3
./baz bar:word3

SSH From a Script Canonical Options

 /usr/bin/ssh -x -n -obatchmode=yes -oconnecttimeout=5 -oGSSAPIAuthentication=no

Generating a random hex string (for a temp file name for example)

 rand=`mcookie`

FQDN

 fqdn=`host -W 1 \`/bin/hostname\` | awk '{print $1}'`

 name=`uname -n`
 fqdn=`nslookup $name |grep -Po \\S+.edu`

Modify Behaviour if No TTY

 tty -s && echo 'Running in a terminal!'
 tty -s || echo 'Running via cron, for example.' >output.txt 

Which Tcl interpreter to use?

 #!/bin/sh
 ##
 ## A few lines of automatic interpreter choosing magic:
 ##
 # \
 [ -x /usr/bin/tclsh ]        && THE_RIGHT_TCL=/usr/bin/tclsh
 # \
 [ -x /usr/sfw/bin/tclsh8.3 ] && THE_RIGHT_TCL=/usr/sfw/bin/tclsh8.3
 # \
 [ -x /usr/sfw/bin/tclsh8.4 ] && THE_RIGHT_TCL=/usr/sfw/bin/tclsh8.4
 # \
 exec $THE_RIGHT_TCL "$0" ${1+"$@"}

One Time Encode/Decode

 # password encode a string with blowfish, and ascii armor it
 echo $plaintext | openssl enc -bf -base64
 # decode it!
 echo $blowfish_b64_text | openssl enc -d -bf -base64

List of installed rpm packages (name only)

 rpm -qa --qf "%{NAME}\n" | sort -u

List of installed deb packages (name only)

 dpkg --get-selections | grep install | sort -u | awk '{print $1}'
The output can be saved in a file and passed to apt-get install to clone

Right version of Grep?

 if  grep -V |grep -q GNU
  then
   echo "GNU grep is the default. Yay!"
  else
   echo "Crap! Wrong grep!"
   exit
 fi

Server Cert Info

 openssl s_client -connect $https-server:443 -showcerts

And sanity:

 #!/bin/ksh
 #
 # This script verifies proper ssl cert installation,
 # including CA cert bundle completeness.
 #
 # Phil Ehrens <pehrens@ligo.caltech.edu>
 # 
 openssl s_client -connect $1:443 -verify 10 2>&1 |grep Verify &
 sleep 1
 kill -s SIGTERM $! >/dev/null 2>&1
 #
 echo ""
 #
 openssl s_client -connect $1:443 -showcerts 2>&1 |egrep ' [0-9] s:' &
 sleep 1
 kill -s SIGTERM $! >/dev/null 2>&1

Copy over network, preserving ownership, perms, links

 tar -cf - $local-dir | ssh $target-host tar -xf - -C $target-dir

Or:

 rsync -r --partial --progress --rsh=ssh $local_file $remote_scp_style_target  

List our local machine names

 #!/bin/ksh
 for ((j=114 ; j<=115 ; j++))
 do
  for ((i=1 ; i<=256 ; i++))
  do
   addr=NNN.NNN.$j.$i
   name=`dig -x $addr +short`
   [ -z "$name" ] && name=$addr
   echo "$addr : '$name'"
  done
 done

Same, but extra feature

 #!/bin/ksh
 #
 # This script walks all over NNN.NNN.11[45]
 # and reports certs and their expiration dates.
 #
 # Phil Ehrens <pehrens@ligo.caltech.edu>
 #
 for ((j=114 ; j<=115 ; j++))
 do
  for ((i=1 ; i<=256 ; i++))
  do
   addr=NNN.NNN.$j.$i
   name=`dig -x $addr +short`
   [ -z "$name" ] && name=$addr
   echo "$addr : '$name'"
   openssl s_client -connect "$addr":443 >"$addr".tmp 2>/dev/null &
   sleep 2
   kill -s SIGTERM $! >/dev/null 2>&1
   openssl x509 -in "$addr".tmp -noout -subject -enddate 2>/dev/null
   rm -f "$addr".tmp
  done
 done

ksh - return file extension

 echo ${file##*.}

Compare RPM lists

 # operates on output of 'rpm -qa |sort'
 # shows files that are in $1 that are not in $2
 # you need to run it in both directions to compare
 # rpms on two servers.
 for rpm in $(cat $1)
 do
  grep $rpm $2 >/dev/null || echo $rpm
 done

SQLite query

 echo 'select * from $db_table;' | sqlite3 $db_name

D'oh!

 $a = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ';
 $b = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
Or:
 iconv --from-code=utf-8 --to-code=us-ascii//translit

Query Google

 http://www.google.com/search?q="Linux Rocks"

Set Title on an XTerm

 printf \\033]0\;\%s\\007 "$title"

Curl/Kerberos

 curl --insecure  -c /tmp/mycookies -b /tmp/mycookies --user albert.einstein --location-trusted https://wiki.ligo.org

Total memory used by a process in kb

#!/bin/ksh
#
# Returns total memory used by process $1 in kb.
#

IFS=$'\n'

for line in $(</proc/$1/smaps)
do
   if [[ $line =~ ^Size: ]]
   then
      IFS=' '
      set -A tmp $line
      ((kb += ${tmp[1]}))
   fi
done

print $kb
Terse version (no faster)
#!/bin/ksh
#
# Returns total memory used by process $1 in kb.
#

IFS=$'\n'

for line in $(</proc/$1/smaps)
do
   [[ $line =~ ^Size:\s+(\S+) ]] && ((kb += ${.sh.match[1]}))
done

print $kb

Regex result(s) into variable(s)

var='orl,bdl,lap'
pattern='^[^,]*,(.*)$'
[[ $var =~ $pattern ]]
newvar=${.sh.match[1]}

Bizen | Linux | Recent Changes | Preferences

Last edited August 25, 2011 3:17 pm by Pokute
Search Bizen: