5

純 POSIX sh 實作各種功能

 1 year ago
source link: https://blog.gslin.org/archives/2023/05/02/11168/%e7%b4%94-posix-sh-%e5%af%a6%e4%bd%9c%e5%90%84%e7%a8%ae%e5%8a%9f%e8%83%bd/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

純 POSIX sh 實作各種功能

看到「pure sh bible」這篇,講純 POSIX 的 sh 可以怎麼實作各種功能,Hacker News 上的討論在「Pure Sh Bible (github.com/dylanaraps)」這邊。

一般我在寫 cross platform 的 shell script 時候會假設是 POSIX environment,而不是只有 POSIX sh 硬扛,所以還是有 awkgrepsed 這類工具可以用... 而這篇裡面提到的方法有些很明顯是硬扛過去的 XD

不過裡面還是有些章節是應該要熟悉的,像是對 string 的處理算是蠻好用的,不需要拿 sed 出來用。

另外可以用 bash 的話,有很多東西會好寫很多,但開頭的 #!/bin/bashFreeBSD 上會因為 bash 會裝到 /usr/local/bin/bash,就不是很好搞... 一個解法是用 /usr/bin/env bashPATH 找,但也不是完全保險的方法 (PATH 有時候會清空...)。

Related

Bash Script 的好習慣

這篇給了一份 bash script 用的 tempalte,但更重要的反而是裡面提到的 best practice:「Minimal safe Bash script template」。 首先是不要寫 /bin/bash 這件事情,因為有些系統是沒有 /bin/bash 的,像是 FreeBSD。 如果程式是可以用 POSIX sh 語法的話,應該優先考慮 /bin/sh,如果用到非 POSIX 標準的語法的話,用 env 帶出來會少一些問題: #!/usr/bin/env bash 再來是 fail 時就趕快停止,不要再往下執行,這點算是老生常談了,文章作者也有給一個範例說明: set -Eeuo pipefail 再來另外一個還蠻有用的事情是攔下常見的 signal 處理「後事」: trap cleanup SIGINT SIGTERM ERR EXIT cleanup() { trap - SIGINT SIGTERM ERR…

December 19, 2020

In "Computer"

第四堂:「Data Wrangling」

有陣子沒寫了,來還個債... 這個系列是從『MIT 的「The Missing Semester of Your CS Education」』這邊延伸出來的,這邊講的是「Data Wrangling」這篇。 這篇是在講 pipe 的用法,在講這些工具之前,其實有個很重要的概念應該要說明 (但沒有在這篇文章裡被提到),也就是 Unix philosophy,這個哲學是指 unix 環境下的工具,都會設計成只做好一件事情。 而要怎麼把這些工具串起來,最常見的就是 pipe,你可以在文章裡看到 grep、sed 與 sort 這些工具的用法,以及怎麼用 pipe 串起來。 這邊剛好也可以提一下,利用 pipe 可以把不同功能打散到不同的 process 上,剛好也可以稍微利用到現在常見的多 CPU 的環境。 另外上面因為提到了 grep,文章內花了不少篇幅在講 Regular expression 這個在 CS 課程裡面也是重要的基礎。 會放這種篇幅長度,一方面是 Regular expression 的實用性很高,另外一方面,學術上與自動機理論中的 DFA 與 NFA 都有關,算是學習計算理論的起點: 然後後面就有提到 AWK…

June 30, 2020

In "Computer"

MySQL 5.1.26

MySQL 5.1.26-rc 放出來了:MySQL 5.1.26-rc has been released。這個版本會是 5.1 最後一個 RC (Release Candicate),下一個就會是 Production Ready (GA) 的版本了。 不過 Paul McCullagh (PBXT 的開發者) 寫了一篇「Mutex contention and other bottlenecks in MySQL」,裡面發現他已經把 PBXT 改到本身已經不是瓶頸,所以反過來開始看 MySQL 的問題... 裡面有很多很有趣的惡搞 (像是他覺得,如果在他的環境裡如果拿掉某些 POSIX mutex lock 不會遇到問題,他就先拔掉 lock 然後看效能可以提昇多少,如果不能拿掉,他就用自己改寫的 spinlock 或是其他方式替代),除此之外,文章裡還有一些數據可以看,可以看出他一步一步改善所走過的路,以及還有哪些地方值得改善。 最後改出來的成果相當輝煌,改到有 60% 的時間會因為在等網路另一邊的 query 進來。 在 comment 有人有提到可以用…

July 15, 2008

In "Computer"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on May 2, 2023Categories Computer, Murmuring, Programming, SoftwareTags posix, script, sh, shell

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK