Liuw's Thinkpad

想要赢就先学会输,想要成功就先学会失败

Archive for the ‘bash’ tag

近期收集的一些有趣的小東東

without comments

近來比較少寫blog,實在是比較忙。用繁體寫是因為看簡體的字多了眼花,得換換口味了。

72pines的頁面很奇怪,假如我不用搜狗的全網加速功能去開,出來的頁面就沒有樣式了。可能它有一部份的伺服器被教育網墻掉了(?)。

在王亮先生的Emacs定制和擴展中,找到一個“能獲取root權限的輔助腳本”,解決了我一直頭痛的一個問題。原理挺簡單,使用TRAMP來完全的。以前一直以為TRAMP主要是用于遠程編輯文件的,沒有發現這個功能,沒仔細看Manual的後果。

(defun wl-sudo-find-file (file dir)
  (find-file (concat "/sudo:localhost:" (expand-file-name file dir))))

當然,其他的內容也很精彩。

第二個,是從Wowubuntu得來的三手信息。爲什麽說是三手呢,因為它也只是轉載的。主要是講Bash shell的一些奇技淫巧。

最牛B的 Linux Shell 命令 系列连载,有點標題黨了,呵呵。

比較有趣的是活用history功能的條目,比如說sudo執行前一條命令是:

$ sudo !!

兩個嘆號“!!”等價于“!-1”,數字換成其他也是可以的,不過誰記得了那麼多啊。

另外一個有趣的功能就是Bash的替換(原來它也有啊,呵呵):

$ !!:s/foo/bar/

很熟悉的語法,只是從來沒有想到過還有這個功能,sigh。

文章提到上面兩個技巧都是出自The Definitive Guide to Bash Command Line History。

還有一些其他的技巧,不僅僅限於Bash本身,不一而足。由於有的要么知道了,要么對我自己用處不大,這里就不多記了。

Written by liuw

August 26th, 2010 at 7:00 pm

Posted in UNIX-like,分享

Tagged with , ,

Bash File Test Operators

without comments

From Advanced Bash-Scripting Guide

Return true if…

-e         file exists
-f         file is a regular file
-s         file is not zero size
-d         file is a directory
-b         file is a block device
-c         file is a character device
-p         file is a pipe
-h/-L      file is a symbolic link
-S         file is a socket
-t         file (descriptor) is associated with a terminal device
           This test option may be used to check whether the stdin [ -t 0 ]
           or stdout [ -t 1 ] in a given script is a terminal
-r         file has read permission (for the user running the test)
-w         file has write permission
-x         file has execute permission
-g         set-group-id (sgid) flag set on file or directory
-u         set-user-id (suid) flag set on file
-k         sticky bit set
-O         you are owner of file
-G         group-id of file same as yours
-N         file modified since it was last read
f1 -nt f2  file f1 newer than f2
f1 -ot f2  file f1 older than f2
f1 -ef f2  file f1 and f2 are hard links to the same file
!          "not" -- reverses the sense of the test above (return true if
           condiction absent)

Written by liuw

December 16th, 2009 at 10:59 pm

Posted in UNIX-like,分享

Tagged with , ,

What’s True?

without comments

A bash(1) script, from Advanced Bash-Scripting Guide.

#!/bin/bash

echo
if [ 0 ]; then
    echo "0 is true"
else
    echo "0 is false"
fi

echo
if [ 1 ]; then
    echo "1 is true"
else
    echo "1 is false"
fi

echo
if [ -1 ]; then
    echo "-1 is true"
else
    echo "-1 is false"
fi

echo
if [  ]; then
    echo "NULL is true"
else
    echo "NULL is false"
fi

echo
if [ xyz ]; then
    echo "random string is true"
else
    echo "random string is false"
fi

echo
if [ $uninit_var ]; then
    echo "Uninitialized variable is true"
else
    echo "Uninitialized variable is false"
fi

echo
if [ -n "$xyz" ]; then
    echo "Uninitialized variable is true"
else
    echo "Uninitialized variable is false"
fi

xyz=
echo
if [ -n "$xyz" ]; then
    echo "Null variable is true"
else
    echo "Null variable is false"
fi

echo
if [ "false" ]; then
    echo "\"false\" is true"
else
    echo "\"false\" is false"
fi

echo
exit 0

Written by liuw

December 16th, 2009 at 10:44 pm

Posted in Programming,UNIX-like,分享

Tagged with ,

Bash/VIM/EMACS中输入特殊字符

without comments

有时用VIM打开一个文本文件,可以看到有的行结尾是^M来结束的,很不美观。在进行替换的时候,这个^M(还有一些其他类似的字符)是不能以一个^加一个M的形式进行输入的,因为它本身是一个字符。

在Bash和VIM中,可以以C-v C-m的形式去输入^M,其他的一些特殊字符也是同理的。

而在EMACS中,C-v已经有所用了,显然就不是用C-v C-m来做了。想起来以前是怎么输入TAB的,小试验一下,成了。实际就是调用quoted-insert这个函数去完成的。按键是C-q C-m。

Written by liuw

November 30th, 2009 at 8:47 pm

Posted in UNIX-like,分享

Tagged with , ,

Shorter Bash Prompt

without comments

原来的Bash prompt很啰嗦,进入的第一个目录都要显示。当目录搞深了,prompt本身就很长。

$ man bash

PROMPTING一节中,有关于prompt的格式写法。

$ export PS1="[\u@\h \W]\\$ "

Written by liuw

November 30th, 2009 at 11:44 am

Posted in UNIX-like

Tagged with ,