[英]Linux alias with checking directory
How can I do alias which check if I'm in directory "python27" and when not it will be go to that directory, run some script and come back to home directory?我如何做别名来检查我是否在目录“python27”中,如果不在目录中,它将转到该目录,运行一些脚本并返回主目录? When I'm in "python27" directory it should just run script and come back to home directory.
当我在“python27”目录中时,它应该只运行脚本并返回主目录。 I mean something like this:
我的意思是这样的:
alias p="if [ pwd != /home/myname ] then cd python27 && python $1 && cd ~ else python $1 && cd ~ fi"
Don't use an alias, use a function instead.不要使用别名,而是使用函数。
You don't need to check whether you're in the python27
directory, just cd
there, run the script, and cd
back home.你不需要检查你是否在
python27
目录中,只需cd
那里,运行脚本,然后cd
回家。
Something like this, with minimal checking:像这样的事情,只需最少的检查:
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 ~
}
You could also use python "$@"
instead of python "$1"
in case you need to pass more arguments (eg, options).如果您需要传递更多参数(例如,选项),您也可以使用
python "$@"
而不是python "$1"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.