用于从多台 Windows 机器复制文件的 Linux 脚本

发布于 2024-11-02 12:26:21 字数 1072 浏览 1 评论 0原文

尝试制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ι不睡觉的鱼゛ 2024-11-09 12:26:21

问题出在 for 命令中。在您的脚本中,i 迭代 $IPADDY 的内容,然后 它迭代 $USERNAME 的内容。同时,循环内的 $USERNAME 扩展为 user1 user2,导致:

mkdir /home/user/Documents/user1 user2

挂载行变为:

mount -t smbfs //192.168.0.2 192.168.0.3/user1 user2 /home/user/user1 user2

等等。

相反,循环遍历文件本身:

while read IPADDY USERNAME; do
  #awesome things here based on $IPADDY and $USERNAME
done < $USER

您可能需要添加 [[ -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 to user1 user2, resulting in:

mkdir /home/user/Documents/user1 user2

The mount line becomes:

mount -t smbfs //192.168.0.2 192.168.0.3/user1 user2 /home/user/user1 user2

And so on.

Rather, loop over the file itself:

while read IPADDY USERNAME; do
  #awesome things here based on $IPADDY and $USERNAME
done < $USER

You might want to add [[ -z $IPADDY ]] && continue to skip over any possible blank lines in the file.

忆沫 2024-11-09 12:26:21

一个问题是您对目标文件使用通配符 (*)。但这些文件不存在 - 因此 /home/user/Documents/$USERNAME/*.ano 无法匹配,rsync 将创建一个文件 *.log

最好这样做:

rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/

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:

rsync -va /home/user/$USERNAME/*.ano /home/user/Documents/$USERNAME/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文