3

I am working with the command line executing the same commands many times manually following the same pattern. Now I am looking for a way to simplify it by just typing the command only once.

Lets take a look at a normal docker example:

docker ps
docker ps -a
docker ps -l
docker stop x 
docker start x 
docker start y 
docker logs y
docker logs -f z

This example also applies to many more commands such as git, brew, gulp, gcloud.

Now I am looking for some sort of a command wrapper shell, that allows me to write with docker that will wraps any command in a nested/sub shell.

Then I don't need to prepend the docker command and just call:

>ps
# does docker ps and displays result 
>stop x
# prepends docker so docker stop x is actually executed   
CTRL+C # to exit the command wrapper 

Does something like this already exist? I was googling for it but could not describe it properly hence I didn't find anything.

5
  • Like a bash alias? Something like this in your .bashrc should work alias dockerstuff = 'docker ps && docker stop x && docker foo'.
    – duenni
    Feb 2, 2015 at 10:50
  • An alias would be to specific task.I am not executing all the commands the same way. I would otherwise write a script to help me on that.
    – Vad1mo
    Feb 2, 2015 at 12:05
  • Ahh, ok. Now I got it. Misunderstood this in the first place.
    – duenni
    Feb 2, 2015 at 12:11
  • 2
    Kinda like what happens if you type telnet or gdb?
    – ewwhite
    Feb 2, 2015 at 12:52
  • @ewwhite exactly something like telnet or dgb! but more general and usable for all commands.
    – Vad1mo
    Feb 2, 2015 at 16:37

2 Answers 2

5

You could also define a function yourself, and include it in your .bash_profile or similar:

function with {
    echo -n "$1> "
    while read input
    do
        if [[ $input == "exit" ]]
        then
            break
        fi
        eval "$1 $input"
        echo -n "$1> "
    done
    echo
}

Example usage:

user@host $ with git
git> status
# On branch master
nothing to commit (working directory clean)
git> exit

user@host $

EDIT: This function does not do input sanitization or anything, so use at your own risk etc...

5
  • trying with docker I get -- docker> images Error: Command not found: input
    – Vad1mo
    Feb 2, 2015 at 12:16
  • Sorry, missed a $ when I typed it out. I've updated the answer - the offending line is the 'eval' one.
    – shearn89
    Feb 2, 2015 at 13:10
  • Your script will do the job. But I am wondering is my requirement so stupid, uncommon or trivial that there is nothing out there yes?
    – Vad1mo
    Feb 2, 2015 at 16:39
  • I think it's a problem that is normally solved with aliases. For example, I use git aliases a lot: gst for git status, ga for git add, gc for git commit, gp for git pull. These plus fast typing speed render a prompt such as the one above irrelevant. Also, why look for an entire program you need to install when bash can do what you wish in only a few lines?
    – shearn89
    Feb 2, 2015 at 16:42
  • 1
    @Vadimo: If there where a tool out there, it would essentially be a fancy version of this answer. Also, asking for a product is off-topic here.
    – Sven
    Feb 2, 2015 at 16:42
1

You could write a bash or other shell script to do this. An easy alternative that's almost as good would be just to define short aliases and prepend them, for example

alias d=docker
alias g=gcloud

and so on. Then run

d ps
d ps -a

and so on, which is hardly more work than typing just the commands.

2
  • this does not solve the problem it just gives paints it in a different color. Also the english alphabet has only 26 letters but I have more commands and I have to memorize if what each letter means.
    – Vad1mo
    Feb 2, 2015 at 12:09
  • 2
    Understood. But it has the virtue of simplicity, and uses the built-in power of the command line. You don't need to write a script and try to cover all cases. I use dozens of aliases or shell functions. Each makes my life easier in a specific way, without requiring writing a script. Feb 2, 2015 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .