2018-03-19

Get Linux Distribution Name (Tcl exec 版)

在最後嘗試 Bash 的版本。在嘗試以後,寫好了 Bash 版,使用 cat, grep 和 sed 取得結果。
#!/bin/bash

if [ -f '/etc/os-release' ]; then
    cat '/etc/os-release' | grep -w "^NAME" | sed -e 's/NAME=//g'
fi


於是我就想,那我可以使用同樣的方式在 Tcl  嗎?答案是可以。
#!/usr/bin/env tclsh
#
# Execute external program to get result
#

package require Tcl 8.6

set myfunction {{} {
    return [exec cat /etc/os-release | grep -w "^NAME" | sed -e "s/NAME=//g"]
}}

if {[file exists "/etc/os-release"]==1} {
    puts [apply $myfunction]
}

為了增加變化,所以我使用了匿名函式的寫法。

沒有留言: