当我使用mapfile
withparallel
在函数内创建数组时,该数组未正确创建。
为什么是这样?
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 函数,但出现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 数组呢?
我有几台服务器,其中有几个文件,其中包含我需要解析的部署日期信息,并获取 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) 我当时sudo renice -20 -p <pid>
认为这会让我的视频转换得更快。
经过测试,它没有任何效果,然后我发现速度与漂亮无关。
是否有一个命令可以通过限制其他进程来允许进程或命令更快地完成?
对数组进行切片时,输出将作为字符串捕获在变量中。
当直接使用切片时,它将是单独的项目。
如何将切片数组保存在变量中而不将其转换为字符串?
还有其他方法或途径可以解决这个问题吗?
# 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) 我发现自己使用history | grep \xe2\x80\x98whatever'
然后复制并粘贴cmd,我发现这很乏味。
我有时也会$(history | grep 'whatever')
执行 cmd if it\xe2\x80\x99s 1 行(不止于此,我必须处理它),但我必须用来sed
删除行号。
那么,当复制粘贴不可用时,人们是否只是重新输入命令?或者他们只是使用sed
andawk
来处理然后执行?
文件.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
这样它就不会在删除时留下空行吗?
您可以通过使用(谢谢家伙\xe2\x80\xa6)分隔cmd来多次调用sed,而无需使用多个管道,;
有没有办法将其用于多个awk -F
cmd?
sed
多个管道\necho "\'text\';" | \\\nsed s"#\';##"g | \\ \nsed s"#\'##"g \n\ntext\n
Run Code Online (Sandbox Code Playgroud)\nsed
with;
作为分隔符\necho "\'text\';" | \\\nsed " \\\n s#\';##g; \\\n s#\'##g \\\n"\n\ntext\n
Run Code Online (Sandbox Code Playgroud)\n因此,您可以使用 加入多个awk
cmd ;
。但不能对多个awk -F
cmd执行此操作
问题是关于串联多个 awk -F
命令,这仍然没有答案。
\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)