惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
G
Google Developers Blog
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
F
Full Disclosure
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
Help Net Security
Help Net Security
The Hacker News
The Hacker News
IT之家
IT之家
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
V
Visual Studio Blog
博客园 - 聂微东
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Last Week in AI
Last Week in AI
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
Google DeepMind News
Google DeepMind News
Cloudbric
Cloudbric
N
News | PayPal Newsroom
A
About on SuperTechFans
S
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
K
Kaspersky official blog
The Cloudflare Blog
I
Intezer

Bash

Linux bash 脚本监控和重启一个守护进程 - V2EX 有个脚本代码的问题 Bash Script 代码比较,你认为哪个更加容易理解? 有使用 kitty 当日常终端的老哥吗? - V2EX 在输入命令时,自动调用 fzf 进行模糊匹配 使用 shell 的 bash -c "functions"时,是不是所有相关的函数都得声明出来? shell 比较浮点数大小的问题,顺便吐槽以下 shell - V2EX bash-completor:声明式编写 Bash 补全脚本 - V2EX sudo 改用 pkexec 的问题 - V2EX sed 如何查找替换反斜杠 - V2EX lobash 发布 v0.5.0 版本 - V2EX 为什么这样写无法连接(join)数组各项? - V2EX 如何使用 awk 打印 nginx 404 日志的目录 - V2EX 请教一下,怎么把向终端输入的内容重定向一份到文件? - V2EX 请教一下 shell 里使用 jq 处理 json 应该怎么写 - V2EX 求一个 wsl2 ubuntu20.04 的默认 /etc/bash.bashrc - V2EX 如何让 bash 的补全 popup 显示 - V2EX 如何去除 Bash variable expansion 后加的单引号? - V2EX 初学者写了个 bash 脚本,求大佬点评 - V2EX tar 使用管道的困惑 - V2EX 如何获取命令标准输出到变量里并保留颜色?或者能不能一边输出一边赋值或判断。 - V2EX 请问如何在 alias 命令里传递参数? - V2EX Bash 下如何优雅地临时在后台运行程序并易于管理? - V2EX shell tr -cd 匹配字符串问题 - V2EX shell 中的 import 能不能支持 as 或 alias 类似功能 - V2EX Shell 多个文本间隔追加的方法 - V2EX 生产服务器集群被黑了,帮看看这个脚本 - V2EX 如何实现一个符合规范的 shell? - V2EX 请教下如何检测文件是否存在 - V2EX bash 里面有 io 多路复用吗? - V2EX 闲的蛋疼,用 shell 写了个拓扑排序。。。 - V2EX echo '1 2'|awk "{print $2}"的输出为什么是 1 2 - V2EX Shell 中冒号变成了斜线 - V2EX 大家觉得 shell 如何呢? - V2EX 如何让 ls 默认带--group-directories-first 选项 - V2EX shell 脚本用 expect 输入密码,密码带有{}导致 expect 报错 - V2EX 询问一个正则表达式,请大家帮忙 - V2EX 请教一个 bash 的问题 - V2EX 统计一下,如果你在用 bash,用什么版本号的 bash? - V2EX sh 可以运行后执行一段脚本吗? bash 是可以的。 - V2EX bash shell history search - V2EX Shell 给变量的赋值里带 $ 要怎么写? - V2EX [求助] shell 处理 yaml - V2EX 今天想合并文件写出个递归。。。 - V2EX 啊,我中毒了 - V2EX 寻一段 shell 来实现自动填写 ssh 登录密码 - V2EX 秀一下原生的 “终端.app”(bash) - V2EX # [求助] 用 curl 抓取一个网站,网站有很多分页,但不论怎么指 page=[2-100],抓取的都只是 page=1 的内容... - V2EX 请问这段 shell 代码有设么问题吗 - V2EX
请教 Shell 空字符串数组的问题 - V2EX
adoyle · 2019-06-17 · via Bash
yangg

1

yangg      2019 年 6 月 17 日

k=$(printf \n\n\n)
echo ${#k[@]}

adoyle

2

adoyle      2019 年 6 月 17 日

@yangg 我在 bash 4.4 和 bash 5.0 都试过了,还是 1

yangg

3

yangg      2019 年 6 月 17 日

@adoyle 那好吧,我在 zsh 试了下是 3 没想到 这个跟 bash 还有区别

adoyle

5

adoyle      2019 年 6 月 18 日

@Kobayashi 感谢回复。while + read 是个好办法,如果用 bash 4 的话,可以用 readarray 来简化写法 `readarray k < <(printf '\n\n\n')`。

> 另外,例 1 中,你的引号使用方法完全是错误的。建议了解一下 word-splitting,不要乱用引号。

恩,例 1 中的引号对于我的问题的确没有意义。不过乱用引号不会造成额外问题,加引号是最保守的,除非因为引号转义符导致什么偏差,但这是别的问题了。
> In either case, quoting anyway will not break anything. So if in doubt, quote!

看了 https://mywiki.wooledge.org/WordSplitting
感觉 Word Splitting 和 IFS 真是麻烦啊....

> 最后送楼主一句话:明知山有虎,……
诶,从接触命令行到现在一直用的是 bash,而且 bash 在大多数系统里都是默认 shell,用 bash 已经变成我的执着。

Kobayashi

6

Kobayashi      2019 年 6 月 18 日

有些地方可能让你造成了误解。

引号的问题:

k=("$(printf '\n\n\n')") 中,输出结果被引起来,这样 3 个换行被当做一个字符串,数组大小必然为 1.

k="$(printf '\n\n\n')" 中,问题同上,不过这次没加括号,返回的根本不是数组,就是 1 个字符串。

你在不该加引号的地方加入了引号,所以我才觉得你可能不大会用引号。先不提空字符串,这样做根本不可能捕捉到大小为 3 的数组。

最后一句话不是指 Bash,而是指捕捉空字符串这个解决方案本身就棘手,换个思路不用它才是最好的做法。

wooledge 维基我读的是 BashGuide 新手教程。单个知识点的话,可能会解释的更为详尽、繁琐,有时候没必要追究太深。

adoyle

7

adoyle      2019 年 6 月 18 日

@Kobayashi 感谢细心指出,引号的问题如你所说,我之后也意识到这个问题,当时脑袋糊涂了,举了这个例子。

捕捉空字符串这个需求是因为我简化了问题导致看起来没意义,实际上我是为了实现一个严格的 split 函数,返回一个数组,就可能会需要返回包含多个空字符串的数组。后来我觉得函数返回数组比较难以实现,就直接去改传入参数了。

BashGuide 还没有读过,因为我之前直接读的 Bash Reference Manual。wooledge wiki 写得非常棒,能够把这些细节深挖到这地步,非常让人佩服。

james122333

8

james122333      2019 年 7 月 5 日

#!/bin/bash

k=($(echo "\n" "\n" "\n"))
echo ${#k[@]}

for i in ${k[@]}
do
echo -en "$i"
done > log

james122333

9

james122333      2019 年 7 月 5 日

感觉 newline 当空不是个好方法
不是很正规