AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1423902
Accepted
abbood
abbood
Asked: 2019-04-11 09:58:54 +0800 CST2019-04-11 09:58:54 +0800 CST 2019-04-11 09:58:54 +0800 CST

如何在shell命令中的脚本中执行脚本

  • 772

我试图让这个脚本工作(它应该做的是在远程服务器的最后一个日志文件中搜索一个字符串):

ssh -i $ssh_key_file ubuntu@$1 -t "cd /path/to/logs; grep \"POST api/orders \" `ls -Art | grep info | tail -n 1` -A 2 | grep \"'store_id' => '$2'\" -B 1; bash --login"

这个脚本

`ls -Art | grep info | tail -n 1`

应该返回给我最后一个日志文件,这是一个例子,如果我在远程服务器上单独运行该脚本:

$ ls -Art | grep info | tail -n 1
info-2019-04-10.log

此外,如果我通过硬编码日志文件名来运行与上面完全相同的脚本,它就可以正常工作:

 ssh -i $ssh_key_file ubuntu@$1 -t "cd /path/to/logs; grep \"POST api/orders \" info-2019-04-10.log -A 2 | grep \"'store_id' => '$2'\" -B 1; bash --login"

我能够将这个(查找最后一个日志文件)脚本嵌入到其他 shell 脚本中,并且运行良好,如下所示:

ssh -i $ssh_key_file ubuntu@$1 -t "cd /path/to/logs; grep $2 `ls -Art | grep info | tail -n 1`; bash --login"

我在原始脚本中做错了什么?

更新

使用这个脚本

function totCreateOrder()
{
ssh -i "$ssh_key_file" ubuntu@"$1" -t<<EOF
cd /var/www/toters/storage/logs && grep "POST api/orders " "\$(ls -Art | grep info | tail -n 1)" -A 2 \
| grep "'store_id' => '"'$2'"'" -B 1
bash --login
EOF
}

正在返回这个奇怪的错误消息

totCreateOrder $prod1as 1006
Pseudo-terminal will not be allocated because stdin is not a terminal.
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-1067-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

74 packages can be updated.
0 updates are security updates.

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


*** System restart required ***

我查看了该错误的含义并改为执行此操作

function totCreateOrder()
{
ssh -i $ssh_key_file ubuntu@$1 -t -t<<EOF
cd /var/www/toters/storage/logs && grep "POST api/orders " "\$(ls -Art | grep info | tail -n 1)" -A 2 \
| grep "'store_id' => '"'$2'"'" -B 1
EOF
}

但后来我收到此错误消息:

totCreateOrder $prod1as 1006
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-1067-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

74 packages can be updated.
0 updates are security updates.

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


*** System restart required ***
Last login: Thu Apr 11 03:38:32 2019 from 185.81.141.32
 1"$(ls -Art | grep info | tail -n 1)" -A 2 | grep "'store_id' => '"'1006'"'" -B

更新二

