使用zsh,根据(可能为空)文件名 glob 的结果创建一个可能为空的数组并不困难。例如:
% pathlist=( /no/such/path/*(N) ); printf -- "%d\n" $#pathlist
0
Run Code Online (Sandbox Code Playgroud)
用bash,然而,最接近的近似我可以想出上述代码失败时的水珠为空,以产生一个空数组:
% pathlist=( /no/such/path/* ); printf -- "%d\n" ${#pathlist[@]} )
1
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该pathlist变量包含一个条目,即字符串“/no/such/path/*”。
如何修改bash代码,以便pathlist为与 glob 匹配的每个文件(如果有)只包含一个条目?
我有两个单独的列表。一个包含 1000 个主机名,另一个包含该主机的 1000 个关联 IP。我需要生成配置条目并推送到我的配置文件以添加我们监控环境下的所有主机。
#!/bin/bash
Enter the hostnames
readarray hosts
Enter the IP's
readarray IP
Run Code Online (Sandbox Code Playgroud)
我应该如何在如下循环中一一迭代?我知道如何通过 using 迭代单个数组for i in "${hosts[@]}",但是在两个单独列表的情况下,如何实现迭代?
echo -e "\nObject host \""$hosts"\"{"
import = \""generic-service"\"
address = \""$IP"\"
group = \""Mom1-Ping"\"
"\n}" >> /etc/icinga2/zones.d/mom2/AP.conf
Run Code Online (Sandbox Code Playgroud)
第一个列表示例(列表 1):
sjc02-mfg-api01.example.com
sjc02-mfg-api02.example.com
sjc02-mfg-api03.example.com
Run Code Online (Sandbox Code Playgroud)
多达 1000 台主机
第二个列表示例(列表 2):
10.20.2.22
10.20.2.23
10.20.2.24
Run Code Online (Sandbox Code Playgroud)
多达 1000 个 IP
预期输出:
Object host "sjc02-mfg-api01.example.com" {
import = "generic-service"
address = "10.20.2.22"
group = "Mom01-Ping"
}
Object host "sjc02-mfg-api02.example.com" {
import = …Run Code Online (Sandbox Code Playgroud) 在 bashread内置命令中-d,我们必须指定换行符以外的行分隔符
是否readarray提供了一些指定行分隔符的方法?
IFS字段分隔符?谢谢。
看到steeldriver的评论后,
$ bash --version
GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ help readarray
readarray: readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array] …Run Code Online (Sandbox Code Playgroud) 我正在写一个函数,这将使一个REST API调用这可能是要么GET,PUT,DELETE,POST,等。
我想将此方法作为参数提供给函数,并将其添加到该单个函数调用的选项数组中。这可能吗?
目前我正在通过创建一个单独的local数组来解决这个问题,但更喜欢只使用单个options数组。
#!/bin/bash
options=(
--user me:some-token
-H "Accept: application/json"
)
some_func () {
local urn=$1
shift
local func_opts=("${options[@]}" "$@")
printf '%s\n' "${func_opts[@]}"
}
# This should return all options including -X GET
some_func /test -X GET
# This should return only the original options
printf '%s\n' "${options[@]}"
Run Code Online (Sandbox Code Playgroud)
我也可以使用一个临时数组来存储 的内容options,添加新选项,然后在函数结束之前重置它,但我认为这也不是一个特别干净的方法。
给这个file.txt:
first line
second line
third line
Run Code Online (Sandbox Code Playgroud)
这适用于bash:
while IFS=' ' read -a args; do
echo "${args[0]}"
done < file.txt
Run Code Online (Sandbox Code Playgroud)
生产
first
second
third
Run Code Online (Sandbox Code Playgroud)
也就是说,我们能够逐行读取文件,并且在每一行上我们使用空格作为分隔符将行进一步拆分为一个数组。但是在 中zsh,结果是一个错误:read: bad option: -a。
我们怎样才能做到与 inzsh一样bash呢?我已经尝试了几种解决方案,但我永远无法使用空格作为分隔符将字符串拆分为数组。
我有一个包含如下数据的文件:
UserName
UserName
UserName
Run Code Online (Sandbox Code Playgroud)
每条数据由一个新行分隔。
我需要将该数据转换为字符串数组并存储在变量中。
如何仅通过 bash 或 shell 执行来实现这一点?
我有一个名为的字符串数组,names其中包含带有一些后续垃圾数据的名称。像这样
Jill Shortz, City Contractor, America
Bill Torts, Family Doctor, Canada
Will Courtz, Folk DJ, Bulgaria
Phil-Lip Warts, Juggler, India
Run Code Online (Sandbox Code Playgroud)
我想通过names使用正则表达式仅提取前两个单词进行迭代(^\w+-*( *\w+)*)并将它们覆盖回,names以便它将包含
Jill Shortz
Bill Torts
Will Courtz
Phil-Lip Warts
Run Code Online (Sandbox Code Playgroud)
这是我尝试的方式,但我的 AIX 机器不喜欢-P在 Perl 模式下执行的参数
for((i=0;i<${#names[@]};++i)); do
names[$i]=`grep -P '(^\w+-*( *\w+)*)' -o <<<"${names[i]}"`
done
Run Code Online (Sandbox Code Playgroud) 我正在制作一个gc可以区分 git commit/checkout的智能别名。
如果gc在不带任何参数或带有-a,-m参数的情况下调用,则运行 git commit。否则,运行 git checkout(如果有一个b带有附加参数的标志)。如果调用 gc 的任何其他变体,我宁愿它抛出错误而不是做一些意想不到的事情。
到目前为止,这是我的 shell 函数。
gc() {
args=$@
commit=false
checkout=false
# Check flags
while getopts ":am:b" opt; do
case $opt in
a|m)
commit=true
;;
b)
checkout=true
;;
\?)
echo "Unknown flags passed."
return 1
;;
esac
done
shift "$((OPTIND-1))"
# Check number of arguments
if [[ "$#" == 0 ]]; then
commit=true
elif [[ "$#" == 1 ]]; then
checkout=true
else
echo …Run Code Online (Sandbox Code Playgroud) (*)和 和有("$(ls)")什么区别?
除了分隔符不同之外,它们基本上相同吗?
我们如何使用任何 Linux 核心实用程序(脚本)对 du 的输出数值进行排序,例如:
136K foo.bar/feed
140K foo.bar/buy-electronic-components
32K foo.bar/cdn-cgi
88K foo.bar/what-is-ground
344K foo.bar/ldr-circuit-diagram
64K foo.bar/what-is-an-led
100K foo.bar/types-of-resistors
516K foo.bar/wp-includes
60K foo.bar/author
56K foo.bar/diy-pcb
112K foo.bar/how-to-learn-electronics
376K foo.bar/category
76K foo.bar/electronic-schematics
84K foo.bar/how-to-bar
88K foo.bar/bar-tools
20K foo.bar/comments
88K foo.bar/right-bar-temperature
48K foo.bar/contact
44K foo.bar/products
80K foo.bar/types-of-bar
3M foo.bar/
3M total
Run Code Online (Sandbox Code Playgroud)
如果以降序方式为:
3M foo.bar/
3M total
516K foo.bar/wp-includes
376K foo.bar/category
344K foo.bar/ldr-circuit-diagram
140K foo.bar/buy-electronic-components
...
Run Code Online (Sandbox Code Playgroud)
非常感谢