How to execute a shell, perl and python script in linux
Question: How to run or execute a shell script or a perl script or a python script in linux.
Answer: Shell script can be executed by using sh or bash or ./ file name. Perl script can be executed using perl file name or pl file name. Python script can be executed by using python file name or py file name.
In this post i am going to explain you how to run or execute a shell script, perl script and python script in linux. As a linux administrator you should know how to execute a shell, perl and python script in linux. Even the programmers who are working under unix / linux like operating system they should also aware of how to execute or run a shell script, perl script and python script in linux operating system.
Lets see how to run the scripts using an example.
How to execute a shell script in linux
We can execute an shell script in multiple ways. I will explain you all the methods in this blog.
Lets create a test shell script called shellscript.sh. Provide execute permission to shellscript.sh. If you are a root or super user you don’t need execute permission to run a shell script but if you are a local user you should have execute permission to run a shell script. Providing execute permission for shell script is an good practice.
#cat > shellscript.sh
echo “Most Powerful Shell Script”
Save the file using keyboard shortcut “ctrl+shift+d”. The first line of shell script “#!” is said to be magic shebang sequence. The “/bin/bash” tells that it is an bash shell script.
#chmod +x shellscript.sh
Let’s execute the shell script
Even though if it is a root user you cannot execute or run the shell script like “./shellscript.sh”
with out giving execute permission “chmod +x shellscript.sh”.
Cool that’s it we have learned how to run or execute a shell script in linux / unix like operating system. Lets see how to run perl script in linux / unix like operating system.
How to run a perl script in linux
Lets begin it with by creating a perl script and giving execute permission to it. Dont forget that perl script should be end with .pl extension. Find the perl script location in your linux box using “which perl” command.
#cat > perlscript.pl
print “Hello, World!\n”;
Save the file using keyboard shortcut “ctrl+shift+d”. The first line of shell script “#!” is said to be magic shebang sequence. The “/usr/bin/perl” tells that it is an perl script.
#chmod +x perlscript.pl
Run or execute the perl script using the below command
We are done with executing perl script. Lets learn how to run python script in linux.
How to run or execute python script in linux
It is also similar in execution like shell and perl script. Create a python script and save it with extention .py and give execute permission to it. Then find out the location of the python script using “which python”command .
#cat > pythonscripting.py
print “Hello, World!\n”
#chmod +x pythonscripting.py
#ls -l pythonscripting.py