我正在逐行检查脚本。这在实现以下目标的意义上起作用:

  • 登录到远程服务器
  • 能够ls -Art | grep info | tail -n 1正确解析
  • 在远程服务器上运行此命令:grep "POST api/orders " "$(ls -Art | grep info | tail -n 1)" -A 2

    函数测试(){ ssh -i $ssh_key_file ubuntu@$1 -t -t<

它返回了这个

test $prod1as
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-1067-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

74 packages can be updated.
0 updates are security updates.

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


*** System restart required ***
Last login: Thu Apr 11 05:18:10 2019 from 185.81.141.85
ubuntu@ip-x-xx-xx:~$ cd /var/www/toters/storage/logs
-Art | grep info | tail -n 1)" -A 2 storage/logs$ grep "POST api/orders " "$(ls
[2019-04-11 04:10:39] production.INFO: POST api/orders [] []
[2019-04-11 04:10:39] production.INFO: array (   'store_id' => '831',   'items' =>    array (     0 =>      array (       'additional_info' => '',       'addons' =>        array (       ),       'quantity' => 1,       'id' => 129369,
),     1 =>      array (       'additional_info' => '',       'addons' =>        array (       ),       'quantity' => 1,       'id' => 133351,     ),     2 =>      array (       'additional_info' => '',       'addons' =>        ..

然而,当我像这样添加管道时,事情就坏了:

function test()
{
ssh -i $ssh_key_file ubuntu@$1 -t -t<<EOF
cd /var/www/toters/storage/logs 
grep "POST api/orders " "\$(ls -Art | grep info | tail -n 1)" -A 2 | grep "'store_id' => '"'$2'"'" -B 1
EOF
}

它返回这个

 $ test $prod1as 1006
Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-1067-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

74 packages can be updated.
0 updates are security updates.

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


*** System restart required ***
Last login: Thu Apr 11 05:33:22 2019 from 185.81.141.85
ubuntu@ip-10-0-1-39:~$ cd /var/www/toters/storage/logs
-Art | grep info | tail -n 1)" -A 2 | grep "'store_id' => '"'1006'"'" -B 1"$(ls
linux script shell
  • 1 1 个回答
  • 92 Views

1 个回答

  • Voted
  1. Best Answer
    Kamil Maciorowski
    2019-04-11T12:28:47+08:002019-04-11T12:28:47+08:00

    我在原始脚本中做错了什么?

    主要问题是:双引号中的反引号在本地扩展。通常我会推荐 here document,但在你的情况下你需要一个伪终端并且不能真正重定向标准输入(这是 here document 所做的)。无论如何使用$()而不是反引号并转义$(或者你可以坚持使用反引号并转义它们,但$()通常是首选)或单引号这个片段。这应该将整个$( … )语法传递给远程端。您还应该确保远程 shell 将其用双引号引起来,因为很明显您不希望结果在这种情况下进行分词和模式匹配。

    一般来说,不应该解析ls. 我想很难用更健壮的东西替换你的代码,除非find在远程端支持-printf. 但是不知道是不是这样,所以我不打算改进这部分。(除了这个:为什么要反转输出ls并获取最后一个条目?为什么不是来自非反转输出的第一个条目?你grep支持-m 1吗?)

    grepcd如果失败,不应调用。

    你应该双引号变量,除非你知道你不需要。:) 不引用$ssh_key_file或$1可能会打开一罐……惊喜。$2在您的代码中有些不同。从上下文中我看到它应该在本地扩展并将结果传递给远程 shell。到达远程 shell 的任何内容都将被解析。我猜你不希望本地扩展的结果$2在远程端进行变量扩展(或者你呢?)。$2在您的原始脚本中,远程 shell 会在双引号内的单引号中看到局部扩展,外部的很重要。所以你应该在本地扩展$2在单引号中出现在远程 shell 中。但是你有文字单引号嵌入这个字符串作为grep模式的一部分,它们应该是双引号。这导致引用狂潮,这很棘手。

    您传递给远程 shell 的仍然是一个将被解析的字符串, (local) 的某些值$2将破坏代码。代码注入是可能的。这是一个常见问题,其中取决于变量的命令作为要解析的字符串传递,很难对此进行清理。我希望你能完全控制本地$2. 如果不是,那就是一个漏洞:控制它的任何人都能够以ubuntu远程主机上的用户身份运行任意命令。例如,如果您在$2扩展到"& rm -rf ~/ & "(双引号属于此处的变量)时运行您的原始代码,您会感到不愉快(所以不要这样做)。

    以下代码段完全未经测试。您的代码看起来非常具体,因此很难对其进行测试。

    ssh -i "$ssh_key_file" ubuntu@"$1" -t "
    cd /path/to/logs && grep \"POST api/orders \" \"\$(ls -Art | grep info | tail -n 1)\" -A 2 \
    | grep \"'store_id' => '\"'$2'\"'\" -B 1
    bash --login
    "
    

    笔记:

    • 由于$2需要到达远程端的本地扩展和单引号,我决定对整个命令进行双引号。将它的某些部分放在单引号中可能会减少引用(和转义)的狂热,但它不一定会使命令不那么神秘,因为很难发现以哪种方式引用了哪个部分。
    • $2仍然使代码注入成为可能。

    验收后说明

    OP 报告以下代码有效:

    function test()
    {
    ssh -i "$ssh_key_file" ubuntu@"$1" -t "cd /path/to/logs; grep \"POST api/orders \" \"\$(ls -Art | grep info | tail -n 1)\" -A 2 \
        | grep \"'store_id' => '\"'$2'\"'\" -B 1 \
        ; bash --login"
    }
    

    此代码以与我上面的代码相同的方式解决了主要问题。就语法(?)而言,变化可能很重要,但我认为它们相对于主要问题而言是次要的。

    • 2

相关问题

  • 对于 cp 或 mv,是否有等同于 cd - 的东西?

  • 以 root 身份运行 docker 容器

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 如何在CentOS 7 中将Ctrl+C 永久更改为Ctrl+K?

  • 资源管理器侧面板中的桌面外壳快捷方式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    Windows 10 服务称为 AarSvc_70f961。它是什么,我该如何禁用它?

    • 2 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Marko Smith

    ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1056)

    • 4 个回答
  • Marko Smith

    我如何知道 Windows 安装在哪个驱动器上?

    • 6 个回答
  • Martin Hope
    Albin 支持结束后如何激活 WindowsXP? 2019-11-18 03:50:17 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    Kagaratsch Windows 10 删除大量小文件的速度非常慢。有什么办法可以加快速度吗? 2019-09-23 06:05:43 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    Inter Sys Ctrl+C 和 Ctrl+V 是如何工作的? 2019-05-15 02:51:21 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve