0

I have a command created and I am trying to convert it to an alias to make it easier to use, however I am unable to solve the problem as to how best to format it. Can you help me?

The principle of the command is simple, it runs two processes in one command, using docker containers, using other alias command. Looking the command below:

docker exec -it database_one bash -c "find ./data -type d -name '202*-*-*' -exec mongorestore --drop {} \;" && docker exec -it database_one mongo logindb --eval 'db.users.update({"username":"admin"},{$set:{"password":"test", "reset_password": false}});'

This command is perfectly fine the way it is, but I have problems with the various internal calls by other commands, such as docker and find, when I try to create an alias for it. I have tried to use eval, among others for this but without success.

4
  • Please explain "without success". Do you get errors? Does something not work properly? What alias/eval commands have you tried, and what happened? (Incidentally, eval is almost never a suitable answer to anything.) Commented May 22, 2023 at 18:42
  • 3
    Don't try to make aliases of a command that already contains nested quotes, you'll just get caught in the quoting mess. Use a shell function instead, see: In Bash, when to alias, when to script and when to write a function? Also try not to just throw around shotguns like eval blindly just because something doesn't work, but instead look into seeing how to build it properly. That might keep the shotgun from going off in the wrong direction.
    – ilkkachu
    Commented May 22, 2023 at 18:53
  • Sorry I didn't express myself well, when I said that about not being successful. For my mistake is in the use of creating the command with several quotes, as you said above! For I could not find another way to execute these commands, in another way! That is why I said unsuccessful, because it is related to the fact that .bash_aliases, I could not understand the construction of the new command.
    – vic.py
    Commented May 22, 2023 at 19:07
  • We expect you to show what you tried. Then we can can offer specific corrections. But, as demonstrated by the comments and answers, functions are the best way to avoid the quoting hell of aliases. Commented May 23, 2023 at 12:36

1 Answer 1

4

as @ilkkachu said you can make a function and add it to your .zshrc or .bashrc file, it would look like this:

docker_command () {
        docker exec -it database_one bash -c "find ./data -type d -name '202*-*-*' -exec mongorestore --drop {} \;" && docker exec -it database_one mongo logindb --eval 'db.users.update({"username":"admin"},{$set:{"password":"test", "reset_password": false}});'
}

after that you can run the command docker_command to execute the function.

or you can make a shell script and save it to $HOME/.local/bin/docker_command and run it directly from your shell, but in this case you need to make sure that the directory you have put the command in is mentioned in the path variable. to check your path variable run echo $PATH.

after you've done one of those options, and you still want to make an "alias" you can add this to your rc file: alias d=docker_command and that will make you execute it with d.

3
  • 2
    If you were going to alias d as docker_command would it not be easier to create the function as d in the first place? Commented May 22, 2023 at 19:54
  • correct. just giving an example. Commented May 23, 2023 at 6:39
  • Thanks for the answer@white-hat-er it really worked, but with minor adjustments, and it is necessary to use the old way of defining functions in shellscript. In this case I had to declare with function docker_command { command_1; command_2;}.
    – vic.py
    Commented May 23, 2023 at 17:58

You must log in to answer this question.

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