how to invoke unix command in perl - 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;));'

Related

trying to locate output file from postgresql \o -o command

I am making an automated script from terminal that creates a file with the output of \l
But I do not know where the \o command in postgresql prints out the file that it has made. The documentation doesn't inform where.
I did read this, but no luck:
Sincerely
\o points at the named file in current working directory of psql. As you found out, this has some issues in automated environments.
This means you have basically two options:
use absolute paths with \o
Alternatively you can use \cd to set your current working directory inside your psql script.
In your particular case, however, you know that psql -l gives you the same info? That may be easier to manage in an automated environment.

How to run scripts in mongoDB

I have the following scripts, which can be run in Sublime text built-in build.
I want to know how to run the sample script in command line
like mongo -f sample_script.js, thanks
sample_script.js
use security_development;
Object.bsonsize(db.grouped.findOne({"_id":"00063acf1078671c6b90d46effad474b"}))
You don't need the -f. Move database selection to command-line argument, though (use and show helpers don't work when executed from a file)
mongo -d security_development sample_script.js

How to ssh as different user, change group, and run a script within Perl

I need to be able to run a script from within a script but first I need to ssh as a different user and then change my group.
I am currently doing the following inside my perl script:
`ssh <user>#<host> ; newgrp <group> ; /script/to/run.pl`
When running this command form the command line it doesn't seam to switch groups. I assume this is because it's changing to a new shell.
How do I get around this and get it to work?
Also, please note, I do not have sudo/root privelages.
The first semicolon is interpreted by the local shell. So the three commands are run on the same host. I think you want this
ssh <user>\#<host> "newgrp <grp>; /bin/run.pl"
salva, in his reply, answered my question:
sg $group -c '$cmd'
The reason the following command:
newgrp <int>
doesn't work is because it creates a new shell. At least that is my best guess. the "sg" command gets around this.
I have found the following to work (with ksh on hpux) :
ssh user#host "echo 'date;pwd;echo bozo;id' | newgrp nerds;"
which basically executes the commands as user:nerds :
I think OP wants to construct a string to execute from Perl, notice the backticks. Not sure but OP might have to use:
$s='ssh <user>#<host> ; newgrp <group> ; /script/to/run.pl'; # Normal single quotes not backticks
exec($s);
OP, there are different ways to execute shell functions from a Perl script. You used backticks. There is also exec($s) and system($s).

DB2 executing a Script in another Script

I am facing a problem in DB2. In my Oracle environment it was very easy for me to include multiple scripts in one master script, which were executed sequentially. e.g.:
Master.sql:
connect ....
#script1.sql
#script2.sql
Now I have to build up same logic in DB2 LUW. Is there a simple way to include multiple scripts in one master script? I would like to have one single db2 call from shell, which executes the master script and within all subscripts.
Regards
Jan
There is notrhing to stop you from creating a single file with multiple sql batches. In the Windows world, it would look like this:
Note: First you initialize the db2 command prompt.
db2cmd -c -w -i %1.bat
With as many of these as you want in the .bat file:
db2 -txf c:\Example\db2html.sql
In Linux, the db2clp is included in the shell once you load the db2profile ('. /home/db2inst1/sqllib/db2profile). In windows, you need to call db2cmd in order to use db2clp.
With a interactive db2clp, you cannot call db2 scripts via #scriptX, however, you can call them from the shell like
db2 -tvf script
However, if you use the CLP*Plus you can do almost everything you do in SQL*Plus. For more information: https://www.ibm.com/developerworks/community/blogs/IMSupport/entry/tech_tip_db2_s_new_clp_plus_utility?lang=en

DB2 procedure compilation from KSH script

I am writing a KSH script to accept a .sql file parameter followed by each schema that file is to be compiled in. The script assumes the .sql file is a DB2 9.7 procedure.
I believe I am stuck on the syntax of the db2 command for the termination character. We always use the at symbol ('#'), however the following snippet fails with error "DB21001E The option "-#" specified after the 'db2' command or in the DB2OPTIONS variable is incorrect." Any assistance would be greatly appreciated.
#!/bin/ksh
. $IBM_DB_DIR/db2profile
db2 connect to dwdev3
const_compil_string_suffix="-vtd# -f ../../stored_procedures/"
script_name="ETL.THING.sql"
db2 "$const_compil_string_suffix$script_name"
db2 terminate
I have confirmed that the resulting string command above the 'terminate' does work at linux prompt compiling the procedure as expected:
db2 -vtd# -f ../../stored_procedures/ETL.THING.sql
Thank you in advance.
Try losing the quotation marks:
db2 ${const_compil_string_suffix}${script_name}
I suspect the option string -vtd# might be the culprit.
When using single character options most unix command accept two types of options, those with and those without arguments.
Apparently the # character is being processed as an option argument, however the error message indicates it is seen as an option on its own.
Try separating the options, as in: -v -t -d#, reorder them. or remove one of the -t or -v options.
Beware though. I have no knowledge about the db2 cli. Experiment at your own risk.
Was the -t option added later?