bash和shell_Bash和Shell扩展:懒惰的列表制作

bash和shell

It’s that time of year again! When stores start putting up colourful sparkly lit-up plastic bits, we all begin to feel a little festive, and by festive I mean let’s go shopping. Specifically, holiday gift shopping! (Gifts for yourself are still gifts, technically.)

又到了每年的这个时候! 当商店开始放上五颜六色的闪亮的塑料碎片时,我们所有人都开始感到有点节日,而节日意味着我们去购物。 特别是节日礼物购物! (从技术上来说,送给自己的礼物仍然是礼物。)

Just so this doesn’t all go completely madcap, you ought to make some gift lists. Bash can help.

只是为了避免一切都变得疯狂,您应该列出一些礼物清单。 Bash可以提供帮助。

支撑扩展 (Brace expansion)

These are not braces: ()

这些不是大括号:( ()

Neither are these: []

这些都不是: []

These are braces: {}

这些是花括号: {}

Braces  tell Bash to do something with the arbitrary string or strings it finds  between them. Multiple strings are comma-separated: {a,b,c}. You can also add an optional preamble and postscript to be attached to each expanded result. Mostly, this can save some typing, such as with common file paths and extensions.

花括号告诉Bash使用它们之间发现的任意字符串进行处理。 多个字符串以逗号分隔: {a,b,c} 。 您还可以添加可选的序言和后记,以附加到每个扩展结果中。 通常,这可以节省一些键入内容,例如使用公共文件路径和扩展名。

Let’s make some lists for each person we want to give stuff to. The following commands are equivalent:

让我们为每个想要提供东西的人列出一些清单。 以下命令是等效的:

touch /home/me/gift-lists/Amy.txt /home/me/gift-lists/Bryan.txt /home/me/gift-lists/Charlie.txt
touch /home/me/gift-lists/{Amy,Bryan,Charlie}.txt
tree gift-lists

/home/me/gift-lists
├── Amy.txt
├── Bryan.txt
└── Charlie.txt

Oh darn, “Bryan” spells his name with an “i.” I can fix that.

哦,该死,“布莱恩”用“ i”拼写他的名字。 我可以解决。

mv /home/me/gift-lists/{Bryan,Brian}.txt

renamed '/home/me/gift-lists/Bryan.txt' -> '/home/me/gift-lists/Brian.txt'

Shell参数扩展 (Shell parameter expansions)

Shell parameter expansion allows us to make all sorts of changes to parameters enclosed in braces, like manipulate and substitute text.

Shell参数扩展使我们能够对括号中的参数进行各种更改,例如操作和替换文本。

There are a few stocking stuffers that all our giftees deserve. Let’s make that a variable:

我们所有的礼品送达者都应该得到一些放养的填充器。 让我们将该变量设为:

STUFF=$'socks\nlump of coal\nwhite chocolate'

echo "$STUFF"
socks
lump of coal
white chocolate

Now to add these items to each of our lists with some help from the tee command to get echo and expansions to play nice.

现在, tee命令的帮助下,将这些项目添加到我们的每个列表中,以使echo和扩展效果更好。

echo "$STUFF" | tee {Amy,Brian,Charlie}.txt

cat {Amy,Brian,Charlie}.txt

socks
lump of coal
white chocolate
socks
lump of coal
white chocolate
socks
lump of coal
white chocolate

模式匹配替换 (Pattern match substitution)

On  second thought, maybe the lump of coal isn’t such a nice gift. You can  replace it with something better using a pattern match substitution in  the form of ${parameter/pattern/string}:

再次考虑,也许煤块不是一个好礼物。 您可以使用${parameter/pattern/string}形式的模式匹配替换将其替换为更好的东西:

echo "${STUFF/lump of coal/candy cane}" | tee {Amy,Brian,Charlie}.txt

cat {Amy,Brian,Charlie}.txt

socks
candy cane
white chocolate
socks
candy cane
white chocolate
socks
candy cane
white chocolate

This replaces the first instance of “lump of coal” with “candy cane.” To replace all instances (if there were multiple), use ${parameter//pattern/string}. This doesn’t change our $STUFF variable, so we can still reuse the original list for someone naughty later.

这将“煤块”的第一个实例替换为“糖果棒”。 要替换所有实例(如果有多个实例),请使用${parameter//pattern/string} 。 这不会更改我们的$STUFF变量,因此以后我们仍然可以为顽皮的人重用原始列表。

子串 (Substrings)

While  we’re improving things, our giftees may not all like white chocolate.  We’d better add some regular chocolate to our lists just in case. Since  I’m super lazy, I’m just going to hit the up arrow and modify a previous  Bash command. Luckily, the last word in the $STUFF variable is “chocolate,” which is nine characters long, so I’ll tell Bash to keep just that part using ${parameter:offset}. I’ll use tee’s -a flag to append to my existing lists:

在我们改善事情的同时,我们的礼物送达人可能并不都喜欢白巧克力。 我们最好将一些常规巧克力添加到清单中,以防万一。 由于我超级懒惰,所以我只想点击向上箭头并修改以前的Bash命令。 幸运的是, $STUFF变量中的最后一个单词是“ chocolate”,它长9个字符,所以我将告诉Bash使用${parameter:offset}保留该部分。 我会用tee-a标志a PPEND到现有列表:

echo "${STUFF: -9}" | tee -a {Amy,Brian,Charlie}.txt

cat {Amy,Brian,Charlie}.txt

socks
candy cane
white chocolate
chocolate
socks
candy cane
white chocolate
chocolate
socks
candy cane
white chocolate
chocolate

You can also:

你也可以:

Do thisWith this
Get substring from n characters onwards${parameter:n}
Get substring for x characters starting at n${parameter:n:x}
做这个 有了这个
n个字符开始获取子字符串 ${parameter:n}
获取从n开始的x个字符的子字符串 ${parameter:n:x}

There! Now our base lists are finished. Let’s have some eggnog.

那里! 现在我们的基本列表已完成。 让我们来一些蛋酒。

测试变量 (Testing variables)

You  know, it may be the eggnog, but I think I started a list for Amy  yesterday and stored it in a variable that I might have called amy. Let’s see if I did. I’ll use the ${parameter:?word} expansion. It’ll write word to standard error and exit if there’s no amy parameter.

您知道,可能是蛋酒,但是我想我昨天为Amy创建了一个列表,并将其存储在我可能称为amy的变量中。 让我们看看我是否这样做。 我将使用${parameter:?word}扩展。 它将把word写到标准错误,如果没有amy参数,则退出。

echo "${amy:?no such}"

bash: amy: no such

I guess not. Maybe it was Brian instead?

我猜不会。 也许是布莱恩?

echo "${brian:?no such}"

Lederhosen

You can also:

你也可以:

Do thisWith this
Substitute word if parameter is unset or null${parameter:-word}
Substitute word if parameter is not unset or null${parameter:+word}
Assign word to parameter if parameter is unset or null${parameter:=word}
做这个 有了这个
如果parameter未设置或为空,则替换word ${parameter:-word}
如果parameter未设置或为空,则替换word ${parameter:+word}
如果parameter未设置或为空,则将word分配给parameter ${parameter:=word}

改变大小写 (Changing case)

That’s  right! Brian said he wanted some lederhosen and so I made myself a  note. This is pretty important, so I’ll add it to Brian’s list in  capital letters with the ${parameter^^pattern} expansion. The pattern part is optional. We’re only writing to Brian’s list, so I’ll just use >> instead of tee -a.

那就对了! 布赖恩说,他想要一些皮裤,所以我给自己写了一张纸条。 这非常重要,因此我将使用${parameter^^pattern}扩展${parameter^^pattern}其以大写字母添加到Brian的列表中。 pattern部分是可选的。 我们仅写给Brian的列表,所以我只用>>而不是tee -a

echo "${brian^^}" >> Brian.txt

cat Brian.txt

socks
candy cane
white chocolate
chocolate
LEDERHOSEN

You can also:

你也可以:

Do thisWith this
Capitalize the first letter${parameter^pattern}
Lowercase the first letter${parameter,pattern}
Lowercase all letters${parameter,,pattern}
做这个 有了这个
首字母大写 ${parameter^pattern}
小写第一个字母 ${parameter,pattern}
小写所有字母 ${parameter,,pattern}

扩展数组 (Expanding arrays)

You know what, all this gift-listing business is a lot of work. I’m just going to make an array of things I saw at the store:

您知道吗,所有这些礼品清单业务都是很多工作。 我将整理一系列在商店中看到的东西:

gifts=(sweater gameboy wagon pillows chestnuts hairbrush)

I can use substring expansion in the form of ${parameter:offset:length} to make this simple. I’ll add the first two to Amy’s list, the middle two to Brian’s, and the last two to Charlie’s. I’ll use printf to help with newlines.

我可以使用${parameter:offset:length}形式的子字符串扩展来简化此过程。 我将前两个添加到Amy的列表中,将中两个添加到Brian的列表中,将后两个添加到Charlie的列表中。 我将使用printf来帮助换行。

printf '%s\n' "${gifts[@]:0:2}" >> Amy.txt
printf '%s\n' "${gifts[@]:2:2}" >> Brian.txt
printf '%s\n' "${gifts[@]: -2}" >> Charlie.txt
cat Amy.txt

socks
candy cane
white chocolate
chocolate
sweater
gameboy

cat Brian.txt

socks
candy cane
white chocolate
chocolate
LEDERHOSEN
wagon
pillows

cat Charlie.txt

socks
candy cane
white chocolate
chocolate
chestnuts
hairbrush

There! Now we’ve got a comprehensive set of super personalized gift lists. Thanks Bash! Too bad it can’t do the shopping for us, too.

那里! 现在,我们有了一套全面的超级个性化礼物清单。 谢谢巴什! 太糟糕了,它也不能为我们购物。

翻译自: https://www.freecodecamp.org/news/bash-shell-expansions/

bash和shell

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值