0

I have created an alias in UNIX in which regex is creating problem.
My intention is to use alias as _enable -input, the parameter should be passed with "-".
Not getting where the problem is.

alias is

alias _enable="function _enable() { [[ $1 =~ ^- ]] || { echo \"Use hyphen (-) before nodes.\"; return 1; } ; [[ $# = 1 ]] && { NODE=`echo ${1##*-}`; } || { NODE=\"\"; } ; $HOME/bin/command --command=enable --node=$NODE; }; _enable $1;"

The actual output is:

SS-04:~ # _enable 1
-bash: conditional binary operator expected
-bash: syntax error near `^-'
Siguza
  • 21,155
  • 6
  • 52
  • 89
pankaj_ar
  • 757
  • 2
  • 10
  • 33

1 Answers1

1

You don't really need an alias and all that quotes escaping.

Just this function is suffice:

_enable() {
   [[ $1 == -* ]] || { echo "Use hyphen (-) before nodes."; return 1; }
   [[ $# -eq 1 ]] && NODE="${1##*-}" || NODE=""
   $HOME/bin/command --command=enable --node="$NODE"
}

Then you can call:

_enable 1
Use hyphen (-) before nodes.
echo $?
1
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • @anubhava...if I am adding the function _enable in an executable file and then calling it, its working fine. Even the one which I have posted, that was aslo working fine as a script. But when I have put it inside an alias, in that case only I am facing issue. The one which you have posted, after adding this method to alias, below output is coming... `SS-04:~ # _enable -p Use hyphen (‐) before nodes. -bash: -p: command not found` – pankaj_ar Aug 06 '15 at 06:43
  • You do not need to put it inside an alias at all. Just add this function definition in your. bashrc file and it will be available every where in your shell. – anubhava Aug 06 '15 at 07:08