重定向

  • ">" 符号:把 ls 的标准“1”输出,重定向到 lsoutput.txt 文件
ls -1 > lsoutput.txt
  • ">>" 符号,把 ls 的标准“1”输出,添加到 lsoutput.txt 文件的尾部
ls -1 >> lsoutput.txt
  • 文件标识符: 0 - 标准输入; 1 - 标准输出; 2 - 标准错误输出

重定向会先执行

cat mydata.txt | sort | uniq > mydata.txt
  • 先执行了 > mydata.txt ,会立即清空文件(将文件截断为 0 字节)

编写shell脚本

#!/bin/sh
# first
# This file looks through all the files in the current
# directory for the string POSIX, and then prints the names of
# those files to the standard output.
  for file in *
  do
    if grep -g POSIX $file
    then
    echo $file
  done
exit 0

脚本执行

  • 设置为可执行
chmod +x first
  • 运行
./first   # 最安全的执行方法
posted on 2026-01-27 10:48  中年二班  阅读(2)  评论(0)    收藏  举报