- Mastering Linux Shell Scripting
- Andrew Mallett
- 216字
- 2021-07-30 09:40:46
Limiting the number of entered characters
We do not need functionality in the scripts we have used so far, but we may need to ask users to hit any key to continue. At the moment, we have set it up in such a way that the variable is not populated until we hit the Enter key. Users have to hit Enter to continue. If we use the -n
option followed by an integer, we can specify the characters to accept before continuing, we will set 1
in this case. Take a look at the following code extract:
#!/bin/bash read -p "May I ask your name: " name echo "Hello $name" read -n1 -p "Press any key to exit" echo exit 0
Now, the script will pause after displaying the name until we press any key; literally, we can press any key before continuing, as we accept just 1
key stroke. Whereas, earlier we were required to leave the default behavior in place, as we could not know how long a entered name would be. We have to wait for the user to hit Enter.
Tip
Note that we add an additional echo here to ensure that a new line is issued before the script ends. This ensures that the shell prompt starts on a new line.
- 深入核心的敏捷開發:ThoughtWorks五大關鍵實踐
- 大學計算機基礎(第二版)
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- Building Mobile Applications Using Kendo UI Mobile and ASP.NET Web API
- Python Data Analysis(Second Edition)
- 概率成形編碼調制技術理論及應用
- Create React App 2 Quick Start Guide
- Python語言實用教程
- .NET 4.5 Parallel Extensions Cookbook
- UML2面向對象分析與設計(第2版)
- 寫給青少年的人工智能(Python版·微課視頻版)
- Python第三方庫開發應用實戰
- PHP+MySQL Web應用開發教程
- Learning Shiny
- Getting Started with Windows Server Security