使用 nushell 六個月的心得

在使用 nushell 這六個月期間,nushell 已經成為我主要的 shell 環境。而過去一些簡易的腳本,我都用 python 寫,如今都用 nu script 代替。nu script 用在做一些部署或設定時,遠比 python script 好用許多,能夠直接修改設定檔、執行程式,不需要額外裝東西。而 pipelin 方式,也讓你在寫 script 或是使用 shell 處理東西時,更加容易

一些簡易的 nu script 範例

  • 檢查系統問題並顯示資訊
let priority = { "debug": 0, "info": 1, "warn": 2, "error": 3, "fatal": 4 }
def main [ level: string = "info" ] {
  [
    {||
      let result = (uname -a|str contains x86)
      let level = (if $result { "info" } else { "error" })
      {
        "level": $level,
        "title": "os arch",
        "msg": (uname -a),
      }
    },
    {||
      let result = (cat /proc/cpuinfo|grep processor|wc -l)
      let level = (if (($result|into int) > 9) { "info" } else { "error" })
      {
        "level": $level,
        "title": "cpu number",
        "msg": (cat /proc/cpuinfo|grep processor|wc -l),
      }
    },
  ] | par-each -t 8 {|it| do $it} | filter {|it| ($priority|get $it.level) >= ($priority|get $level) }
}

You'll only receive email when they publish something new.

More from kjelly
All posts