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
How I made my own command ?
- Writing in
~./bashrc
After writing a
.jsfile or other kinds of files, you could run it withnodeor other runtime tools.In the
~./bashrc, we could make use ofaliasto 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 wordabcdHere is how we could do it
alias abcd="node /var/local/abcd.js"
The down side of writing
aliasin~./bashrcis that the script can only be used by the users whose~./bashrchas 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
abcdas 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
abcdcommandIf the
abcd.jsaccepts 3 arguments, which you include something likelet [ var1, var2, var 3] = process.argv.slice(2)in your.jsfile to get the arguments , you could further do this
# ------- vim abcd -------
#!bin/bash
node "/var/local/abcd.js" $1 $2 $3
# ------- vim abcd -------
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)
Typo at:
!bin/bash
Should be:
!/bin/bash