繁体   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