用于从多台 Windows 机器复制文件的 Linux 脚本
尝试制作一个 bash 脚本时遇到问题,该脚本将从安装到 Windows 上该共享的连接的文件中读取 IP 地址和用户名,然后将任何文件类型复制到名为用户名的新文件夹中。 目前它还不太有效,如果找不到 Windows 共享,它会生成数百个名为 *.ano 的文件夹。
请帮忙
文本文件:
192.168.0.2 user1
192.168.0.3 user2
bash脚本:
USER='/home/user/user.ip'
IPADDY=$(grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $USER)
USERNAME=$(awk '{ print $NF }' $USER)
for i in $IPADDY $USERNAME
do
mkdir /home/user/Documents/$USERNAME
mount -t smbfs //$IPADDY/$USERNAME /home/user/$USERNAME
rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/*.ano
done
大家好,感谢您如此快速的回复,我已按如下方式更改了代码,但仍然收到多个文件,我在这里做错了什么吗
USER='/home/user/user.ip'
IPADDY=$(grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $USER)
USERNAME=$(awk '{ print $NF }' $USER)
while read IPADDY USERNAME; do
mkdir /home/user/Documents/$USERNAME
mount -t smbfs //$IPADDY/$USERNAME /home/user/$USERNAME
rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/
done < $USER
Having a issue trying to make a bash script that will read ip address and usernames from a file that mount a connection to that share on windows and then copy ano file types into a new folder called the users name.
At the moment it doesn't quite work, it makes hundreds of folders called *.ano if it can not find the windows share.
Help please
Text file:
192.168.0.2 user1
192.168.0.3 user2
bash script:
USER='/home/user/user.ip'
IPADDY=$(grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $USER)
USERNAME=$(awk '{ print $NF }' $USER)
for i in $IPADDY $USERNAME
do
mkdir /home/user/Documents/$USERNAME
mount -t smbfs //$IPADDY/$USERNAME /home/user/$USERNAME
rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/*.ano
done
Hi all thanks for such a quick reply, I have change the code as follow but still get multiple files have I done something wrong here
USER='/home/user/user.ip'
IPADDY=$(grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $USER)
USERNAME=$(awk '{ print $NF }' $USER)
while read IPADDY USERNAME; do
mkdir /home/user/Documents/$USERNAME
mount -t smbfs //$IPADDY/$USERNAME /home/user/$USERNAME
rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/
done < $USER
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在
for
命令中。在您的脚本中,i
迭代 $IPADDY 的内容,然后 它迭代 $USERNAME 的内容。同时,循环内的 $USERNAME 扩展为user1 user2
,导致:挂载行变为:
等等。
相反,循环遍历文件本身:
您可能需要添加
[[ -z $IPADDY ]] && continue
跳过文件中任何可能的空白行。The problem is in the
for
command. In your script,i
iterates over the contents of $IPADDY, then it iterates over the contents of $USERNAME. Meanwhile, $USERNAME inside the loop gets expanded touser1 user2
, resulting in:The mount line becomes:
And so on.
Rather, loop over the file itself:
You might want to add
[[ -z $IPADDY ]] && continue
to skip over any possible blank lines in the file.一个问题是您对目标文件使用通配符 (
*
)。但这些文件不存在 - 因此/home/user/Documents/$USERNAME/*.ano
无法匹配,rsync 将创建一个文件*.log
。最好这样做:
One problem is that you use a wildcard (
*
) for the destination files. But those files don't exist - therefore/home/user/Documents/$USERNAME/*.ano
cannot match and rsync will create a file*.log
.Better do: