小编Nic*_*ine的帖子

使用mapfile和parallel创建bash数组时,为什么在函数内部使用时没有创建数组?

当我使用mapfilewithparallel在函数内创建数组时,该数组未正确创建。

为什么是这样?

数组创建不在函数中

mapfile -t arr < <(parallel -j 0 echo ::: {1..5})  

declare -p arr
declare -a arr=([0]="1" [1]="2" [2]="3" [3]="4" [4]="5")
Run Code Online (Sandbox Code Playgroud)

同样的事情,但是在函数内部

mapRay() { mapfile -t "$1" < <(parallel -j 0 "$2" ::: "$3"); }

mapRay arr echo {1..2}

declare -p arr
declare -a arr=([0]="1")
Run Code Online (Sandbox Code Playgroud)

bash

4
推荐指数
1
解决办法
424
查看次数

是否可以将 bash 数组作为参数传递给函数?

我想将数组传递给 bash 函数,但出现bad substitution错误

例子


mapfile -t ray < <(parallel -j 0 echo ::: {1..10})

declare -p ray

declare -a ray=([0]="2" [1]="1" [2]="3" [3]="4" [4]="5" [5]="6" [6]="7" [7]="8" [8]="9" [9]="10")

arrLen() {
  echo "${#$1[@]}"
 }

arrLen ray

-bash: ${#$1[@]}: bad substitution

Run Code Online (Sandbox Code Playgroud)

那么是否不可能将参数传递给 bash 数组呢?

bash array

3
推荐指数
1
解决办法
686
查看次数

SSH for 循环:传递给变量中捕获的函数的参数未扩展

我有几台服务器,其中有几个文件,其中包含我需要解析的部署日期信息,并获取 2 个月或更早的文件的本地副本。

#!/bin/bash

# split on new line not space
# don't want to quote everything
# globbing desired
IFS=$'\n'

servers=(
  blue
  red
)

parseDate() {
    grep 'deploy_date:' $file | \
    sed  ...                  | \
    # much more in here...
}

getOldFiles() {
    local #all vars
    currentDateInSecs=$(date +%s)
    twoMonthsInSecs=526000
    for server in ${servers[@]}; do
        oldfile=$(
          ssh $server "
            $(declare -f parseDate)
            for file in /dir/$server/file*; do
                dateInSecs=$(date -jf '%b/%e/%Y' $(parseDate) +%s)
                timeDiff=$((\$currentDateInSecs - \$dateInSecs))
                ((\$timeDiff >= \$twoMonthsInSecs)) &&
                    cat …
Run Code Online (Sandbox Code Playgroud)

bash ssh command-substitution variable-substitution for

3
推荐指数
1
解决办法
434
查看次数

是否有一个命令可以使另一个命令或进程更快地完成?

背景

我当时sudo renice -20 -p <pid>认为这会让我的视频转换得更快。

经过测试,它没有任何效果,然后我发现速度与漂亮无关。

问题

是否有一个命令可以通过限制其他进程来允许进程或命令更快地完成?

process time

2
推荐指数
1
解决办法
100
查看次数

当设置为 var 时,如何将切片数组的输出保存为单独的项目而不是字符串?

对数组进行切片时,输出将作为字符串捕获在变量中。

当直接使用切片时,它将是单独的项目。

如何将切片数组保存在变量中而不将其转换为字符串?

还有其他方法或途径可以解决这个问题吗?

例子:

# setting IFS to this, no need to quote anything
IFS=$'\n'

mapfile -t arr < <(for i in {1..6}; do echo $i; done)

declare -p arr
declare -a arr=([0]="1" [1]="2" [2]="3" [3]="4" [4]="5" [5]="6")

# $() or `` give the same results  
slc=`echo ${arr[@]:0:3}`  

# proving that when the slice is saved as a var, it's a string
for i in $slc; do echo $i; done
1 2 3

# proving that when the slice …
Run Code Online (Sandbox Code Playgroud)

bash array

1
推荐指数
1
解决办法
287
查看次数

过去人们如何“复制粘贴”命令?

我发现自己使用history | grep \xe2\x80\x98whatever'然后复制并粘贴cmd,我发现这很乏味。

\n

我有时也会$(history | grep 'whatever')执行 cmd if it\xe2\x80\x99s 1 行(不止于此,我必须处理它),但我必须用来sed删除行号。

\n

那么,当复制粘贴不可用时,人们是否只是重新输入命令?或者他们只是使用sedandawk来处理然后执行?

\n

linux bash

0
推荐指数
1
解决办法
148
查看次数

如何用 sed 删除一行而不留下空行?

文件.txt:

line: this-is-some-text
line2: this-is-some-other-text
Run Code Online (Sandbox Code Playgroud)

例子:

line: this-is-some-text
line2: this-is-some-other-text
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,第一行已被删除,但留下了一个空行。

你能执行sed这样它就不会在删除时留下空行吗?

sed

0
推荐指数
1
解决办法
129
查看次数

就像如何执行多个“sed”命令(由“;”分隔)一样,您可以使用“awk -F”来执行此操作吗?它适用于多个“awk”,但不适用于“awk-F”

您可以通过使用(谢谢家伙\xe2\x80\xa6)分隔cmd来多次调用sed,而无需使用多个管道,;有没有办法将其用于多个awk -Fcmd?

\n

使用sed多个管道

\n
\necho "\'text\';" | \\\nsed s"#\';##"g  | \\ \nsed s"#\'##"g   \n\ntext\n
Run Code Online (Sandbox Code Playgroud)\n

使用sedwith;作为分隔符

\n
\necho "\'text\';" | \\\nsed "            \\\n  s#\';##g;       \\\n  s#\'##g         \\\n"\n\ntext\n
Run Code Online (Sandbox Code Playgroud)\n

编辑:

\n

因此,您可以使用 加入多个awkcmd ;。但不能对多个awk -Fcmd执行此操作

\n

问题是关于串联多个 awk -F命令,这仍然没有答案。

\n

背景

\n
\n# \'/x/ gives the href of the actual videos\n# awk -F \'/x/\' \'{print$2}\xe2\x80\x99 \n# because the /x/ is unique to the video …
Run Code Online (Sandbox Code Playgroud)

awk text-processing

-2
推荐指数
3
解决办法
1382
查看次数