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.

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

Echoes the name of the current user's terminal. Note that each separate xterm window counts as a<br />

different terminal.<br />

stty<br />

bash$ tty<br />

/dev/pts/1<br />

Shows and/or changes terminal settings. This complex command, used in a script, can control<br />

terminal behavior and the way output displays. See the info page, and study it carefully.<br />

Example 13−1. setting an erase character<br />

#!/bin/bash<br />

# erase.sh: Using "stty" to set an erase character when reading input.<br />

echo −n "What is your name? "<br />

read name<br />

echo "Your name is $name."<br />

stty erase '#'<br />

echo −n "What is your name? "<br />

read name<br />

echo "Your name is $name."<br />

# Try to erase characters of input.<br />

# Won't work.<br />

# Set "hashmark" (#) as erase character.<br />

# Use # to erase last character typed.<br />

exit 0<br />

Example 13−2. secret password: Turning off terminal echoing<br />

#!/bin/bash<br />

echo<br />

echo −n "Enter password "<br />

read passwd<br />

echo "password is $passwd"<br />

echo −n "If someone had been looking over your shoulder, "<br />

echo "your password would have been compromised."<br />

echo && echo # Two line−feeds in an "and list".<br />

stty −echo<br />

# Turns off screen echo.<br />

echo −n "Enter password again "<br />

read passwd<br />

echo<br />

echo "password is $passwd"<br />

echo<br />

stty echo<br />

# Restores screen echo.<br />

exit 0<br />

A creative use of stty is detecting a user keypress (without hitting ENTER).<br />

Example 13−3. Keypress detection<br />

Chapter 13. System and Administrative Commands 217

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

Saved successfully!

Ooh no, something went wrong!