How can I call a shell function from a Perl script?
You'll need to call that function in the same shell that sources utils.sh
, so:
my $dir_name = `source utils.sh; getDir`;
chomp($dir_name);
print $dir_name, "\n";
How can I call a shell command in my Perl script?
How to run a shell script from a Perl program
1. Using system
system($command, @arguments);
For example:
system("sh", "script.sh", "--help" );
system("sh script.sh --help");
System will execute the $command with
@arguments and return to your script when finished. You may check $!
for certain errors passed to the OS by the external application. Read
the documentation for system for the nuances of how various
invocations are slightly different.2. Using
exec
This is very similar to the use of system, but it will
terminate your script upon execution. Again, read the documentation
for exec for more.3. Using backticks or
qx//
my $output = `script.sh --option`;
my $output = qx/script.sh --option/;
The backtick operator and it's equivalent
qx//
, excute the command and options inside the operator and return that commands output to STDOUT when it finishes.There are also ways to run external applications through creative use of open, but this is advanced use; read the documentation for more.
Source and run shell function within perl regex
The answer was to scrap this whole idea and use a better one..
Lets step back first.. Big Picture:
Goal was to make the script
program output an executable shell script of the entire recorded session.
Back to Answers..
The above implementation was supposed to remove all prompts and control characters from the output of script
(which is the input examples I gave) and then remove the output of each command (i.e. any line that didn't contain control characters).
Passing the evalPS
function to perl
to execute proved to be quite redundant and getting bash
and perl
to expand the parameters correctly was a nightmare..
The Final Solution
Scrapped the perl regex idea and used a combination of subshell and history redirection to grab the commands for the entire script
session, while it was running.
The entire implementation looks like this:
# log cmds to script file as they are entered (unbuffered)
# spawn script cmd in subshell and wait for it to finish
wait -n
(
history -c
export HISTFILE="${SCRIPT_FILE}"
shopt -s histappend
script -q --timing="${TIME_FILE}" "${REC_FILE}"
history -a
)
...
Simple and much easier to read! :)
Hope this helps anyone trying to make their own mods to script
in the future, cheers!
Is it possible to call a function within qx?
my $result = qx(some-shell-command @{[ get_value() ]});
# or dereferencing single scalar value
# (last one from get_value if it returns more than one)
my $result = qx(some-shell-command ${ \get_value() });
but I would rather use your first option.
Explanation: perl arrays interpolate inside ""
, qx()
, etc.
Above is array reference []
holding result of function, being dereferenced by @{}
, and interpolated inside qx()
.
Related Topics
Does Linking an '-Lpthread' Changes Application Behaviour? (Linux, Glibc)
Sub-Shell Differences Between Bash and Ksh
What Scheduling Algorithms Does Linux Kernel Use
Cpu Affinity Masks (Putting Threads on Different Cpus)
How to Install a Rpm Package and Its Dependencies Offline
Bash - Surround All Array Elements or Arguments with Quotes
Arm Inline Asm: Exit System Call with Value Read from Memory
Create a Virtual Floppy Image Without Mount
Find String Inside a Gzipped File in a Folder
How to Change the Mime Type of a File from the Terminal
Linux Command Output as a Parameter of Another Command
How to Use Awk to Convert All the Lower-Case Letters into Upper-Case
Force Cmake to Use the Full Library Path
Comparing Variables with Strings Bash
How to Execve a Process, Retaining Capabilities in Spite of Missing Filesystem-Based Capabilities