开发者

How can I check if my Bash function got an argument or not?

Quick question. I love emacs. I hate typing stuff though, so I like to use e to invoke emacs.

Previously I had my .bash_profile (OS X) configured as: alias e="emacs .". However, I was tired of still having to t开发者_StackOverflowype emacs {file} when I just wanted to edit one file.

So I tried to whip this up, from some googling, but bash is complaining about the []:

###smart emacs, open file, or if none, dir 
e()
{
    if [$1]; then
        emacs $1
    else
        emacs .
    fi
}

I want to use this to do: e something.c or, just e.


Try

if [ $# -ge 1 ]; then
  emacs "$@"

I think bash is very peculiar about spaces. I'm even surprised you're allowed to omit a space between function name and (). (Also, using $@ should open all files you pass.)

ETA: better check against number of arguments, in case of e "" foo.txt...


#!/bin/bash                       

###smart emacs, open file, or if none, dir
e()
{
    if [[ -z "$1" ]]; then # if "$1" is empty
        emacs .
    else
        emacs "$1"
    fi
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