Doing var=`cmd1 | cmd2` in shell - sh

Hi I'm trying to execute a command containing a | and capturing the output in Bourne Shell but I can't get it to work.
Additionally, I'd like to capture this command's output in an array so I tried
#!/bin/sh
rsl=($(cmd))
But I get an error with the first parenthesis.
Thanks for your help.

The POSIX Bourne shell does not support arrays.
Consider using Bash, Ksh or another more advanced shell instead.

Related

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;));'

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).

Call a bash script in perl

I am trying to call a bash script in perl, by doing this -
my $which_mpi = "/sw/tools/tacc/builds/carter/site/salt_which_mpi";
$mpi_stack = system("$which_mpi",-n);
the problem is that I want the script to execute when it is called by system command in line
$mpi_stack = system("$which_mpi",-n);
but the problem with this is while assigning the path in $which_mpi it automatically executes the script. So instead of me getting this value
WHICH_MPI : /sw/tools/salt/builds/carter/altd/bin/site/salt_which_mpi
I am getting the path with the output of the bash script like
WHICH_MPI : /sw/tools/salt/builds/carter/altd/bin/site/salt_which_mpiopenmpi 1.6.1
where openmpi 1.6.1 is the output of my salt_which_mpi bash script.
You can use perl's backtick operator
`bash -c "$WHICH_MPI"`
take a look at this question:
another stackoverflow question

Would like to take a Perl variable and pass it to bash

I am trying to enter storage commands like symmask in Unix. I would like to build a script that would take a variable from Perl like standard input and then use that variable in a bash shell. I have been trying to do both in Perl but I can't run the storage command in Perl script. Unless I am just missing it.
You can run all external programs and commands from Perl with system, exec and the backtick-operator (`` and qx()).
Please refer to:
http://perldoc.perl.org/functions/system.html
http://perldoc.perl.org/perlop.html#%60STRING%60
http://perldoc.perl.org/functions/exec.html
If you want to, say, copy stuff to another server, you can use the backticks like this:
my $file = 'foo.csv';
`scp foo.csv someone#otherserver:dir/foo.csv`;

How I redirect python stdout to C stdin using pipelines in linux

I have a python script and I want to use the output of it to be the input of other C program. I want to use pipelines, sintax would be:
python_script.py | C_program
but I don't know how to redirect the pythons stdout to C stdin
Redirection is done by the shell. As long as python script writes to stdout and C_program reads from stdin it should work ok.
It seems that you doing all right.
For example, if I need to redirect scripts output to program input, I used such construction:
$ my_script.py | progr
with case if progr accepts arguments in you code.
If its not working, maybe your python is writing to standard error and not standard output. If this is the case you need to:
( python_script.py 2>&1 ) | C_program