Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Compiling GNU coreutils on Mac OS X

# See:
# - http://www.gnu.org/software/coreutils/
# - http://en.wikipedia.org/wiki/GNU_Core_Utilities
# - http://homepage.mac.com/matsuan_tamachan/software/CoreUtils.html
# - http://www.mail-archive.com/bug-gnulib@gnu.org/msg08420.html

# In addition see:
# The Heirloom Project, http://heirloom.sourceforge.net
# http://homepage.mac.com/stefan.tramm/iWiki/HeirloomNotes.html
# http://codesnippets.joyent.com/posts/show/1632


path='
/usr/local/bin 
/usr/local/sbin
/usr/local/lib
/usr/local/include
/usr/local/man
/usr/bin
/bin
/usr/sbin
/sbin
'

path="$(printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E 's/^:|:$//g')"

export PATH="${path}"


echo $PATH
printf "%s\n" "$PATH" | tr ':' '\n'


alias curl=/usr/bin/curl
alias ls=/bin/ls
alias make=/usr/bin/make
alias nl=/usr/bin/nl
alias port=/opt/local/bin/port
alias tr=/usr/bin/tr

alias curl ls make nl port tr


# first compile & install gawk on Mac OS X
# http://www.gnu.org/software/gawk/

cd ~/Desktop
curl -L -O http://ftp.gnu.org/pub/gnu/gawk/gawk-3.1.6.tar.gz
tar -xzf gawk-3.1.6.tar.gz
cd gawk-3.1.6
./configure --help
./configure --disable-nls --prefix=/usr/local
make
/usr/bin/sudo /usr/bin/make install

/usr/local/bin/gawk --version


# alternative gawk installation
# port info gawk
# /usr/bin/sudo /opt/local/bin/port install gawk
# port installed | grep gawk
# /opt/local/bin/gawk --version
# export PATH="/opt/local/bin:${path}"


# compile & install the GNU coreutils

/usr/sbin/gcc_select -h
/usr/sbin/gcc_select -l
/usr/sbin/gcc_select      #  gcc version 4.0.1
#/usr/bin/sudo /usr/sbin/gcc_select 3.3

/usr/bin/sw_vers -productVersion    # 10.4.11

cd ~/Desktop
curl -L -O http://ftp.gnu.org/gnu/coreutils/coreutils-6.9.tar.gz
tar -xzf coreutils-6.9.tar.gz
cd coreutils-6.9
./configure --help
./configure --prefix=/usr/local/gnucoreutils
make
/usr/bin/sudo /usr/bin/make install


open -a Finder /usr/local/gnucoreutils
ls -1 /usr/local/gnucoreutils/bin | nl


export PATH="${PATH}:/usr/local/gnucoreutils/bin"
printf "%s\n" "${PATH}" | tr ':' '\n' | nl


# stat
which stat
/usr/bin/stat -x /dev/stdin
/usr/local/gnucoreutils/bin/stat -x /dev/stdin

/usr/local/gnucoreutils/bin/stat --help
/usr/local/gnucoreutils/bin/stat /dev/stdin

/usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/stat /usr/local/bin/gnustat
which gnustat
gnustat --version


# ls
ls --version
/usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/ls /usr/local/bin/gnuls
gnuls --version


# seq
which seq   #  /usr/local/gnucoreutils/bin/seq
/usr/bin/sudo /bin/ln -is /usr/local/gnucoreutils/bin/seq /usr/local/bin
which seq   #  /usr/local/bin/seq

seq --version
seq 5 25
seq 5 0.5 10


# For information on MANPATH see man man, man 5 man.conf and
# the "MANPATH /path/to/mandir" entries in: 
# sudo nano +40 /usr/share/misc/man.conf or 
# sudo nano +40 /private/etc/man.conf
# example: MANPATH /usr/local/gnucoreutils/share/man
# (in general make sure there is no MANPATH directory that is a symbolic link to another directory)

/usr/bin/manpath | tr ':' '\n' | nl
export MANPATH="${MANPATH}:/usr/local/gnucoreutils/share/man"
printf "%s\n" "${MANPATH}" | tr ':' '\n'

man seq

man -aW stat
man -aW stat | grep coreutils | xargs man
man -a stat


man 1 info
info --help
info info 2>/dev/null | less -p INFOPATH
export INFOPATH="${INFOPATH}:/usr/local/gnucoreutils/share/info"

Search with man less

# use n or N to iterate through matches
# [esc-u] turns off search highlighting

man bash | less -p 'ARITHMETIC EVALUATION'   
man bash | less -p 'Arithmetic Expansion'

help let

help test | sed -E "s/(Arithmetic tests)/$(printf '\e[1m\\1\e[m')/"

Create & print PDF files from the command line on Mac OS X

export PATH=/usr/bin:/bin:/usr/sbin:/sbin
export IFS=$' \t\n'

open -a 'Printer Setup Utility'

man 8 system_profiler
/usr/sbin/system_profiler  -listDataTypes
/usr/sbin/system_profiler SPPrintersDataType


defaultprinter=$(/usr/sbin/system_profiler SPPrintersDataType | egrep -B 5 'Default: Yes' | head -n 1 | sed -e 's/^[[:space:]]*//' -e 's/:$//')
printf "%s\n" "$defaultprinter"
lpstat -d | awk '{print $NF}'
alias opendefaultprinter='/usr/bin/open -a "$defaultprinter"'

opendefaultprinter


testfile="${HOME}/Desktop/testfile.txt"

