When attempting to run a script, it can be rather inconvenient to have to locate the script, and run it time and time again. What if there were a way to run a script from anywhere. Well that's what we'll be doing today. This can be useful for any scripts you use frequently, it can save you time by removing the need to locate and execute.
In this step we'll be creating a script that we'll use as an example.
Here we set our interpreter path and and ask the user a simple question.
We need to save the script without an extension so we don't have to use the "python FILENAME.py" method.
We save our script as "ask" with no extensions in the Scripts folder on our Desktop.
To be able to run the script we need to give it execute permission...
Here we can see that the script does not have execute permissions, yet...
We run the above command to give our script execute permission, this will enable us to run the script without the "python FILENAME.py" method. Now if we check the permissions again we should see the name appear in green as it is now executable...
We can see in the about result that that script now has the needed permissions, on to the next step!
At the moment we need to be in the scripts directory in order to execute it. In order to execute it from anywhere we'll need to move it to the proper place.
We copy the script to the "/bin" directory. This is where commands for our system reside. If we look in the "/bin" directory, we should be able to see our script.
Now we should be able to run this script from anywhere by simply entering "ask" as a command.
Since we've created this new command, it's only fitting that we give it a test. In order to test it, we'll enter "ask" as a command from any directory...
We've run the new "ask" command from our home directory and it works! We've ran our script from a different directory! But we can't just leave it's question unanswered, let's answer it honestly and see if it agrees.
There we have it! Now we're able to run this script from any directory, effectively creating a new command. This works for any script you have, you can use this to do a multitude of things. You can make a bash script to execute a set of commands automatically from anywhere, or you can do what we did here today and create a script that actually has some use and execute it from any directory.
Let me know what you think! If you have any questions comment them down below and I'm sure they'll be answered!
Thank you for reading!
Want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.
Other worthwhile deals to check out: