使用shell脚本将文件从另一个用户复制到我的主目录

时间:2015-10-23 14:05:05

标签: shell

我在一个input.txt中有绝对路径的文件名

#!/bin/bash
index=0
while read line ; do
myarray[$index]="$line"
index=$(($index+1))
done < input.txt
su - anotheruser <<EOF
for e in "${myarray[@]}"
do
 cp $e /home/myhomedirectory 
done
EOF

错误: 标准必须是tty

我们如何实现上述情况?

1 个答案:

答案 0 :(得分:1)

一个不错的xargs one liner:

su -c "xargs -IFILE cp 'FILE' /home/myhomedirectory/ < input.txt" anotheruser

使用echo ...进行测试

$ cat input.txt
x/y/z
a/b/c
$ su -c "xargs -IFILE echo cp 'FILE' /home/myhomedirectory/ < input.txt" $user
Password: 
cp x/y/z /home/myhomedirectory/
cp a/b/c /home/myhomedirectory/
$

注意使用&#34; su -c命令 - user&#34;表单(使用&#34; - &#34;在用户提供登录环境之前)需要input.txt文件的完全限定路径。

相关问题