473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to store value of system command in variable in linux ?

Hi All,

I have been using the Perl in window environment, but now i started using perl scripts in Linux flavor. I am new to Linux + perl combination.
I am trying to get the value of any system command executed in a variable, something like

1. my $DIR = system(pwd);
2. print "Current Dir: $DIR \n";

Here i don't get any output in line 2, where as line prints the current working directory.
I face the same problem when try to execute other commands using system(<command>). Please tell me the solution for this.

Your Help is highly appreciated,
Pawan Kumar
Jun 30 '11 #1

✓ answered by rovf

system does not return the output of the system command.

If you want to collect the standard output (not the standard error), you usually use the backticks operator:

my $dir=qx(pwd);

or, if you prefer, the equivalent

my $dir=`pwd`;

(Of course, for getting the working directory, you could easier do it without shelling out - see the module Cwd).

Note that backticks interpolate variables like double-quoted strings do!

2 5279
rovf
41
system does not return the output of the system command.

If you want to collect the standard output (not the standard error), you usually use the backticks operator:

my $dir=qx(pwd);

or, if you prefer, the equivalent

my $dir=`pwd`;

(Of course, for getting the working directory, you could easier do it without shelling out - see the module Cwd).

Note that backticks interpolate variables like double-quoted strings do!
Jun 30 '11 #2
Thanks rovf,

both the methods you mentioned, worked for me successfully.

Thanks,
Pawan
Jul 1 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: mkent | last post by:
I'm trying to use signal.alarm to stop a run-away os.system command. Can anyone exlain the following behavior? Given following the trivial program: import os import signal def...
4
by: shaggystyle | last post by:
I'm looking for a way to call system commands with variable parameters within a Unix environment. Sort of like system() but i need to be able to pass variable parameters....so I guess it would...
15
by: Sokar | last post by:
Hello, I am writing a script which is using the system() command. In the script the system command invokes an encryption with system("gpg -f file.txt"). The problem comes when the script is...
3
by: ram s via .NET 247 | last post by:
Hi On the click of a button on the main form i call 4 threads(addressof class files) which do som process on different tablesin the db. I have variables in the threads which store value ofthe...
2
by: summer00 | last post by:
Hi everyone, I found that the value of a variable(string type for example) is lost after the aspx page postback. E.G: private void Page_Load(object sender, System.EventArgs e) {
4
by: Rob | last post by:
Using VB.net and SQL server... I have a stored procedure that is simply returning a row count.... I know how to execute a stored procedure and add the output parameter. But how do I store...
1
by: darren kirby | last post by:
Hello all. I have a python script here which is just a wrapper for 2 or more system commands. I would estimate the program spends at least 95.5% of 'real' time running the system commands. ...
1
by: silverburgh.meryl | last post by:
Hi, I am writing a C++ program under linux. Can you please tell me how can I execute a System command in c++? Kind of like System.exec() in Java? Thank you.
2
by: rajeshjava | last post by:
I am working with xsl. i want to store a value in the variable. xml Source looks like the below one <?xml version="1.0" encoding="UTF-8" ?> <getTradingInstallmentHistoryResponse...
1
by: irvg | last post by:
I have a "for" loop to do something over a number of files in a directory. In particular I need to send a command line job that uses each file in thar directory as an input to node in a linux...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.