簡體   English   中英

使用awk生成自定義更好的ls-l輸出

[英]Produce a custom nicer ls-l output using awk

我正在嘗試編寫awk腳本以從ls -l生成自定義輸出,如下所示:


File xxx.txt has size of 100 blocks, was last modified on July 3 2013, is owned by Kohn. The user has read permission, has write permission and has execute permission.

Dir abc has size of 200 blocks, was last modified on July 1 2013, is owned by Kohn. The user has read permission, does not have write permission and has execute permission.
...

我發現最困難的任務是解析第一列$ 1以獲得權限和文件/目錄。 您能給我個提示如何解決這個問題嗎?

真誠的,Kohn

不要解析ls使用stat

#!/bin/bash

myls() {
    local filetype=$(stat -c "%F" "$1")
    local format="${filetype^} %n has size of %b blocks, "
    format+="was last modified on $(date -d "@$(stat -c "%Y" "$1")" "+%B %e, %Y"), "
    format+="is owned by %U. "
    format+="$(permissions "$1")"
    stat -c "$format" "$1"
}

permissions() {
    local user_perms=$(stat -c "%A" "$1")
    local string="The user "
    string+="$(has ${user_perms:1:1} r) read permission, "
    string+="$(has ${user_perms:2:1} w) write permission, "
    string+="$(has ${user_perms:3:1} x) execute permission."
    echo "$string"
}
has() { [[ $1 == $2 ]] && echo "has" || echo "does not have"; }

for file; do
    myls "$file"
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM