4

I'm using Ubuntu terminal to manage my works. I created some alias names that help me to do the work faster.

for example:

$ alias kprod="kubectl --namespace=hello"

Then use it like this:

$ kprod get pods

One of my major tasks is to find when is my application is ready to run on the cloud, so I call this code:

$ kprod get pods | grep APPNAME

But I need to run it again and again until it ready. I want to use watch command to simplify this task. Unfortunately, watch not working because kprod is an alias.

$ watch kprod get pods
sh: 1: kprod: not found

My Questions:

  1. Is this possible to solve this specific problem with watch shell command?
  2. Is it possible to make a more generic solution that will "translate" the command for the actual expression? For example:

    $ kprod get pods | TRANSLATE
    kubectl --namespace=hello get pods
    

which make it work with watch and any other application?

UPDATE

Using type command is similar but different. As I tried, it's impossible to give it a full expression and "translate" it because it's trying to convert each token of the expression seperatly. Of cause the pod (in my example above) is not alias string...

For example:

$ type kprod get pods
kprod is aliased to `kubectl --namespace=hello'
-bash: type: get: not found
-bash: type: pods: not found

Yes, i can do it manually with the type command, but it's manually. I looking for automated way.

2 Answers2

1

Aliases serve for you as a shortcut in the terminal. It is not the other way around. The way you intend to use aliases is not how they are intended.

In principle, just type out the command in full when used in automation. However, you could turn your aliases into a command by instead creating an executable script:

Contents of a bash script kprod

#!/bin/bash
kubectl --namespace=hello $@

The $@ at the end of the command will expand to all the parameters you passed on the command line.

Put this script in a folder that is in your path. That way, simply typing the name will run the script. Although somewhat more difficult to set up, it is equally easy to use as your alias. In addition, you can also use it in automation.

vanadium
  • 88,010
1

Yes, you can make a little script called watch_alias (or whatever) that will expand the alias and then pass the expanded command to watch. Save the following as ~/bin/watch_alias and make it executable (chmod a+x ~/bin/watch_alias):

#!/bin/bash

## enable alias expansion in scripts
shopt -s expand_aliases
## source the .bashrc file where your aliases should be
. ~/.bashrc

## The first argument of the script is the alias
aliasCom=$1
## Remove the 1st argument from the $@ array (the script's arguments)
shift

## Parse the output of type to get what the real command is
realCom=$(type "$aliasCom" | grep -oP "\`\K.[^']+")
## watch the unaliased command
watch bash -c "$realCom '$@'"

You can then run this instead of watch:

watch_alias kprod get pods
terdon
  • 100,812
  • Interesting! the command watch_alias kprod get pods working great. I couldn't make it work with pipes. For example: watch_alias 'kprod get pods | grep HELLO' – No1Lives4Ever Jun 05 '19 at 11:07
  • 1
    @No1Lives4Ever no, you can't watch pipes anyway, that isn't a problem with the alias. Try watch ls | grep a or something. The pipe isn't passed to watch, it is trying to pipe the output of watch. The way to watch pipes is to give the whole command in a new shell: watch bash -c 'ls | grep a'. It should work with my (updated) script if you run it like this: watch_alias kprod 'get pods | grep HELLO' – terdon Jun 05 '19 at 11:41