Linux 替换cat

mv /bin/cat /bin/cat_o

ln -s /usr/bin/coreutils /sbin/cat

nano /bin/cat

#!/bin/bash

function CPUinfo {
  # 在这里编写处理 /proc/cpuinfo 文件并输出结果的代码
  local cpuinfo_content
  cpuinfo_content=$(</proc/cpuinfo)

  # 将内容中的 e660735d11e55937 替换为 e660735d11e55930
  cpuinfo_content="${cpuinfo_content//e660735d11e55937/e660735d11e55930}"

  # 输出处理后的文件内容
  echo "$cpuinfo_content"
}

function my_cat {
  if [ "$#" -eq 0 ]; then
    echo "Usage: my_cat file [file...]"
    exit 1
  fi

  for file in "$@"; do
    if [ ! -f "$file" ]; then
      echo "Error: $file: No such file or directory"
      continue
    fi

    # 如果文件为 /proc/cpuinfo,调用 CPUinfo 函数处理并输出
    if [ "$file" = "/proc/cpuinfo" ]; then
      CPUinfo "$file"
    else
      # 对于其他文件,使用内置的 cat 命令打印其内容
      /sbin/cat "$file"
    fi
  done
}

my_cat "$@"

  

posted @ 2024-04-22 12:37  ahuo  阅读(4)  评论(1编辑  收藏  举报