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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

# May need to also press ENTER if a key not listed above pressed.<br />

read −n3 key<br />

# Read 3 characters.<br />

echo −n "$key" | grep "$arrowup" #Check if character code detected.<br />

if [ "$?" −eq $SUCCESS ]<br />

then<br />

echo "Up−arrow key pressed."<br />

exit $SUCCESS<br />

fi<br />

echo −n "$key" | grep "$arrowdown"<br />

if [ "$?" −eq $SUCCESS ]<br />

then<br />

echo "Down−arrow key pressed."<br />

exit $SUCCESS<br />

fi<br />

echo −n "$key" | grep "$arrowrt"<br />

if [ "$?" −eq $SUCCESS ]<br />

then<br />

echo "Right−arrow key pressed."<br />

exit $SUCCESS<br />

fi<br />

echo −n "$key" | grep "$arrowleft"<br />

if [ "$?" −eq $SUCCESS ]<br />

then<br />

echo "Left−arrow key pressed."<br />

exit $SUCCESS<br />

fi<br />

echo −n "$key" | grep "$insert"<br />

if [ "$?" −eq $SUCCESS ]<br />

then<br />

echo "\"Insert\" key pressed."<br />

exit $SUCCESS<br />

fi<br />

echo −n "$key" | grep "$delete"<br />

if [ "$?" −eq $SUCCESS ]<br />

then<br />

echo "\"Delete\" key pressed."<br />

exit $SUCCESS<br />

fi<br />

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

echo " Some other key pressed."<br />

exit $OTHER<br />

# Exercises:<br />

# −−−−−−−−−<br />

# 1) Simplify this script by rewriting the multiple "if" tests<br />

#+ as a 'case' construct.<br />

# 2) Add detection of the "Home," "End," "PgUp," and "PgDn" keys.<br />

The −t option to read permits timed input (see Example 9−4).<br />

The read command may also "read" its variable value from a file redirected to stdin. If the file<br />

contains more than one line, only the first line is assigned to the variable. If read has more than one<br />

Chapter 11. Internal Commands and Builtins 129

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

Saved successfully!

Ooh no, something went wrong!