输入与输出

用户键盘输入

您可以使用 read 命令接受键盘输入,并将输入值分配给用户定义的 shell 变量。

$ read -p "Prompt" variable1 variable2 variableN

简单的示例如下:

#!/bin/bash
read -p "Enter your name : " name
echo "Hi, $name. Let us be friends!"

Enter your name : Vivek Gite
Hi, Vivek Gite. Let us be friends!

# read three numbers and assigned them to 3 vars
read -p "Enter number one : " n1
read -p "Enter number two : " n2
read -p "Enter number three : " n3

# display back 3 numbers - punched by user.
echo "Number1 - $n1"
echo "Number2 - $n2"
echo "Number3 - $n3"

# A shell script to display the Internet domain name owner information (domain.sh):
read -p "Enter the Internet domain name (e.g. nixcraft.com) : " domain_name
whois $domain_name

您可以使用 -t 选项使读取命令超时。如果未在 TIMEOUT 秒内读取完整的输入行,则会导致读取超时并返回失败。例如,如果在 10 秒内未提供任何输入,则程序将中止:

read -t 10 -p "Enter the Internet domain name (e.g. nixcraft.com) : " domain_name
whois $domain_name

-s 选项导致来自终端的输入不会显示在屏幕上。这对于密码处理很有用:

#!/bin/bash
read -s -p "Enter Password  : " my_password
echo
echo "Your password - $my_password"

echo & printf

在 Shell 中,echo 与 printf 都可以用来输出内容:

$ echo $varName  # not advisable unless you know what the variable contains
$ echo "$varName"
$ printf "%s\n" "$varName"

使用 echo 命令显示一行文本或一个变量值。它不提供格式化选项。当您知道变量的内容不会引起问题时,这是显示简单输出的好命令。对于大多数用途,首选 printf。

#!/bin/bash
# Display welcome message, computer name and date
echo "* Backup Shell Script *"
echo
echo "* Run time: $(date) @ $(hostname)"
echo

# Define variables
BACKUP="/nas05"
NOW=$(date +"%d-%m-%Y")

# Let us start backup
echo "* Dumping MySQL Database to $BACKUP/$NOW..."

# Just sleep for 3 secs
sleep 3

# And we are done...
echo
echo "* Backup wrote to $BACKUP/$NOW/latest.tar.gz"

您也可以使用通配符和 echo 命令打印文件名:

cd /etc
echo *.conf

aatv.conf adduser.conf apg.conf argus.conf atool.conf brltty.conf ca-certificates.conf
chkrootkit.conf cowpoke.conf cvs-cron.conf cvs-pserver.conf dconf.conf dconf-custom.conf
debconf.conf deluser.conf
....
...
..
wodim.conf wpa_supplicant.conf wvdial.conf xorg.conf

printf 命令格式并在屏幕上显示数据。但是,printf 不提供新行。您需要使用%指令提供格式字符串,并使用转义符以与 C printf() 函数最相似的方式来格式化数字和字符串参数。使用 printf 生成格式化的输出。

FORMAT controls the output as in C printf.  Interpreted sequences are:

       \"     double quote

       \NNN   character with octal value NNN (1 to 3 digits)

       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     produce no further output

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab

       \xHH   byte with hexadecimal value HH (1 to 2 digits)

       \uHHHH Unicode (ISO/IEC 10646) character with hex value HHHH (4 digits)

       \UHHHHHHHH
              Unicode character with hex value HHHHHHHH (8 digits)

       %%     a single %

       %b     ARGUMENT as a string with '\' escapes interpreted, except that octal escapes are of the form
              \0 or \0NNN and all C format specifications ending with one of diouxXfeEgGcs,
              with ARGUMENTs converted to proper type first. Variable widths are handled.

printf 更详细的示例如下:

vech="Car"
printf "%s\n" $vech
printf "%1s\n" $vech
printf "%1.1s\n" $vech
printf "%1.2s\n" $vech
printf "%1.3s\n" $vech
printf "%10.3s\n" $vech
printf "%10.1s\n" $vech
no=10
printf "%d\n" $no
big=5355765
printf "%e\n" $big
printf "%5.2e\n" $big
sales=54245.22
printf "%f\n" $sales
printf "%.2f\n" $sales
上一页
下一页