- Linux自動化運維:Shell與Ansible(微課版)
- 楊寅冬主編
- 2270字
- 2024-07-26 17:04:22
1.2.4 數據輸入輸出
在Shell腳本中,可以使用多種命令來實現數據輸入輸出功能,其中常用的命令有echo命令、printf命令、read命令。
1. echo命令

V1-4 數據輸入輸出
echo是一個常用的Shell命令。它的主要功能是輸出字符串,可以將指定的文本字符串輸出到標準輸出(默認是屏幕),也可以用于輸出提示信息、調試信息、結果信息等。echo命令的基本語法如下。
echo [options] string
其中,options表示可選的命令選項;string表示要輸出的字符串。echo命令選項如表1-2所示。
表1-2 echo命令選項

使用以下命令輸出字符串,當輸出的字符串中包含空格或其他特殊字符時,通常使用引號標識字符串,例如:
[opencloud@server ~]$ echo "Hello, World!" Hello, World!
使用以下命令輸出帶有轉義符的字符串,例如:
[opencloud@server ~]$ echo -e "Hello,\tWorld!" Hello, World!
在腳本中,可以使用echo命令輸出變量的值,例如:
[opencloud@server ~]$ cat echo.sh #!/bin/bash name="John" age=18 echo "My name is $name,I am $age years old." # 執行腳本,輸出結果如下 [opencloud@server ~]$ bash echo.sh My name is John,I am 18 years old.
在Shell中,echo命令支持一些常見的轉義符,可以用來輸出特殊字符。在使用轉義符時,將字符串放在雙引號之內。echo命令支持的轉義符如表1-3所示。
表1-3 echo命令支持的轉義符

使用echo命令和常見轉義符的一些示例如下。
# 使用echo命令不換行輸出字符串,繼續在當前行輸出 [opencloud@server ~]$ echo -e "Hello,\cWorld!" Hello, [opencloud@server ~]$ # 輸出轉義符 [opencloud@server ~]$ echo -e "\e" # 輸出換頁符 [opencloud@server ~]$ echo -e "Hello,\fWorld!" Hello, World! # 輸出換行符 [opencloud@server ~]$ echo -e "Hello,\nWorld!" Hello, World! # 輸出水平制表符 [opencloud@server ~]$ echo -e "Hello,\tWorld!" Hello, World! # 輸出垂直制表符 [opencloud@server ~]$ echo -e "Hello,\vWorld!" Hello, World!
2. printf命令
在Shell腳本中,printf命令主要用于格式化輸出字符串,輸出帶有特定格式的信息,如輸出字符串、數字、字符、符號或者其他值。它與echo命令類似,但支持更多的格式化選項。
printf命令的基本語法如下。
printf format [argument...]
其中,format表示一個字符串,用于指定輸出的格式,它可以包含轉義序列,這些轉義序列用于指定輸出的格式和內容;argument表示一個或多個參數,用于提供要輸出的內容。
printf命令需要在字符串中使用占位符,然后指定要輸出的值。它可以通過在字符串中包含一些格式說明符,并按照格式說明符指定的格式將參數輸出到標準輸出。例如:
printf "%-10s %-8s %-4s\n" 姓名 性別 體重/kg printf "%-10s %-8s %-4.2f\n" 郭靖 男66.1234 printf "%-10s %-8s %-4.2f\n" 楊過 男68.6543 printf "%-10s %-8s %-4.2f\n" 郭芙 女47.9876
輸出結果如下。
姓名 性別 體重/kg 郭靖 男 66.12 楊過 男 68.65 郭芙 女 47.99
在這個示例中,%-10s表示輸出一個左對齊且寬度為10的字符串;%-8s表示輸出一個左對齊且寬度為8的字符串;%-4.2f表示輸出一個左對齊且寬度為4,小數點后保留2位的浮點數。
printf命令使用的占位符如表1-4所示。
表1-4 printf命令使用的占位符

續表

