Login Register






Create custom command line commands for scripts filter_list
Author
Message
Create custom command line commands for scripts #1
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/customscripts

Step 2: Copy the script(s) to our newly created folder

Code:
$ cp /path/to/script/filename /opt/customscripts/filename

Step 3: Make the file(s) executable

Code:
$ chmod +x /opt/customscripts/filename

When 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/bin

Step 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 command

Step 3: Verify the command / symlink

Code:
$ ls -l | grep command

If 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 splitfile

I 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 chunksize
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Create custom command line commands for scripts #2
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 PATH
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Create custom command line commands for scripts #3
(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.

Just put this to your .bash_profile
Code:
PATH=$PATH:/opt/customscripts export PATH

Thanks for the reply @Deque. Didn't know about that method Smile Will do that on my system for that folder, and use the method from the tutorial on those not living in the customscripts folder Smile

Update: That will only work for a single user correct?
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Create custom command line commands for scripts #4
(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
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Create custom command line commands for scripts #5
@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 Smile
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Create custom command line commands for scripts #6
(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 Smile

.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_profile
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: Create custom command line commands for scripts #7
Windows users might want to know that for them it's as easy as copying your executable file in the System32 directory.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: Create custom command line commands for scripts #8
(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 Smile Thanks for your reply
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply