【问题标题】:Problem to print the array elements using a bash script that access remote a server使用访问远程服务器的 bash 脚本打印数组元素的问题
【发布时间】:2020-03-07 22:53:06
【问题描述】:

我编写了一个 bash 脚本,它只从文件夹名称中提取日期,并将提取的内容(日期)放入一个数组中以执行其他操作。本地工作正常,当我想在服务器上执行此远程操作时出现问题。

我通过 ssh 访问服务器,从文件夹名称中提取日期的部分工作正常,主要问题是当我想用日期填充数组时。

以下是我脚本中的一些代码:

#! bin/bash

ssh -t -t user@serveradress << 'EOT'

 # go in the path where to perform the extraction of dates
cd share/Pictures_G

 # create an array, perform the extraction of dates , populate the array with dates

declare -a all_dates

all_dates=($(ls | grep -o "[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}"))
len=${all_dates[@]}
echo "$len"
EOT

所以命令 ls | grep -o "[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}" 单独工作正常,但是当我以我在上面脚本中使用的方式使用它时,会提供下一个输出:

all_dates=
len=
echo

据我了解,没有任何东西传递给数组。

【问题讨论】:

  • ls 仅列出文件而不列出日期。您还可以通过len=${#all_dates[@]} 获得数组的长度。可能您想使用find 命令提供按日期查找文件的机制。
  • 你好。 ' ls ' 命令是那里更复杂结构的一部分。我也不想使用'find',因为在描述中如何指定我想从名称中提取日期,这是与我的目的相关的信息。 @stephanmg
  • 您需要将它存储在数组中吗?
  • @stephanmg 我要去看看。谢谢!

标签: arrays bash ssh grep remote-server


【解决方案1】:

您真的需要将信息存储在数组中吗?如果没有,恕我直言,这是一个可读的解决方案:

#!/bin/bash

for file in $(find . -type f); do
  echo "File: $file";
  echo "Date: $(grep 'pattern' <<< "$file")"
done

【讨论】:

    【解决方案2】:

    当您通过here documents 传递多行字符串时,文本会受到参数扩展、命令替换等的影响。

    相反,考虑使用单引号定义要执行的命令(避免所有替换),然后通过here document 传递它。由于命令不使用单引号,所以比较简单。

    #! /bin/bash
    CMD='
    
     # go in the path where to perform the extraction of dates
    cd share/Pictures_G
    
     # create an array, perform the extraction of dates , populate the array with dates
    
    declare -a all_dates
    
    all_dates=($(ls | grep -o "[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}"))
    len=${all_dates[@]}
    echo "$len"
    '
    ssh -t -t user@serveradress <<EOT
        $CMD
    EOT
    

    等效方法,没有中间变量

    echo '
     # PUT COMMANDS HERE
     # go in the path where to perform the extraction of dates
    cd share/Pictures_G
     # MORE COMMANDS HERE
    ...
    echo "$len"
    ' | ssh -t -t user@serveradress
    
    **UPDATE 1: Parameterizing the command**
    
    If the command line has to be parametrized to using variables in the calling script, they should be placed into double quotes, instead of single quotes. For example, if TARGET_DIR reference the remote path. Note that the single quote has to be terminated, and the variable should be placed in double quotes for safety.
    

    TARGET_DIR=分享/图片_G CMD=' # 进入提取日期的路径 cd '"$TARGET_DIR"'

    # 创建一个数组,执行日期的提取,用日期填充数组

    声明 -a all_dates

    all_dates=($(ls | grep -o "[0-9]{4}-[0-9]{2}-[0-9]{2}")) len=${all_dates[@]} 回声“$ len” '

    
    

    【讨论】:

    • 感谢您的回复,我会试试这个,我会告诉你结果是什么。祝你有美好的一天!
    • 您好,我尝试了第一个示例的结构,但输出为 $CMD。
    • 请尝试不引用 EOT,根据更新的答案
    • 谢谢!该解决方案有效,如果我可以在这种情况下从命令行添加参数,我只有一个问题?!? (我也会将答案标记为有助于解决我的问题)。
    • 您应该可以从命令行添加参数。我将更新第一个代码示例
    猜你喜欢
    • 2015-04-03
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 2018-03-17
    相关资源
    最近更新 更多