您真的需要将信息存储在数组中吗?如果不是,这是恕我直言的可读解决方案:
#!/bin/bash
for file in $(find . -type f); do
echo "File: $file";
echo "Date: $(grep 'pattern' <<< "$file")"
done
,
当您通过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”
'
本文链接:https://www.f2er.com/3118828.html