shell脚本中获取远程文件时出错

发布于 2024-11-26 19:36:36 字数 1243 浏览 0 评论 0原文

我有这个脚本正在 4 台服务器上执行 scp 并在调用服务器上获取文件,但是当我运行它时,出现以下错误:

syntax error near unexpected token `then'
line 16: `if[[ $line = $texts ]] ; then 

代码如下:

#!/bin/sh
servers=("[email protected]" "[email protected]" "[email protected]" "[email protected]")
tlen=${#servers[@]}
count=o
total=0
texts="Untar done you can upload new .tar file"
for(( i=0; i<${tlen}; i++ ));
    do
    echo "in"
    scp ${servers[$i]}:/usr/site/html/test/test.txt /mnt/backups/updateimage
    cat test.txt ; while read line
    do
    echo "in"
    let count++
    echo "$line"
    if[[ $line = $texts ]] ; then 
    echo "true" 
    let total++ 
    else echo "false" 
    fi
   done < test.txt
done
exit 0

I have this script that is doing a scp onto 4 servers and getting the file on the calling server , but when I run it I get the following error:

syntax error near unexpected token `then'
line 16: `if[[ $line = $texts ]] ; then 

The code is as follows:

#!/bin/sh
servers=("[email protected]" "[email protected]" "[email protected]" "[email protected]")
tlen=${#servers[@]}
count=o
total=0
texts="Untar done you can upload new .tar file"
for(( i=0; i<${tlen}; i++ ));
    do
    echo "in"
    scp ${servers[$i]}:/usr/site/html/test/test.txt /mnt/backups/updateimage
    cat test.txt ; while read line
    do
    echo "in"
    let count++
    echo "$line"
    if[[ $line = $texts ]] ; then 
    echo "true" 
    let total++ 
    else echo "false" 
    fi
   done < test.txt
done
exit 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

帅冕 2024-12-03 19:36:36

if[[

编辑

之间添加一个空格我刚刚运行了此测试,它的正确计算结果为 false

#!/bin/bash

set -eu

texts="Untar done you can upload new .tar file"
line="foo"

if [[ $line = $texts ]] ; then
  echo "true"
else
  echo "false"
fi

我怀疑您的问题出在其他地方

Put a space between if and [[

EDIT

I've just ran this test and it properly evaluates to false

#!/bin/bash

set -eu

texts="Untar done you can upload new .tar file"
line="foo"

if [[ $line = $texts ]] ; then
  echo "true"
else
  echo "false"
fi

I suspect your problem is elsewhere

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