2015年2月23日 星期一

profile與bashrc的差別與在archlinux中的執行順序

根據Bash Guide for Begineers的說法,
profile用來放環境變數(Environment variables)例如: PATH, USER, MAIL,HOSTNAME及HISTSIZE等
bashrc則是用來放function與alias,
由於使用者在每次登入時都會去讀/etc/profile,
因此在多shell環境的系統上將專屬於某特定shell的設定分開放可以避免一些相容性問題.
例如Bourne shell看不懂Bash語法之類的.

直接觀察新安裝的archlinux, 跟shell有關係的初始檔有這些
/etc/profile
/etc/bash.bashrc或/etc/bashrc                  #archlinux/ubuntu/debian裡的是bash.bashrc, Fedora/CentOS裡的會是bashrc
~/.bash_profile
~/.bashrc
大致上分為profile與bashrc兩群,
實驗常見的使用情景一, 不考慮初始檔內部source別人的話,
從login到拿到shell為止, 系統會自動以這樣的順序執行這些rc檔,
1. /etc/profile
2.~/.bash_profile
這種場景不會去執行到/etc/bash.bashrc與~/.bashrc

場景二:
不login拿到shell, 例如直接/bin/bash, su 或是在X裡開個新terminal拿到shell,
執行的順序是:
1./etc/bash.bashrc
2.~/.bashrc
這個場景不會執行/etc/profile與~/.bash_profile,
但是fork出來的child shell還是會拿到parent shell的變數,
因此不做login-specific initializations沒問題

所以可以理解在多shell的系統環境下, 每個user可能使用不同的shell,
因此用profile處理login-specific initializations,
用bashrc處理Bash的shell-specific initializations, 兩者獨立.
至於從login拿到shell的人就分別在system-wide與individual-wide的profile檔裡source bashrc檔做shell-specific initializations.
果然在/etc/profile與~/.bash_profile裡看到利用source的方式叫用bashrc

/etc/profile:
if test "$PS1" && test "$BASH" && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi

~/.bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc

這樣流程就很好理解了,
System-wide configuration then individual-wide configuration,
Login-specific initializations then Shell-specific initializations,
從Login開始的話就是(/etc/profile -> /etc/bash.bashrc) =>(~/.bash_profile -> ~/.bashrc)

#值得一提的是,
distributer建議自訂系統層級的環境變數做法是寫成shell script後放進/etc/profile.d,
而不是自己去改/etc/profile, 免得未來系統升級時要手動merge兩個版本的/etc/profile,
這是不太允許重灌的系統管理員會有的顧慮,
個人使用者要不要這樣做隨自己的便,
當然個人化的環境變數設定放進~/.bash_profile這是沒有太大問題的

2015年2月17日 星期二

中文化vconsole

作法:
安裝fbterm
#pacman -S fbterm

想執行fbterm的user加入video群組
#usermod -aG sudo land

讓普通user能按Ctrl+Space切換輸入法
#chmod u+s /usr/bin/fbterm
或是
#sudo setcap 'cap_sys_tty_config+ep' /usr/bin/fbterm

vim ~/.fbtermrc
#也可以換成DejaVu Sans Mono, Droid Sans Fallback, 多試幾種, 有字距過窄切掉邊邊或是出現重影的問題,
font-name=Monaco, et24l
font-size=14
input-method=fcitx-fbterm

安裝fcitx輸入法
#pacman -S fcitx fcitx-chewing fcitx-configtool fcitx-fbterm

編輯~/.xinitrc
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS="@im=fcitx"

編輯~/.profile
alias fbterm='LANG=zh_TW.UTF-8 fbterm'
export DISPLAY=:0
#fbterm -i fcitx-fbterm #選用, 沒要進shell就開fbterm的話不需要這個