/usr/bin/jot -b 'This is a test sentence!' 10 | cat -n > "$testfile"
open -e "$testfile"

/usr/bin/enscript -p ~/Desktop/testfile.ps "$testfile"
open ~/Desktop/testfile.ps

/usr/bin/enscript -p - "$testfile" | lpr 
opendefaultprinter


# use Bash process substitution instead of ~/Desktop/testfile.txt
# cf. http://wooledge.org:8000/ProcessSubstitution,
# http://wooledge.org:8000/BashFAQ (FAQ 20 & 24) and
# http://tldp.org/LDP/abs/html/process-sub.html

ls -l <((/usr/bin/jot -b 'This is a test sentence!' 10))
stat -x <((/usr/bin/jot -b 'This is a test sentence!' 10))
cat -n <((/usr/bin/jot -b 'This is a test sentence!' 10))

/usr/bin/enscript -p ~/Desktop/testfile.ps <((/usr/bin/jot -b 'This is a test sentence!' 10))
open ~/Desktop/testfile.ps

enscript -p - <((/usr/bin/jot -b 'This is a test sentence!' 10)) | lpr 
opendefaultprinter


man pstopdf
man enscript
enscript --help
enscript --list-media
enscript --list-options


/usr/bin/enscript -p ~/Desktop/testfile.ps "$testfile"
/usr/bin/pstopdf ~/Desktop/testfile.ps
open ~/Desktop/testfile.pdf

/usr/bin/enscript -p - "$testfile" | /usr/bin/pstopdf -i -o ~/Desktop/test.pdf
open ~/Desktop/test.pdf

echo 'Hello, world!' | enscript -q -B --word-wrap -f Helvetica30 -p - | lpr

lpstat -d
lpstat -W completed
lpstat -W not-completed
lpq -a
lpq -P $(/usr/bin/lpstat -d | /usr/bin/awk '{print $NF}')
opendefaultprinter

open http://127.0.0.1:631/printers
open "http://127.0.0.1:631/printers/$(/usr/bin/lpstat -d | /usr/bin/awk '{print $NF}')"


# clear print queue
function cpq() {
   /usr/bin/cancel -a -
   return 0
}


# get print job id 
lpq -P $(lpstat -d | awk '{print $NF}') | awk '{print $3}' | egrep "[[:digit:]]+"
lpstat -W not-completed | awk '{print $1}' | sed -n -E -e 's/^.*-([[:digit:]]+)$/\1/p'

# clear print queue
function cpq() {
defaultprinter=$(/usr/bin/lpstat -d | /usr/bin/awk '{print $NF}')
for jobid in $(/usr/bin/lpstat -W not-completed | awk '{print $1}' | sed -E -n -e 's/^.*-([[:digit:]]+)$/\1/p'); do
   #/usr/bin/cancel -u "$(/usr/bin/logname)" $jobid
   /usr/bin/lprm -P "$defaultprinter" $jobid
done
return 0
}


# list available fonts for man enscript
cat -n /usr/share/enscript/font.map

enscript -q -B --word-wrap -f Times-Roman25 -p - <((jot -b 'This is a test sentence!' 10 )) | pstopdf -i -o ~/Desktop/test.pdf
open ~/Desktop/test.pdf


man groff
groff --help

info groff
info --show-options groff

open http://www.groff-wiki.info
open http://www.gnu.org/software/groff/manual/groff.pdf
open /usr/share/doc/groff/1.19.1/html/momdoc/intro.html

# http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/pdfroff.1.html
open /usr/share/doc/groff/1.19.2/pdf/pdfmark.pdf


echo 'Hello, world!' | groff -Tps - | lpr

groff -Tps <((jot -b $'This is a test sentence!\n' 10)) | lpr

groff -Tps <((jot -b $'This is a test sentence!' 10)) > ~/Desktop/test.ps
open ~/Desktop/test.ps

echo 'Hello, world!' | groff -Tps - | pstopdf -i -o ~/Desktop/test.pdf
open ~/Desktop/test.pdf

groff -Tps <((jot -b $'This is a test sentence!\n' 10 )) | pstopdf -i -o ~/Desktop/test.pdf
open ~/Desktop/test.pdf


# cf. http://www.macgeekery.com/tips/cli/pretty-print_manual_pages_as_ps_pdf_or_html
function pdfman() { man $1 -t | open -f -a Preview; return 0; };
function printman() { man $1 -t | lpr; return 0; };

pdfman bash
printman bash

Reading manual pages with ManOpen

export PATH=/usr/bin:/bin:/usr/sbin:/sbin
export IFS=$' \t\n'

cd ~/Desktop
curl -LO http://www.clindberg.org/projects/ManOpen-2.5.1.dmg

hdiutil mount ~/Desktop/ManOpen-2.5.1.dmg

ls -ld /Volumes/ManOpen-2.5.1/*

/usr/bin/sudo /bin/cp /Volumes/ManOpen-2.5.1/openman /usr/bin

/usr/bin/sudo /bin/cp /Volumes/ManOpen-2.5.1/openman.1 /usr/share/man/man1

/usr/bin/sudo /bin/cp -R /Volumes/ManOpen-2.5.1/ManOpen.app /Applications

hdiutil unmount /Volumes/ManOpen-2.5.1

ls -ld /usr/bin/openman /usr/share/man/man1/openman.1 /Applications/ManOpen.app
man openman

alias om='openman'

om ls
om chflags
om openman