shell 复制文件到另一个文件,支持换行格式

shell 复制文件到另一个文件,支持换行格式

方法一

tmpFile=/etc/config/network

# 缓存文件
cp /etc/config/network /dev/shm/network -f

# 清空原文件
echo "" > /etc/config/network

x=`wc -l $tmpFile |awk '{print $1}'`
i=1
while [ $i -le $x ]
do
  str=$(echo "`head -$i $tmpFile | tail -1`")
  # 一行一行写入
  echo $str >> /etc/config/network
  i=`expr $i + 1`
done

方法二

#!/bin/bash

# 源文件名和目标文件名
source_file="=/etc/config/network"
target_file="target.txt"

# 逐行读取源文件内容并复制到目标文件
while IFS= read -r line; do
  echo "$line" >> "$target_file"
done < "$source_file"

echo "文件复制完成。"
最后修改:2023 年 07 月 27 日 11 : 12 AM

发表评论