输入与输出
用户键盘输入
您可以使用
$ 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
您可以使用
read -t 10 -p "Enter the Internet domain name (e.g. nixcraft.com) : " domain_name
whois $domain_name
#!/bin/bash
read -s -p "Enter Password : " my_password
echo
echo "Your password - $my_password"
echo & printf
在
$ echo $varName # not advisable unless you know what the variable contains
$ echo "$varName"
$ printf "%s\n" "$varName"
使用
#!/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"
您也可以使用通配符和
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
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.
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