missing之bash脚本编写-4

2021-01-13  本文已影响0人  墨道院

用通配符和大括号实现的shell globbing 的便利功能

当调用脚本的时候,你可能会输入一些类似的,同质的选项。正好Bash有一些技巧可以扩展文件的后缀名,叫做shell globbing,中文我也暂时不知道怎么翻译。

看看下面的例子:

convert image.{png,jpg}
# Will expand to
convert image.png image.jpg

cp /path/to/project/{foo,bar,baz}.sh /newpath
# Will expand to
cp /path/to/project/foo.sh /path/to/project/bar.sh /path/to/project/baz.sh /newpath

# Globbing techniques can also be combined
mv *{.py,.sh} folder
# Will move all *.py and *.sh files
mkdir foo bar
# This creates files foo/a, foo/b, ... foo/h, bar/a, bar/b, ... bar/h
touch {foo,bar}/{a..h}
touch foo/x bar/y
# Show differences between files in foo and bar
diff <(ls foo) <(ls bar)
# Outputs
# < x
# ---
# > y

编写Bash脚本是一件非常反直觉的事情,因此有个工具shell check可以像编译器那样检查你的脚本写的是否有问题。

上一篇 下一篇

猜你喜欢

热点阅读