Side Project: Wrote A Node Program That Can Write A Bash Script To Your Computer (Mac/Linux maybe Windows???)

Jimmy McBride - Jan 21 '20 - - Dev Community

I don't know how well it will work on Windows, so if you're on a windows machine, give it a try and let me know if it works or not! 🔥

Check out my GitHub repo to test it out!

What the program does:

You're most likely going to be in the repo's dir when you node index.js to run the program, so it starts by changing into your home directory. Once inside the home directory, it checks to see if you have a bin folder. If so, it moves on; if not, it makes on for you.

Then it checks your path variable, if $HOME/bin is not in your path then it exports $HOME/bin to your path variable from your .bashrc file. If you don't have a .bashrc file, then one is created. If you do have one, it's appended to the end of your file.

Once both of those checks are finished, then it changes to your (potentially new) home/bin directory and creates a file named hello, adds text for a simple greeting to be echoed on command and changes the permissions of the file to 755. And just like that, I was able to write a program to write a bash script on your computer.

Now it's your turn!

Start writing more bash scripts on your machine! Everything is already set for you now. All you have to do is create a file in your home/bin, write the script (don't forget the shebang!) and chmod 755 file_name and your good to go!

I hope you enjoy making your work environment more productive like I do! If you want to read about the basics of writing your own bash scripts, check out my blog, Write A Bash Script 101. For a slightly more in-depth look at different tools that you can use in your bash scripts, check out my blog, Bash Script Tool Kit.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .