簡體   English   中英

帶檢查目錄的 Linux 別名

[英]Linux alias with checking directory

我如何做別名來檢查我是否在目錄“python27”中,如果不在目錄中,它將轉到該目錄,運行一些腳本並返回主目錄? 當我在“python27”目錄中時,它應該只運行腳本並返回主目錄。 我的意思是這樣的:

alias p="if [ pwd != /home/myname ] then cd python27 && python $1 && cd ~ else python $1 && cd ~ fi"

不要使用別名,而是使用函數。

你不需要檢查你是否在python27目錄中,只需cd那里,運行腳本,然后cd回家。

像這樣的事情,只需最少的檢查:

p() {
    if [[ -z $1 ]]; then
        echo >&2 "need an argument"
        return 1
    fi
    if ! cd /full/path/to/python27; then
        echo >&2 "can't cd to python27"
        return 1
    fi
    python "$1"
    cd ~
}

如果您需要傳遞更多參數(例如,選項),您也可以使用python "$@"而不是python "$1"

暫無
暫無

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

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