简体   繁体   中英

how to invoke unix command in perl

i am trying to execute following unix command but its not getting executed

$array_of_tables= `dbsmp $srv_name`;
print "$array_of_tables\n";

please help me to find out list of tables in a data base through perl scripting.

Also i am trying to copy a file from a path to different path by using following command:-

copy(`cd /osp/slee/service/$srv_name/bin/exec/script.txt`,`cd /osp/local/home/linus/amit/scripts`);

but getting an error:-

Usage: copy(FROM, TO [, BUFFERSIZE])

please provide some solution Thanks

Use doublequotes instead of back ticks.

copy("/osp/slee/service/$srv_name/bin/exec/script.txt","/osp/local/home/linus/amit/scripts");

and remove the cd

In Perl, the preferable way to capture the output of a system (shell) command is the qx() operator. See http://perldoc.perl.org/perlop.html#Quote-Like-Operators .

$array_of_tables = qx(dbsmp $srv_name);
print("$array_of_tables\n");

Actually, backticks should also work, so the problem must lie with your dbsmp command. I'm not sure what that command is; you'll have to provide more information about the utility and what error you're seeing.

For comparison, I can retrieve the list of tables in my local postgres database as a pipe-separated table using this shell command:

> psql -tAXq main postgres <<<\\d;

And this can be run from Perl as follows:

> perl -e 'print(qx(psql -tAXq main postgres <<<\\\\d;));'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM