{

How to call Shell Script from Perl code example


How to execute Shell Script or command from Perl  code example  Perl with code example Multiple ways to call a shell script or a command from Perl code.

  • Using System function with command and arguments, example system(“sh test.sh”,), control returned to called code with output and errors.
  • using the exec function, It breaks the execution once the command executes.

How to call shell commands or scripts from Perl with code examples

  • using the System function

The System function executes the command or scripts and returns the control to the next line of execution Syntax:

system(commands,arguments)

Commands are shell commands.

arguments are parameters passed to commands

For example, a System command executes shell commands, and It executes the command and control returned to the next line. And it prints the string

system("ls -l"); ## or you can use system("ls" , "-l");

print "End\n";

Output:

total 4
-rw-rw-r-- 1 sbx_user1051 990 66 Dec 20 14:21 main.plx
-rw-rw-r-- 1 sbx_user1051 990  0 Dec 20 14:21 stdin
End

TO execute the shell script file from a Perl code

system("sh","test.sh"); ## or you can use system("ls" , "-l");

print "End\n";

It executes the command sh test.sh and returns the output and control to the next line.

  • using exec function

exec function execute the command or scripts and does not return the control, stop its execution. Syntax:

exec(commands,arguments)

Commands are shell commands.

arguments are parameters passed to commands

For example, exec command to execute shell commands, It executes the command and stops its execution after the command executes, And it does not print the string

exec("ls -l"); ## or you can use exec("ls" , "-l");

print "End\n";

Output:

total 4
-rw-rw-r-- 1 sbx_user1051 990 66 Dec 20 14:21 main.plx
-rw-rw-r-- 1 sbx_user1051 990  0 Dec 20 14:21 stdin

TO execute the shell script file from a Perl code using the exec command

exec("sh","test.sh"); 

print "End\n";

It executes the command sh test.sh and control does not go to the next line.

Conclusion

Multiple ways to execute shell commands from Perl code, System executes scripts or commands and returns the control to the next line. exec function also do the same but not returned to next code statement

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.