printf命令使用%c格式化字符的示例如下。
(1)輸出單個字符,例如:
printf "The first letter of the alphabet is %c\n" 'a'
輸出結果如下。
The first letter of the alphabet is a
(2)輸出字符數組中的所有字符,例如:
characters=('a' 'b' 'c') printf "The characters are: %c %c %c\n" "${characters[@]}"
輸出結果如下。
The characters are: a b c
(3)輸出字符變量的值,例如:
letter='Z' printf "The letter is: %c\n" "$letter"
輸出結果如下。
The letter is: Z
printf命令使用%s格式化字符串的示例如下。
(1)輸出單個字符串,例如:
printf "The name of this website is %s\n" "Stack Overflow"
輸出結果如下。
The name of this website is Stack Overflow
(2)輸出字符串數組中的所有字符串,例如:
names=('Alice' 'Bob' 'Eve') printf "The names are: %s %s %s\n" "${names[@]}"
輸出結果如下。
The names are: Alice Bob Eve
(3)輸出字符串變量的值,例如:
greeting='Hello, world!' printf "The greeting is: %s\n" "$greeting"
輸出結果如下。
The greeting is: Hello, world!
printf命令使用%d格式化整數的示例如下。
(1)輸出單個整數,例如:
printf "The number is: %d\n" 42
輸出結果如下。
The number is: 42
(2)輸出整數數組中的所有數字,例如:
numbers=(1 2 3) printf "The numbers are: %d %d %d\n" "${numbers[@]}"
輸出結果如下。
The numbers are: 1 2 3
(3)輸出整數變量的值,例如:
count=5 printf "The count is: %d\n" "$count"
輸出結果如下。
The count is: 5
printf命令使用%b格式化二進制整數的示例如下。
(1)輸出單個二進制整數,例如:printf "The number is: %b\n" 5
輸出結果如下。
The number is: 101
(2)輸出二進制整數數組中的所有數字,例如:
numbers=(5 6 7) printf "The numbers are: %b %b %b\n" "${numbers[@]}"
輸出結果如下。
The numbers are: 5 6 7
(3)輸出二進制整數變量的值,例如:
binary=1101 printf "The binary number is: %b\n" "$binary"
輸出結果如下。
The binary number is: 1101
printf命令使用%n輸出字符總數的示例如下。
(1)在字符串中使用%n,例如:
printf "There are %d characters in this string.%n" 8 count echo "The value of count is: $count"
輸出結果如下。
There are 14 characters in this string. The value of count is: 38
(2)在字符串數組中使用%n,例如:
strings=('This is string 1' 'This is string 2') printf "There are %d characters in string 1.%n" 100 count1 printf "There are %d characters in string 2.%n" 1000 count2 echo "The value of count1 is: $count1" echo "The value of count2 is: $count2"
輸出結果如下。
There are 100 characters in string 1. There are 1000 characters in string 2. The value of count1 is: 37 The value of count2 is: 38
printf命令使用%(datefmt)T格式化日期和時間的示例如下。
(1)輸出當前日期和時間,例如:
printf "The current date and time is: %(%Y-%m-%d %H:%M:%S)T\n"
輸出結果如下。
The current date and time is: 2023-01-08 20:41:08
(2)輸出使用自定義格式的日期和時間,例如:
printf "The date and time is: %(%a %b %d %I:%M %p %Z %Y)T\n"
輸出結果如下。
The date and time is: Sun Jan 08 08:44 PM CST 2023
3. read命令
在Shell腳本中,read命令是一個內置命令,用于從標準輸入(通常是鍵盤)中讀取一行文本并將其賦值給一個或多個變量。該命令通常用于在腳本運行時從用戶那里獲取輸入。
read命令的基本語法如下。
read [options] variable1 [variable2...]
其中,options表示可選的命令選項,可以用于指定輸入的格式;variable1表示一個變量名,將保存讀取的輸入值;variable2表示可選變量,用于將輸入的值賦給多個變量。
read命令常見命令選項如表1-5所示。
表1-5 read命令常見命令選項

使用read命令讀取變量并輸出。
[opencloud@server ~]$ cat read.sh #!/bin/bash read -p "What is your name? " name echo "Hello, $name"
執行腳本并查看輸出結果。
[opencloud@server ~]$ bash read.sh What is your name? # 在命令行中輸入tom jerry并按Enter鍵 Hello, tom jerry
在這個示例中,腳本會先輸出一行提示,詢問用戶的名字。此后,腳本將等待用戶輸入名字,并將輸入的值賦給變量name。最后,腳本將輸出一行提示。
使用read命令向用戶詢問密碼,并隱藏其輸入的內容。
[opencloud@server ~]$ read -sp "Enter your password: " password Enter your password: #輸入任何字符串,不會顯示在屏幕上
在這個示例中,腳本會先輸出一行提示,詢問用戶輸入密碼。此后,腳本將等待用戶輸入密碼,輸入的內容將被隱藏。最后,輸入的值被賦給變量password。
使用read命令,向用戶詢問數字,只讀取一個字符。
[opencloud@server ~]$ read -n 1 -p "Enter a number: " number Enter a number: 1
在這個示例中,腳本會先輸出一行提示,詢問用戶輸入數字。此后,腳本將等待用戶輸入一個字符,并將輸入的值賦給變量number。
- Linux運維實戰:CentOS7.6操作系統從入門到精通
- 嵌入式Linux驅動程序和系統開發實例精講
- 異質結原理與器件
- Linux Shell編程從入門到精通(第2版)
- VMware NSX Cookbook
- Dreamweaver CS5.5 Mobile and Web Development with HTML5,CSS3,and jQuery
- Windows Server 2012網絡操作系統項目教程(第4版)
- Linux內核設計的藝術:圖解Linux操作系統架構設計與實現原理
- INSTANT Migration from Windows Server 2008 and 2008 R2 to 2012 How-to
- 云原生落地:產品、架構與商業模式
- Fedora 12 Linux應用基礎
- Windows 7實戰從入門到精通
- CSS揭秘
- 鴻蒙HarmonyOS應用開發從入門到精通
- Gradle Effective Implementations Guide(Second Edition)