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 />

# The variables may contain embedded whitespace,<br />

#+ or even (horrors), control characters.<br />

# Excerpts from system file, /etc/rc.d/rc.sysinit<br />

#+ (on a Red Hat Linux installation)<br />

if [ −f /fsckoptions ]; then<br />

fsckoptions=`cat /fsckoptions`<br />

...<br />

fi<br />

#<br />

#<br />

if [ −e "/proc/ide/${disk[$device]}/media" ] ; then<br />

hdmedia=`cat /proc/ide/${disk[$device]}/media`<br />

...<br />

fi<br />

#<br />

#<br />

if [ ! −n "`uname −r | grep −− "−"`" ]; then<br />

ktag="`cat /proc/version`"<br />

...<br />

fi<br />

#<br />

#<br />

if [ $usb = "1" ]; then<br />

sleep 5<br />

mouseoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep −E "^I.*Cls=03.*Prot=02"`<br />

kbdoutput=`cat /proc/bus/usb/devices 2>/dev/null|grep −E "^I.*Cls=03.*Prot=01"`<br />

...<br />

fi<br />

Do not set a variable to the contents of a long text file unless you have a very good reason for doing so.<br />

Do not set a variable to the contents of a binary file, even as a joke.<br />

Example 14−1. Stupid script tricks<br />

#!/bin/bash<br />

# stupid−script−tricks.sh: Don't try this at home, folks.<br />

# From "Stupid Script Tricks," Volume I.<br />

dangerous_variable=`cat /boot/vmlinuz`<br />

# The compressed Linux kernel itself.<br />

echo "string−length of \$dangerous_variable = ${#dangerous_variable}"<br />

# string−length of $dangerous_variable = 794151<br />

# (Does not give same count as 'wc −c /boot/vmlinuz'.)<br />

# echo "$dangerous_variable"<br />

# Don't try this! It would hang the script.<br />

# The document author is aware of no useful applications for<br />

#+ setting a variable to the contents of a binary file.<br />

exit 0<br />

Chapter 14. Command Substitution 238

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

Saved successfully!

Ooh no, something went wrong!