如何检查我的Bash功能是否有参数?
-
11-10-2019 - |
题
快速问题。我爱Emacs。我讨厌打字内容,所以我喜欢使用 e
调用emacs。
以前,我的.bash_profile(OS X)配置为: alias e="emacs ."
. 。但是,当我只想编辑一个文件时,我仍然厌倦了仍必须键入emacs {file}。
因此,我试图通过一些谷歌搜索来鞭打这个问题,但Bash抱怨 []
:
###smart emacs, open file, or if none, dir
e()
{
if [$1]; then
emacs $1
else
emacs .
fi
}
我想用这个做: e something.c
要不就 e
.
解决方案
#!/bin/bash
###smart emacs, open file, or if none, dir
e()
{
if [[ -z "$1" ]]; then # if "$1" is empty
emacs .
else
emacs "$1"
fi
}
其他提示
尝试
if [ $# -ge 1 ]; then
emacs "$@"
我认为Bash对空间非常奇特。我什至很惊讶您可以忽略功能名称和()之间的空间。 (另外,使用$@应该打开您通过的所有文件。)
ETA:更好地检查参数数量的情况 e "" foo.txt
...
不隶属于 StackOverflow