Bash scripts. Create and run in WebStorm

Artem Diashkin
Apr 4, 2020 · 3 min read

As you know, we often use different CLI commands and amount of them could be huge.

  1. How to remember all of them? You could write them down and copy-paste every time.
  2. How to customize them? Copy-paste again?
  3. What if you want to be sure that you and another developer from your team knew exactly what will be executed?
  4. What if you want to ask a user a question before executing some script?
  5. What if you want to pass different parameters to the script?

I will show you how to write your own bash script using WebStorm that will solve all those issues.

As an example, I’ll be using AdonisJs project, so we could run scripts like:

ENV_PATH=.env.dev adonis migration:status

but not without copy-pasting it every time. Besides, there are six commands for using migrations (Migration commands. Official documentation) + there could be three or more environments in our project.

So let’s see how our run scripts will look like in WebStorm?

And let’s see how the execution of status-testing will look like…

Use shortcut ⌃+⌥+R (Control+Option+R) to open the “run” menu:

Or just select your script from the top menu bar:

Result:

Type “yes” in the console:

Source code of this script (I will explain it step-by-step futher):

Now let’s discuss what is happening

Lines 5 and 6

env_param=$1
command_param=$2

Set first parameter, passed to the script, to env_param, and the second parameter to the command_param

Line 8

We are creating a function that will validate the first passed parameter. If the first parameter equals to one of the commands CONSTANT, that are declared in ./constants.sh (it is attached to the end of this story), it will set value 1, that equals to true, to the newly created global variable is_first_valid_result that will be reused at the line 36.

Line 19

Create function that will validate the second parameter.

Line 49

We are waiting for user to enter text in the terminal and set it to choice variable.

If everything is “OK” → on line 44 we are executing a command like this:

ENV_PATH=.env.dev adonis migration:status

…but from the parent directory, using command cd ../..

When we expect some amount of argument passed to function run_command , we can reuse those arguments like this:

Constants file

After knowing how to create your own script you could make your software development much easier in the future!

LITSLINK Frontend Development

Your go-to place for frontend dev trends, know-hows, hacks and tricks.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK Frontend Development

Frontend development is key to user’s happiness & satisfaction. Read how Litslink’s engineering team is solving the hardest FE challenges for the most admirable startups in 2021.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK Frontend Development

Frontend development is key to user’s happiness & satisfaction. Read how Litslink’s engineering team is solving the hardest FE challenges for the most admirable startups in 2021.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store