首页 > 系统相关 >【Shell脚本(三) -- echo及printf输出】

【Shell脚本(三) -- echo及printf输出】

时间:2022-12-12 11:07:14浏览次数:41  
标签:%- 输出 Shell -- echo sh printf test

一、Shell echo命令

Shell 的 echo 指令用于字符串的输出。命令格式:

echo string

1.显示普通字符串:

echo "It is a test"
echo It is a test

加不加引号效果一样

2.显示转义字符

如果需要显示转义字符,则需要在转义字符前加上反斜杠 \


echo "\"It is a test\""
echo \"It is a test\"

输出:

"It is a test"
"It is a test"

3.显示变量

#!/bin/bash

read name1 name2
echo "name1:$name1 name2: $name2"

read为从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量
输出:

[root@liang shell]# ./shell01.sh
liang1 liang2 #此处为输入的变量
name1:liang1 name2: liang2

4.显示换行

#!/bin/bash

echo -e "first line! \n" # -e 开启转义 \n换行
echo "It is a test"

echo -e "first line! \c" # -e 开启转义 \c不换行
echo "It is a test"

输出:


[root@liang shell]# ./shell01.sh
first line!

It is a test
first line! It is a test

默认换行。

5.显示结果定向至文件


echo "It is a test" > myfile

6.原样输出字符串,不进行转义或取变量(用单引号)

echo '$name"'
输出:


$name\"

7.显示命令执行结果


echo `date`
echo `ls`

注意: 这里使用的是反引号 `, 而不是单引号 '。


显示当前日期及ls命令执行的结果
[root@liang shell]# ./shell01.sh
Mon Oct 26 18:13:26 CST 2020
case_loop2.sh case_loop.sh def_function.sh for_loop2.sh for_loop.sh get_sys.sh homework1.sh homework.sh if_condiction.sh jisuanqi.sh one_shell.sh pass000.sh pass001.sh passwd_bk.sh shell01.sh shell02.sh sshd_config_bk sys1.txt sys.txt text0001.sh text0002.sh text0003.sh valiables.sh

二、Shell printf 命令

printf 使用引用文本或空格分隔的参数,外面可以在 printf 中使用格式化字符串,还可以制定字符串的宽度、左右对齐方式等。默认 printf 不会像 echo 自动添加换行符,我们可以手动添加 \n。

printf 命令的语法:

printf  format-string  [arguments...]

参数说明:

  • format-string: 为格式控制字符串
  • arguments: 为参数列表。

printf实例


#!/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@liang shell]# ./shell01.sh
姓名 性别 体重kg
郭靖 男 66.12
杨过 男 48.65
郭芙 女 47.99

最常用的格式指示符有两个%s用于字符串,而%d或者%i用于十进制整数,%f用于浮点格式

输出类型:
  • %ns:输出字符串。n 是数字,指代输出几个字符;
  • %ni:输出整数。n 是数字,指代输出几个数字‘’
  • %m.nf: 输出浮点数。m 和 n 是数字,指代输出的整数位数和小数位数。如 %8.2f 代表共输出 8 位数,其中 2 位是小数,6 位是整数

%-10s 指一个宽度为10个字符(-表示左对齐,没有则表示右对齐),任何字符都会被显示在10个字符宽的字符内,如果不足则自动以空格填充,超过也会将内容全部显示出来。

更多用法


#!/bin/bash

# format-string为双引号
printf "%d %s\n" 1 "abc"

# 单引号与双引号效果一样
printf '%d %s\n' 1 "abc"

# 没有引号也可以输出
printf %s abcdef

# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用
printf %s omn jkf

printf "%s\n" abc def

printf "%s %s %s\n" a b c d e f g h i j

# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替
printf "%s and %d \n"

输出内容:


[root@liang shell]# ./shell01.sh
1 abc
1 abc
abcdefomnjkfabc
def
a b c
d e f
g h i
j
and 0

输出格式:

\a: 输出警告声音;
\b:输出退格键,也就是 Backspaced 键;
\f:清除屏幕;
\n:换行;
\r:回车,也就是 Enter 键;
\t:水平输出退格键,也就是 Tab 键;
\v:垂直输出退格键,也就是 Tab 键;
实例:
新建一个文件 text0001.txt


ID Name    PHP   Linux  MySQL Average
1 Liming 82 95 86 87.66
2 Sc 74 96 87 85.66
3 Gao 99 83 93 91.66

直接输出:

[root@liang shell]#  printf '%s' $(cat text0001.txt)
IDNamePHPLinuxMySQLAverage1Liming82958687.662Sc74968785.663Gao99839391.66

输出结果十分混乱。这就是 printf 命令,如果不指定输出格式,则会把所有输出内容连在一起输出。其实文本的输出本身就是这样的,cat 等文本输出命令之所以可以按照格式漂亮地输出,那是因为 cat 命令已经设定了输出格式。
那么,为了用 printf 输出合理的格式,应该这样做:


[root@liang shell]# printf '%s\t %s\t %s\t %s\t %s\t %s\t\n' $(cat text0001.txt)
ID Name PHP Linux MySQL Average
1 Liming 82 95 86 87.66
2 Sc 74 96 87 85.66
3 Gao 99 83 93 91.66

注意:在 printf 命令的单引号中输入的任何空格都不会反映到格式输出中,只有格式输出符号才能影响 printf 命令的输出结果。

因为我们的文档有6列,所以使用 6 个"%s"代表这 6 列字符串,每个字符串之间用"\t"分隔,也可以设定字符串长度;最后还要加入"\n",使得每行输出都换行,否则这些数据还是会连成一行的。



标签:%-,输出,Shell,--,echo,sh,printf,test
From: https://blog.51cto.com/u_15874356/5929195

相关文章