我必须通过 ssh 编辑 root 拥有的文件。我在文件中添加一个条目,保留前 9 行并将其余行重新排序到一个临时文件中。我知道 > 覆盖了文件中的内容(这就是我想要的),但我需要将根目录保留为文件的所有者。我怎样才能做到这一点?谢谢!
#!/bin/bash
user=""
echo "User:"
read user
ssh xxxx@xxxx "
sed -i '\$a$user' file;
(head -n 9 file ; tail -n +10 file | sort) > temp;
cat temp > file;
rm -f temp
"