0
我在我的.bashrc文件中寫了一個別名,每當我啓動bash shell時都會打開一個txt文件。 問題是我想打開這樣的文件只有一次,那是我第一次打開shell。在bashrc中只使用別名
有沒有辦法做到這一點?
我在我的.bashrc文件中寫了一個別名,每當我啓動bash shell時都會打開一個txt文件。 問題是我想打開這樣的文件只有一次,那是我第一次打開shell。在bashrc中只使用別名
有沒有辦法做到這一點?
此問題的一般解決方案是具有某種會話鎖定。您可以使用正在編輯其他文件的進程的pid和/或tty創建文件/ tmp/secret,並在完成時刪除鎖定文件。現在,您的其他會話應設置爲不創建該文件(如果該文件已存在)。
正確鎖定是一個複雜的主題,但對於簡單的情況,這可能已經足夠好了。如果沒有,谷歌的「互斥」。請注意,如果您弄錯了,可能會有安全隱患。
爲什麼你使用這個別名?聽起來像代碼應該直接在你的.bashrc中,而不是在別名定義中。
所以如果說,你現在在你的.bashrc
什麼是一樣的東西
alias start_editing_my_project_work_hour_report='emacs ~/prj.txt &̈́'
start_editing_my_project_work_hour_report
unalias start_editing_my_project_work_hour_report
...然後用鎖,沒有別名,你可能會與一些落得像
# Obtain my UID on this host, and construct directory name and lock file name
uid=$(id -u)
dir=/tmp/prj-$uid
lock=$dir/pid.lock
# The loop will execute at most twice,
# but we don't know yet whether once is enough
while true; do
if mkdir -p "$dir"; then
# Yay, we have the lock!
(echo $$ >"$lock" ; emacs ~/prj.txt; rm -f "$lock") &
break
else
other=$(cat "$lock")
# If the process which created the UID is still live, do nothing
if kill -0 $other; then
break
else
echo "removing stale lock file dir (dead PID $other) and retrying" >&2
rm -rf "$dir"
continue
fi
fi
done
我不明白這個問題的動機。如果你想讓命令只在第一次登錄時執行,把它們放在'.login'或'.bash_login'中,當它們是登錄shell時,它們由sh-shells執行,不像.bashrc執行登錄和非登錄bash shell。 – 2012-03-22 12:26:26
我試過編輯這些文件,但沒有奏效。我剛剛把命令'gvim path/file.txt',然後當我開始bash會話什麼都沒有發生。 – whatsup 2012-03-22 14:54:11
同意你應該嘗試解決這個問題,或者也可以。發佈一個單獨的問題! – tripleee 2012-03-22 20:00:19