![]() |
|
Create custom command line commands for scripts - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Computers (https://sinister.li/Forum-Computers) +--- Forum: Operating Systems (https://sinister.li/Forum-Operating-Systems) +--- Thread: Create custom command line commands for scripts (/Thread-Create-custom-command-line-commands-for-scripts) |
Create custom command line commands for scripts - RogueCoder - 05-20-2013 Source: My blog When writing your own or download scripts and you want to run them from the command line it can be a hassle to always have to be in the correct folder and write ./filename etc. It is much easier to run the scripts using the same method as the rest of the Linux commands. In this article I will show you how easy that can be. The original files can live anywhere really, but I like to keep them in the /opt folder and this is what I will be using in this tutorial Part 1 – Create a folder to house the custom scripts Step 1: Create a customscripts folder inside /opt Code: $ mkdir /opt/customscriptsStep 2: Copy the script(s) to our newly created folder Code: $ cp /path/to/script/filename /opt/customscripts/filenameStep 3: Make the file(s) executable Code: $ chmod +x /opt/customscripts/filenameWhen this has been done we need to create the command that we will use to trigger the script. To do this we need to add a symbolic link (symlink) to the /usr/bin folder Part 2 – Create the command Step 1: Change the current working directory to /usr/bin Code: $ cd /usr/binStep 2: Create the command / symlink The command is normally the same as the filename, unless something else is more suitable. For example if the executable file is named something like executable or execute it would make more sense to name it after the application name Code: $ ln -s /opt/customscripts/filename commandStep 3: Verify the command / symlink Code: $ ls -l | grep commandIf this show that the command is executable, i.e lrwxrwxrwx, you’re all good and you can now execute this script from anywhere in your system. To try it out, navigate to anywhere and try running the script using the command you just created in /usr/bin Example I have made a couple of python scripts, and one of them is named splitfile.py, you can find the code here. Here’s the approach to make it executable from anywhere in my system Code: $ mkdir /opt/customscripts
$ cp /path/to/splitfile.py /opt/customscripts
$ chmod +x /opt/customscripts/splitfile.py
$ cd /usr/bin
$ ln -s /opt/customscripts/splitfile.py splitfileI can now use the following approach to execute this script … Code: $ splitfile /path/to/file chunksize… instead of … Code: $ cd /path/to/splitfile.py
$ ./splitfile.py /path/to/textfile chunksize
or
$ python splitfile /path/to/textfile chunksizeRE: Create custom command line commands for scripts - Deque - 05-20-2013 If you have all of these scripts in one folder, why not just add the folder to the PATH variable? That would be less work than creating a symlink for every single file. Just put this to your .bash_profile Code: PATH=$PATH:/opt/customscripts
export PATHRE: Create custom command line commands for scripts - RogueCoder - 05-20-2013 (05-20-2013, 03:21 PM)Deque Wrote: If you have all of these scripts in one folder, why not just add the folder to the PATH variable? That would be less work than creating a symlink for every single file. Thanks for the reply @Deque. Didn't know about that method Will do that on my system for that folder, and use the method from the tutorial on those not living in the customscripts folder ![]() Update: That will only work for a single user correct? RE: Create custom command line commands for scripts - Deque - 05-20-2013 (05-20-2013, 03:29 PM)shp0ngl3 Wrote: Update: That will only work for a single user correct? Yes. If you want to set it for all users (except root), put these lines into /etc/profile RE: Create custom command line commands for scripts - RogueCoder - 05-20-2013 @Deque I tried it out, creating .bash_profile adding the lines you suggested. But it didn't really work out.. fter closing all open terminals and reopened one I echoed $PATH and /opt/customscripts was not present.. Haven't gotten around to google it yet so it's on my todo list
RE: Create custom command line commands for scripts - Deque - 05-20-2013 (05-20-2013, 06:48 PM)shp0ngl3 Wrote: @Deque I tried it out, creating .bash_profile adding the lines you suggested. But it didn't really work out.. fter closing all open terminals and reopened one I echoed $PATH and /opt/customscripts was not present.. Haven't gotten around to google it yet so it's on my todo list .bash_profile is only executed if you login. That's actually the only difference to .bashrc which is run everytime you start a non-login shell. Instead of a login you can also do: Code: source .bash_profileRE: Create custom command line commands for scripts - noize - 05-21-2013 Windows users might want to know that for them it's as easy as copying your executable file in the System32 directory. RE: Create custom command line commands for scripts - RogueCoder - 05-21-2013 (05-21-2013, 05:05 PM)noize Wrote: Windows users might want to know that for them it's as easy as copying your executable file in the System32 directory. Good info there @noize Thanks for your reply
|