Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Linux Shell Scripting Cookbook, Second Edition

You're reading from   Linux Shell Scripting Cookbook, Second Edition Don't neglect the shell ‚Äì this book will empower you to use simple commands to perform complex tasks. Whether you're a casual or advanced Linux user, the cookbook approach makes it all so brilliantly accessible and, above all, useful.

Arrow left icon
Product type Paperback
Published in May 2013
Publisher Packt
ISBN-13 9781782162742
Length 384 pages
Edition 2nd Edition
Tools
Arrow right icon
Toc

Table of Contents (16) Chapters Close

Linux Shell Scripting Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Shell Something Out FREE CHAPTER 2. Have a Good Command 3. File In, File Out 4. Texting and Driving 5. Tangled Web? Not At All! 6. The Backup Plan 7. The Old-boy Network 8. Put on the Monitor's Cap 9. Administration Calls Index

Reading n characters without pressing the return key


read is an important Bash command to read text from the keyboard or standard input. We can use read to interactively read an input from the user, but read is capable of much more. Most of the input libraries in any programming language read the input from the keyboard; but string input termination is done when return is pressed. There are certain critical situations when return cannot be pressed, but the termination is done based on a number of characters or a single character. For example, in a game, a ball is moved upward when + is pressed. Pressing + and then pressing return every time to acknowledge the + press is not efficient. In this recipe we will use the read command that provides a way to accomplish this task without having to press return.

How to do it...

You can use various options of the read command to obtain different results as shown in the following steps:

  1. The following statement will read n characters from input into the variable_name variable:

    read -n number_of_chars variable_name
    

    For example:

    $ read -n 2 var
    $ echo $var
    
  2. Read a password in the nonechoed mode as follows:

    read -s var
    
  3. Display a message with read using:

    read -p "Enter input:"  var
    
  4. Read the input after a timeout as follows:

    read -t timeout var
    

    For example:

    $ read -t 2 var
    #Read the string that is typed within 2 seconds into variable var.
    
  5. Use a delimiter character to end the input line as follows:

    read -d delim_char var
    

    For example:

    $ read -d ":" var
    hello:#var is set to hello
    
You have been reading a chapter from
Linux Shell Scripting Cookbook, Second Edition - Second Edition
Published in: May 2013
Publisher: Packt
ISBN-13: 9781782162742
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime