Bash - 需要在登入時使 df 輸出對使用者友好

Bash - 需要在登入時使 df 輸出對使用者友好

我試圖在用戶登入時向他們顯示磁碟可用空間。基本上是 df -h 的輸出。我將其添加到 .bash 中:

  echo -ne '\e[0;34m'"Disk: \e[m"$(df -h)"\n"

它工作正常並且在 shell 登入時自動顯示此內容,但問題是輸出很擁擠。不保留列格式。它看起來像這樣:

檔案系統1024塊使用的可用容量安裝在/dev /sda6 468789752 4907272 440046272 2% /無4 0 4 0% /sys /sys /fs /fs /cgroup UDEV 3785472 48 385/run 3785472 8 /run /none none none none none none none none none none none unt用戶 /dev/sda1 184307 66572 104116 40% /boot

我希望它以與用戶正在運行時相同的格式顯示df -h。關於如何修改我的命令以獲得用戶友好的輸出有什麼建議嗎?我也嘗試過使用,df -P | column -t但它仍然很狹窄。

使用Ubuntu伺服器。

答案1

報價全亂了。這應該有效

 echo -ne '\e[0;34m' Disk: '\e[m' "$(df -h)" "\n"

基本上如果你這樣做

managemac3$ df=`df -h`
managemac3$ echo $df
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on /dev/disk0s2 233Gi 54Gi 179Gi 24% 14151768 46917672 23% / devfs 207Ki 207Ki 0Bi 100% 716 0 100% /dev map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /home
managemac3$ echo "$df"
Filesystem      Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
/dev/disk0s2   233Gi   54Gi  179Gi    24% 14151768 46917672   23%   /
devfs          207Ki  207Ki    0Bi   100%      716        0  100%   /dev
map -hosts       0Bi    0Bi    0Bi   100%        0        0  100%   /net
map auto_home    0Bi    0Bi    0Bi   100%        0        0  100%   /home
managemac3$

變數周圍的引號可以檢查格式

相關內容