1.终端打印

命令echo

[root@SERVER-Test testperl]# echo "hello world"
hello world

注意:

[root@SERVER-Test ~]# echo "$HOME"
/root
[root@SERVER-Test ~]# echo "HOME"
HOME
[root@SERVER-Test ~]# echo "${HOME}"
/root
[root@SERVER-Test ~]# echo "${HOME} work"
/root work

1)在echo中使用双引号,如果希望打印!,你可以在其之前加上一个特殊的转义字符(\)将!转义。

[root@SERVER-Test testperl]# echo "hello world !"
-bash: !": event not found
[root@SERVER-Test testperl]# echo "hello world \!"
hello world \!

[root@SERVER-Test testperl]# echo "$SHELL"
/bin/bash
[root@SERVER-Test testperl]# echo "/etc/*.conf"
/etc/*.conf
[root@SERVER-Test testperl]# echo "Today is $(date)"
Today is 2014年 03月 27日 星期四 21:02:09 CST


2)不带引号时候,不能显示分号;

[root@SERVER-Test testperl]# echo hello;hello
hello
-bash: hello: commandnot found
[root@SERVER-Test testperl]# echo 'hello;hello'
hello;hello
[root@SERVER-Test testperl]# echo "hello;hello"
hello;hello

3)带单引号的echo,不会对变量(如$var)求值。意味着echo '$var'直接返回$var,而echo $var将会根据$var返回$var的值。

[root@SERVER-Test testperl]# echo '$var'
$var
[root@SERVER-Test testperl]# echo $var
[root@SERVER-Test testperl]#

[root@SERVER-Test testperl]# echo '$SHELL'
$SHELL
[root@SERVER-Test testperl]# echo '/etc/*.conf'
/etc/*.conf
[root@SERVER-Test testperl]# echo "today is $(date)"
today is 2014年 03月 27日 星期四 21:05:59 CST

4)注意“\”用法

[root@SERVER-Test testperl]# echo "Path is \$PATH"
Path is $PATH
[root@SERVER-Test testperl]# echo "Path is $PATH"
Path is /opt/svn/bin:/usr/lib64/qt-3.3/bin:/usr/local/mysql/bin:/usr/local/zabbix/bin:/usr/local/zabbix/sbin:/usr/local/svn/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/sbin:/usr/bin:/usr/sbin:/home/linqingtao/bin:/home/comake
[root@SERVER-Test testperl]#

[root@SERVER-Test testperl]# cat test.sh
#!/bin/bash
echo "A found by an old martial \
arts One day Goku meets a \
girl named retrieve the seven"
[root@SERVER-Test testperl]# sh test.sh
A found by an old martial arts One day Goku meets a girl named retrieve the seven
[root@SERVER-Test testperl]#


命令printf

[root@SERVER-Test testperl]# printf "hello world"
hello world[root@SERVER-Test testperl]#

输出差不多,不同的地方就是没有自动换行!

默认printf不会想echo自动添加换行符,我们可以手动添加 \n

[root@SERVER-Test testperl]# printf "hello world\n"
hello world

printf 使用引用文本或空格分隔的参数,外面可以在printf中使用格式化字符串,还可以制定字符串的宽度、左右对齐方式等。

我来用一个脚本来体现printf的功能

[root@SERVER-Test testperl]# cat printf.sh
#!/bin/bash
########
printf"%-10s %-8s %-4s\n"姓名 性别 体重kg
printf"%-10s %-8s %-4.2f\n"郭靖 男 66.1234
printf"%-10s %-8s %-4.2f\n"杨过 男 48.6543
printf"%-10s %-8s %-4.2f\n"郭芙 女 47.9876输出结果为虾米

输出结果为下面

[root@SERVER-Test testperl]# sh printf.sh
姓名     性别   体重kg
郭靖     男      66.12
杨过     男      48.65
郭芙     女      47.99

%s %c %d %f都是格式替代符
%-10s 指一个宽度为10个字符(-表示左对齐,没有则表示右对齐),任何字符都会被显示在10个字符宽的字符内,如果不足则自动以空格填充,超过也会将内容全部显示出来。
%-4.2f 指格式化为小数,其中.2指保留2位小数。

例如:列出最常用的10条命令

[root@SERVER-Test testperl]# cat history.sh
#!bin/bash
#列出最常用的10条历史命令
printf "%-32s %-10s\n" 命令 次数
cat ~/.bash_history | awk '{ list [$1] ++; } \
END {
for (i in list )
{
printf ("%-30s %-10s\n",i,list [i]); }
}'| sort -nrk 2 | head

看个例子:

[root@SERVER-Test ~]# var=123
[root@SERVER-Test ~]# echo "$var"
123
[root@SERVER-Test ~]# echo "${var}"
123
[root@SERVER-Test ~]# printf "${var}"
123[root@SERVER-Test ~]# printf "$var"
123[root@SERVER-Test ~]# printf "$var \n"
123
[root@SERVER-Test ~]# printf "%s\n" $var
123
[root@SERVER-Test ~]# printf "%s\n" ${var}
123
[root@SERVER-Test ~]#

补充内容

注意echo和printf中的(-e和-n)应该出现在命令行内任何字符串之前。

[root@SERVER-Test testperl]# echo "1\t2\t3"
1\t2\t3
[root@SERVER-Test testperl]# echo -e "1\t2\t3"
1   2   3
[root@SERVER-Test testperl]# echo -e "1\nt2\nt3"
1
t2
t3
[root@SERVER-Test testperl]# echo -n "1\t2\t3"
1\t2\t3[root@SERVER-Test testperl]#
[root@SERVER-Test testperl]# printf "1\t2\t3"
1   2   3[root@SERVER-Test testperl]#

echo -e "$j\c"  ,我们知道每次echo后系统会自动的换行,但加上了\c 后,不会在自动换行了,实例运行如下:

[root@SERVER-Test testperl]# echo -e "today is";date
today is
2014年 03月 27日 星期四 17:14:07 CST
[root@SERVER-Test testperl]# echo -e "today is \c";date
today is 2014年 03月 27日 星期四 17:15:37 CST

看下面例子

[root@SERVER-Test testperl]# echo -e "number of user login:";who
number of user login:
root     pts/0        2014-03-24 15:59 (10.1.1.7)
root     pts/1        2014-03-26 09:55 (172.172.172.153)
root     pts/2        2014-03-27 11:11 (172.172.172.115)
root     pts/3        2014-03-25 11:28 (10.1.1.7)
root     pts/4        2014-03-18 10:40 (172.172.172.81)
root     pts/6        2014-03-18 10:40 (172.172.172.81)
root     pts/7        2014-03-25 12:02 (10.1.1.7)
root     pts/8        2014-03-27 11:25 (172.172.172.115)
root     pts/11       2014-03-18 11:47 (172.172.172.81)
root     pts/12       2014-03-27 14:40 (172.172.172.81)
root     pts/14       2014-03-25 19:48 (172.172.172.81)
[root@SERVER-Test testperl]# echo -e "number of user login:";who | wc -l
number of user login:
11
[root@SERVER-Test testperl]# echo -e "number of user login: \c";who | wc -l
number of user login: 11

脚本实例:显示登录用户,日期

[root@SERVER-Test testperl]# cat test10.sh
#!/bin/bash
####
clear
echo "hello $USER"
echo -e "today is \c";date
echo -e "number of users login: \c";who | wc -l
cal
exit
[root@SERVER-Test testperl]# sh test10.sh
hello root
today is 2014年 03月 27日 星期四 17:25:52 CST
number of users login: 11
      三月 2014
日 一 二 三 四 五 六
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
[root@SERVER-Test testperl]#


注意“:=”用法

[root@SERVER-Test testperl]# cat test.sh
#!/bin/bash
echo ${arg:=Foo}
bank=HSBC
echo ${bank:=Citi}
echo ${bank:=Citi}
[root@SERVER-Test testperl]# sh test.sh
Foo
HSBC
HSBC
[root@SERVER-Test testperl]# cat test.sh
#!/bin/bash
echo ${arg:=Foo}
bank=HSBC
echo ${bank:=Citi}
unset bank
echo ${bank:=Citi}
[root@SERVER-Test testperl]# sh test.sh
Foo
HSBC
Citi


[root@SERVER-Test testperl]# cat test.sh
#!/bin/bash
vech=
vech=""
vech="123"
echo $vech
[root@SERVER-Test testperl]# sh test.sh
123
[root@SERVER-Test testperl]# cat test.sh
#!/bin/bash
vech=
vech=""
echo $vech
[root@SERVER-Test testperl]# sh test.sh
[root@SERVER-Test testperl]#


脚本实例:

[root@SERVER-Test testperl]# cat test.sh
#!/bin/bash
echo "******backup sgell script******"
echo
echo "**** run time: $(date) @ $(hostname)"
echo
backup="/root/testperl"
now=$(date +"%d-%m-%Y")
echo "****dumping mysql database to $backup/$now******"
sleep 3
echo
echo "**** backup write to $backup/$now/latest.tar.gz******"
[root@SERVER-Test testperl]# sh test.sh
******backup sgell script******
**** run time: 2014年 03月 27日 星期四 20:42:37 CST @ SERVER-Test
****dumping mysql database to /root/testperl/27-03-2014******
**** backup write to /root/testperl/27-03-2014/latest.tar.gz******
[root@SERVER-Test testperl]#