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





Messages In This Thread
Create custom command line commands for scripts - by RogueCoder - 05-20-2013, 02:59 PM