運行可中文輸入的utf fbterm(for 識別原生中文檔名 or UTF PTT(ssh bbsu@ptt.cc))
#Linux本身就是UTF ready, 故中文檔名為utf編碼, 只需要支援中文的utf字型就可以看的見中文檔名
#export LC_ALL=zh_TW.UTF-8

運行可中文輸入的big5 fbterm(for 7777 or telnet to ppt)
#export LC_ALL=zh_TW.Big5;fcitx-fbterm-helper

問題:
1.cannot communicate fcitx with dbus...
成因: 直接執行fcitx開啟輸入法
解法: 內定會先偵測X11是不是在運行, 要在fbterm下使用的話, 使用fcitx-
fbterm-helper, 先載入fcitx後呼叫fbterm
#fcitx-fbterm-helper

2.stdin isn't a interactive tty...
其實沒解,過一陣子再跑一次fcitx就沒問題

2015年2月16日 星期一

自定Console下提示符號顏色

# == Colors ==
red='\[\033[0;31m\]'
RED='\[\033[1;31m\]'
green='\[\033[0;32m\]'
GREEN='\[\033[1;32m\]'
yellow='\[\033[0;33m\]'
YELLOW='\[\033[1;33m\]'
blue='\[\033[0;34m\]'
BLUE='\[\033[1;34m\]'
purple='\[\033[0;35m\]'
PURPLE='\[\033[1;35m\]'
cyan='\[\033[0;36m\]'
CYAN='\[\033[1;36m\]'
white='\[\033[0;37m\]'
WHITE='\[\033[1;37m\]'
NC='\[\033[0m\]'              # No Color
if [ $(id -u) -eq 0 ];
then    # you are root, make the prompt red
        PS1="${white}[ ${red}\u${cyan}@\h:${purple}\w ${white}] - \A \n${red}\$ ${green}"
else
        PS1="${white}[ ${green}\u${cyan}@\h:${purple}\w ${white}] - \A \n${green}\$ ${green}"
fi

#或是
reset=$(tput sgr 0)
red=$(tput sgr 1)
blue=$(tput sgr 4)
green=$(tput sgr 2)
PS1='\[$red\]\u\[$reset\] \[$blue\]\w\[$reset\]  \[$red\]\$ \[$reset\]\[$green\] '

2015年2月12日 星期四

Windows 8 設定開機清理磁碟

Windows系統用久了總是會累積一堆沒刪乾淨的屎, 暫存檔, 安裝檔之類的, 只好想個辦法自動化清一清
  • 磁碟清理工具(cleanmgr.exe) +
  • 工作排程器(控制台->系統及安全性->系統管理工具->工作排程器)
微軟自己對cleanmgr的說明也不是很清楚, 偏偏工作模式又不是很直覺,
我是覺得那樣的用法有點過度設計, 簡單的說用法就兩種:
  1. 檔案管理員->磁碟上按右鍵->磁碟清理, 就是一般的用法, 選磁碟, 選要清理的檔案類型, 等他跑完
  2. cleanmgr.exe /sageset:XX 建立XX號配置, 使用cleanmgr.exe /sagerun:XX語法執行XX號配置
既然要自動化就只能選方法2,
下/sageset:XX 參數帶出清理檔案類型選單讓你選擇想清理的對象,
設定好之後存在登錄檔裡, /sagerun:XX叫出來跑, 執行的對象是所有可以執行的磁碟
把回收桶的清理交由回收桶自己管,
其餘的檔案類型全選, cleanmgr.exe /sageset:99, 存為99號後面用.

叫出工作排程器->動作->基本工作->輸入識別用名稱(可省略)->設為在電腦啟動時執行->啟動程式->指令或程式碼輸入:cleanmgr.exe,
新增引數為: /sagerun:99
完成之後重新開機看看, 雖然是設為電腦啟動執行,
但實際上它內定是等到第一個人登入之後才會動作, 
如果不論如何只要重開機後就想執行的話,
打開工作排程器->儲存的工作內容->一般->安全性選項中即可設定"不論使用者登入與否皆執行"