mirror of
				https://github.com/Mabbs/mabbs.github.io
				synced 2025-11-04 07:19:41 +00:00 
			
		
		
		
	更新 _posts/2022-03-27-radio.md, _posts/2019-04-24-shell.md, _posts/2021-05-07-ssh.md, _posts/2022-03-12-alpine.md, _posts/2022-02-15-termux.md
This commit is contained in:
		@@ -9,13 +9,13 @@ tags: [Shell, 编程]
 | 
			
		||||
<!--more-->
 | 
			
		||||
 | 
			
		||||
Shell 脚本很棒,你可以非常轻松地写出有用的东西来。甚至像是下面这个傻瓜式的命令:
 | 
			
		||||
```
 | 
			
		||||
```shell
 | 
			
		||||
# 用含有 Go 的词汇起名字:
 | 
			
		||||
$ grep -i ^go /usr/share/dict/* | cut -d: -f2 | sort -R | head -n1
 | 
			
		||||
goldfish
 | 
			
		||||
```
 | 
			
		||||
如果用其他编程语言,就需要花费更多的脑力,用多行代码实现,比如用 Ruby 的话:
 | 
			
		||||
```
 | 
			
		||||
```ruby
 | 
			
		||||
puts(Dir['/usr/share/dict/*-english'].map do |f|
 | 
			
		||||
  File.open(f)
 | 
			
		||||
    .readlines
 | 
			
		||||
@@ -25,7 +25,7 @@ end.flatten.sample.chomp)
 | 
			
		||||
Ruby 版本的代码虽然不是那么长,也并不复杂。但是 shell 版是如此简单,我甚至不用实际测试就可以确保它是正确的。而 Ruby 版的我就没法确定它不会出错了,必须得测试一下。而且它要长一倍,看起来也更复杂。   
 | 
			
		||||
 | 
			
		||||
这就是人们使用 Shell 脚本的原因,它简单却实用。下面是另一个例子:
 | 
			
		||||
```
 | 
			
		||||
```shell
 | 
			
		||||
curl https://nl.wikipedia.org/wiki/Lijst_van_Nederlandse_gemeenten |
 | 
			
		||||
    grep '^<li><a href=' |
 | 
			
		||||
    sed -r 's|<li><a href="/wiki/.+" title=".+">(.+)</a>.*</li>|\1|' |
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user