24
Sharing of how I made my own command
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.
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.
- Writing in
~./bashrc
After writing a
.js
file or other kinds of files, you could run it withnode
or other runtime tools.In the
~./bashrc
, we could make use ofalias
to help us run those files in a much easier wayFor example to execute
node abcd.js
, we could make it with as simple as only one wordabcd
Here is how we could do it
alias abcd="node /var/local/abcd.js"
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
- 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 -------
By doing so, every user could use the
abcd
commandIf the
abcd.js
accepts 3 arguments, which you include something likelet [ 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 -------
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.
24