DEV Community

Ekim
Ekim

Posted on

Sharing of how I made my own command

Weekly sharing

Hi everyone, I am Ekim, a fresh Bootcamp graduate and an IT helper (I don't dare to call myself a programmer yet). Every Friday, I will share some of the work that I've done over the last week in a bid to get feedbacks from you guys and record my journey to become a programmer.

Introduction

Last week was a hectic journey. I didn't have much time working on blog writing. So this blog wouldn't be as detailed as others I've written. Despite that, I would like to share with you some tips for making your own command on Ubuntu.

Previously

Multi-Master VRRP set-up

How I made my own command ?

  1. Writing in ~./bashrc
  • After writing a .js file or other kinds of files, you could run it with node or other runtime tools.

  • In the ~./bashrc, we could make use of alias to help us run those files in a much easier way

  • For example to execute node abcd.js, we could make it with as simple as only one word abcd

  • Here is how we could do it

       alias abcd="node /var/local/abcd.js"
Enter fullscreen mode Exit fullscreen mode
  • The down side of writing alias in ~./bashrc is that the script can only be used by the users whose ~./bashrc has those script.

  • If there are multiple accounts in the server, you could not make sure that everyone could use that shortcut

  1. Writing an executable at /usr/local/bin
  • An executable is a file which is set to be executed

  • In that file, you specify how the script is run

  • Using the above abcd as an example, what you need is the following

       cd /usr/local/bin
       touch abcd
       chmod a+x ./abcd         # enable execution for all groups

       # edit the executable
       vim abcd

       # ------- vim abcd -------
       #!bin/bash

       node "/var/local/abcd.js"
       # ------- vim abcd -------
Enter fullscreen mode Exit fullscreen mode
  • By doing so, every user could use the abcd command

  • If the abcd.js accepts 3 arguments, which you include something like let [ var1, var2, var 3] = process.argv.slice(2) in your .js file to get the arguments , you could further do this

       # ------- vim abcd -------
       #!bin/bash

       node "/var/local/abcd.js" $1 $2 $3
       # ------- vim abcd -------
Enter fullscreen mode Exit fullscreen mode

Conclusion

Now, you have created a command called abcd in your system. Input your newly created command into your console and see if it could run as expected. If I did anything wrong, please let me know as well. That's all for today. I hope you enjoy reading.

Top comments (1)

Collapse
 
fq211776 profile image
FQ211776

Typo at:

!bin/bash

Should be:

!/bin/bash