27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

# Stephane Chazelas suggests the following alternative:<br />

usage() {<br />

echo "Usage: `basename $0` C−program−file" >&2<br />

exit 1<br />

}<br />

WEIRD=`echo −n −e '\377'` # or WEIRD=$'\377'<br />

[[ $# −eq 1 ]] || usage<br />

case `file "$1"` in<br />

*"C program text"*) sed −e "s%/\*%${WEIRD}%g;s%\*/%${WEIRD}%g" "$1" \<br />

| tr '\377\n' '\n\377' \<br />

| sed −ne 'p;n' \<br />

| tr −d '\n' | tr '\377' '\n';;<br />

*) usage;;<br />

esac<br />

# This is still fooled by things like:<br />

# printf("/*");<br />

# or<br />

# /* /* buggy embedded comment */<br />

#<br />

# To handle all special cases (comments in strings, comments in string<br />

# where there is a \", \\" ...) the only way is to write a C parser<br />

# (lex or yacc perhaps?).<br />

which<br />

exit 0<br />

which command−xxx gives the full path to "command−xxx". This is useful for finding out whether a<br />

particular command or utility is installed on the system.<br />

$bash which rm<br />

/usr/bin/rm<br />

whereis<br />

Similar to which, above, whereis command−xxx gives the full path to "command−xxx", but also to<br />

its manpage.<br />

$bash whereis rm<br />

whatis<br />

rm: /bin/rm /usr/share/man/man1/rm.1.bz2<br />

whatis filexxx looks up "filexxx" in the whatis database. This is useful for identifying system<br />

commands and important configuration files. Consider it a simplified man command.<br />

$bash whatis whatis<br />

whatis<br />

(1) − search the whatis database for complete words<br />

Example 12−25. Exploring /usr/X11R6/bin<br />

#!/bin/bash<br />

# What are all those mysterious binaries in /usr/X11R6/bin?<br />

Chapter 12. External Filters, Programs and Commands 181

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!