Problem using "system" command in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem using "system" command in perl
# 1  
Old 12-02-2011
Question Problem using "system" command in perl

Hello!!!

I'm trying to pass the output from bash command to perl variable in a perl script, and I used the "system" command to execute the bash statment and pass the result to perl string variable, in this perl script I used a variable $file that store data for using it as a regular expression.
Here I show you some part of the script:

Code:
$output = system "find /path/ | grep $file | sed 's/ /\ /g'";
system "evince $output";
print $output;

Output: in the screen the script prints 0

The result I want obtain is the path found by grep allocated in the perl variable $output.

the only I have obtained is a zero allocated in $output perl variable. I have tried the bash one-line script in linux terminal and it works very fine because it shows me the correct result I want.

I would appreciate if you can help me to solve this
# 2  
Old 12-02-2011
Quote:
Originally Posted by evolabo
...
Code:
$output = system "find /path/ | grep $file | sed 's/ /\ /g'";
system "evince $output";
print $output;

Output: in the screen the script prints 0

The result I want obtain is the path found by grep allocated in the perl variable $output.

the only I have obtained is a zero allocated in $output perl variable. ...
You have assigned the exit status of the Unix command to the Perl variable $output. Since the command was successful, its exit status was 0 and so it gets assigned to $output.

If you want to assign the output of the Unix command to a Perl variable, then use backticks (``) or qx// function.

Code:
$
$
$ # Run a command first and check its output
$
$ ls f1
f1
$
$ # Now run this command from within Perl, and using "system" function
$
$ perl -le 'system ("ls f1")'
f1
$
$ # Try to assign the command output to a Perl variable
$
$ perl -le '$x = system ("ls f1"); print "\$x = $x"'
f1
$x = 0
$
$
$ # The first line is the actual output; the second line is the value of $x
$ # which in turn is the exit status of the command
$
$ # Use backticks to assign the output in Perl
$
$ perl -le '$x = `ls f1`; print "\$x = $x"'
$x = f1
 
$
$
$ # You can also use qx// function
$
$ perl -le '$x = qx/ls f1/; print "\$x = $x"'
$x = f1
 
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 12-03-2011
Thank you a lot. This was really didactic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What does "force devmap reload" as in "multipath -r" means for my system and stability of my system?

Cannot present unpresented disks back again. On a test server tried this as a solution "multipath -r" and it worked. Too worried to try it in production before I know all the information. Any info would be appreciated! Also some links to the documentation on this specific issue could help a... (1 Reply)
Discussion started by: jsteppe
1 Replies

2. Shell Programming and Scripting

awk "date" and "system" command

Hello experts! I need your help please I have a file.txt of which I want to extract 3rd and 4th columns with date with the form e.g.: 2016-11-25 03:14:50and pass them to "date" command, but also append the 9th column in a file as well. So I want to execute date -d '2016-11-25 03:14:50' ... (2 Replies)
Discussion started by: phaethon
2 Replies

3. AIX

[Tip] Problem with rpm ("different operating system")

I have once experienced this problem without understanding what caused it but now learned thatn there is even a PMR dealing with it. Sometimes it happens that you encounter the following (rather cryptical) error message when trying to install an rpm-package: package <rpm_package_name> is for a... (1 Reply)
Discussion started by: bakunin
1 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Programming

Problem with system("command")

Sorry, I don't speak English very well but I will try to explain my problem! If I write a C++ program and I use che system call system("command") I have this problem: If I would launch a program I do this: system("~/Agostino/program/test") and all work fine!! But if I divide this... (9 Replies)
Discussion started by: acciues
9 Replies

8. Programming

Problem with socket binding - "system" call

Hi, I am having an issue with using sockets. I have a program which binds to a socket and listen on it. Later I spawn a thread to handle some function. In the new thread created I need to call a shell script which executes the specified function. Here I am using a system command to call the... (5 Replies)
Discussion started by: Janardhanbr
5 Replies

9. UNIX for Dummies Questions & Answers

Unix "at" / "Cron" Command New Problem...Need help

Hi All, I am trying to schedule a one time job using the at command with the help of shell script for my project. The shell script should take a parameter as a command line argument from the at command itself. Is it possible to take a command line parameter for a shell script in the command... (3 Replies)
Discussion started by: Mohanraj
3 Replies

10. Shell Programming and Scripting

perl "system" cmd return values..

perl 5.6.1: when i try a "system" command(with if loops for $?), i get this: child exited with value 1 what is meant by this $? values and what does it meant if it returns 1?.. (0 Replies)
Discussion started by: sekar sundaram
0 Replies
Login or Register to Ask a Question