四时宝库

程序员的知识宝库

Linux,shell,echo输出、printf打印,输入/读取文件,代码案例

Shell打印

案例:echo、printf命令

#!/bin/bash

# echo字符串输出
echo "默认换行输出"
echo -e "解析:\n特殊符号"
echo -n "不换行输出"

# printf格式化输出命令
printf "换行输出\n"

# 格式化输出(5表示宽度,-表示做开头)
printf "%5d-----%5s-----%5.3f\n" 10 "AAAAA" 10.11
printf "%5d-----%5s-----%5.3f\n" 10 "BBBBB" 10.11
printf "%-5d-----%-5s-----%-5.3f\n" 10 "CCCCC" 10.11
printf "%-5d-----%-5s-----%-5.3f\n" 10 "DDDDD" 10.11

输入文件

案例:

#!/bin/bash

# 覆盖输入到文件
echo "这里是内容" > file1
# 追加输出到文件
echo -e "这里是内容\n这里是内容" >> file1

# 覆盖输入文件
cat > file2 <<-EOF
我是文件内容
我是文件内容
EOF

# 追加输入文件
cat >> file2 <<-EOF
我是追加内容
我是追加内
EOF

# 覆盖输入文件,既输入到文件,控制台也打印
tee > file3 <<-EOF
tee文件内容
tee文件内容
EOF

# 追加输入文件,既输入到文件,控制台也打印
tee >> file3 <<-EOF
tee追加内容
tee追加内容
EOF

读取文件

案例:

#!/bin/bash

# 读取文件
cat file1 | while read line1
do
  echo "$line1"
done

# 读取文件
while read line2
do
  echo "$line2"
done<file2

# 读取文件
lines=`cat file3`
for line3 in $lines
do
  echo $line3
done

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接