In this article i will tell you "How to run shell script from any location like an Unix command" or "How to run scripts without typing the full path"
As we know that we can run unix command at unix terminal from any location and we dont bother where this command script is located into system.
But when we write our own script we have to give a proper path to the script or we need to go to the script location to run it.
For example if we have a test script demo under /home/mukesh/Desktop , then if you want to run script you need to change directory to script path /home/mukesh/Desktop or you need to give a complete path /home/mukesh/Desktop/demo
Let us see how can run demo script from anywhere or from any location on linux/unix terminal.
Consider a sample script name "demo" as at following location and content.
mukesh@ubuntu:~/Desktop$ pwd
/home/mukesh/Desktop
/home/mukesh/Desktop
mukesh@ubuntu:~/Desktop$ cat demo
#!/bin/bash
echo "Hello, this is demo script"
#!/bin/bash
echo "Hello, this is demo script"
mukesh@ubuntu:~/Desktop$ ./demo
Hello, this is demo script
Hello, this is demo script
Following are the steps to make your script like an Unix command.
1). Create a directory bin under your home directory.
2). Place your shell script file in this bin directory.
3). Update the PATH variable.
PATH=$PATH:~mukesh/bin (where: mukesh is my username, you can keep your own username)
Once done move the script "demo" to your "/home/mukesh/bin" directory and then you would be able to run script from any where . Lets try to run demo script from Documents directory /home/mukesh/Documents
mukesh@ubuntu:~/Documents$ demo
Hello, this is demo script
Like wise you can keep any number of script in bin directory "/home/mukesh/bin" and able to run like an unix command.Hello, this is demo script
Note:
You can create any other directory name also other than "bin" but in that case you may need to make an entry in .bashrc file.
Consider if you want to create a directory "testDir" in place of "bin" then just vi the .bashrc file and place the following line at the end of file and save.
export PATH="/home/mukesh/testDir:$PATH
No comments:
Post a Comment