Redian新闻
>
System Environment variables in Perl
avatar
System Environment variables in Perl# Unix - 噫吁兮,危乎高哉
j*c
1
I don't know if this is a properiate board to post this question, but here it
is:
In perl, one can always read the system environment variable from pre-defined
hash %ENV. If one runs a perl script from a shell, it will inherit all the
environment variables available in the shell, including the system variables
like ARCH, USER, HOSTNAME etc. However, my problem is that if I submit a
job(in perl) to a job manager(Condor as we use), I can't get these variables
in %ENV. I suspect that Condor use a s
avatar
D*e
2
you sould be able to specify the shell in the job script in most of the batch
managers.

it
pre-defined
different

【在 j*c 的大作中提到】
: I don't know if this is a properiate board to post this question, but here it
: is:
: In perl, one can always read the system environment variable from pre-defined
: hash %ENV. If one runs a perl script from a shell, it will inherit all the
: environment variables available in the shell, including the system variables
: like ARCH, USER, HOSTNAME etc. However, my problem is that if I submit a
: job(in perl) to a job manager(Condor as we use), I can't get these variables
: in %ENV. I suspect that Condor use a s

avatar
c*e
3
不知你需要什么特定的环境变量,
可不可以写一个 shell 的 wrapper,这样可以先 invoke 你想要的 shell,
然后在wrapper 中调用你的 perl script?

it
pre-defined
different

【在 j*c 的大作中提到】
: I don't know if this is a properiate board to post this question, but here it
: is:
: In perl, one can always read the system environment variable from pre-defined
: hash %ENV. If one runs a perl script from a shell, it will inherit all the
: environment variables available in the shell, including the system variables
: like ARCH, USER, HOSTNAME etc. However, my problem is that if I submit a
: job(in perl) to a job manager(Condor as we use), I can't get these variables
: in %ENV. I suspect that Condor use a s

avatar
j*c
4
The reason that I was writting a perl script is because I don't want to write
any shell script(actually I know very little about shell script). The specified
variables I want to know is "ARCH" or "OSTYPE" which always gives the type
of operation system (I need to invoke different binary and different parameter
files in my wrapper); and "USER", "HOSTNAME".
Thanks anyway, I will try to findout how to specify a shell in Condor.

【在 c*****e 的大作中提到】
: 不知你需要什么特定的环境变量,
: 可不可以写一个 shell 的 wrapper,这样可以先 invoke 你想要的 shell,
: 然后在wrapper 中调用你的 perl script?
:
: it
: pre-defined
: different

avatar
o*z
5
这些东东用uname -a之类的命令不能得到吗
user可以用whoami或者$
【在 j*c 的大作中提到】
: The reason that I was writting a perl script is because I don't want to write
: any shell script(actually I know very little about shell script). The specified
: variables I want to know is "ARCH" or "OSTYPE" which always gives the type
: of operation system (I need to invoke different binary and different parameter
: files in my wrapper); and "USER", "HOSTNAME".
: Thanks anyway, I will try to findout how to specify a shell in Condor.

avatar
j*c
6
The problem is now solved. Condor can be specified to inherit or not the shell
environment. Thanks everyone for your help.

【在 j*c 的大作中提到】
: The reason that I was writting a perl script is because I don't want to write
: any shell script(actually I know very little about shell script). The specified
: variables I want to know is "ARCH" or "OSTYPE" which always gives the type
: of operation system (I need to invoke different binary and different parameter
: files in my wrapper); and "USER", "HOSTNAME".
: Thanks anyway, I will try to findout how to specify a shell in Condor.

avatar
j*c
7
good idea. but how to redirect the output of "uname" or "whoami" to
a scalar variable in perl?

【在 o***z 的大作中提到】
: 这些东东用uname -a之类的命令不能得到吗
: user可以用whoami或者$
avatar
o*z
8
chomp($output=`uname -a`);

【在 j*c 的大作中提到】
: good idea. but how to redirect the output of "uname" or "whoami" to
: a scalar variable in perl?

avatar
j*c
9
how comes this would work?

【在 o***z 的大作中提到】
: chomp($output=`uname -a`);
avatar
b*s
10
it work, haha.
actually you can call any shell command and get the output
in a scalar variablie like this.
like $list = `ls -l`

【在 j*c 的大作中提到】
: how comes this would work?
avatar
j*c
11
do you mean in perl?
I don't think so.

【在 b****s 的大作中提到】
: it work, haha.
: actually you can call any shell command and get the output
: in a scalar variablie like this.
: like $list = `ls -l`

avatar
b*s
12
I've used it for a million times. It amost works for
every script language. why don't you try it by yourself?

【在 j*c 的大作中提到】
: do you mean in perl?
: I don't think so.

avatar
j*c
13
I didn't try million times, only couple times yesterday. and it doesn't work.
in Perl a system call can only be done by "exec","system" or "syscall"
functions.

【在 b****s 的大作中提到】
: I've used it for a million times. It amost works for
: every script language. why don't you try it by yourself?

avatar
o*z
14
i don't think exec or system can invoke a system call
`uname -a` can't either

【在 j*c 的大作中提到】
: I didn't try million times, only couple times yesterday. and it doesn't work.
: in Perl a system call can only be done by "exec","system" or "syscall"
: functions.

avatar
p*f
15
exec builtin -> exec(2)
system -> fork(2), exec(2) and waitpid(2)
qx or `` -> fork, exec, and waitpid
all of these will result in system calls, at least for *nix systems.

【在 o***z 的大作中提到】
: i don't think exec or system can invoke a system call
: `uname -a` can't either

avatar
o*z
16
but they don't invoke system calls like
syscall(&SYS_write, fileno(STDOUT), $s, length $s);

【在 p******f 的大作中提到】
: exec builtin -> exec(2)
: system -> fork(2), exec(2) and waitpid(2)
: qx or `` -> fork, exec, and waitpid
: all of these will result in system calls, at least for *nix systems.

avatar
w*n
17
It works for sure. the reason you can not do it successfully, I doubt
1. can you tell the difference between `` and ''
2, have you set your system path correctly.
BTW, be clear with the concept "system call", it is a special terminology
I don't think it means what you want it mean here ... and you make other
people confused.

【在 j*c 的大作中提到】
: I didn't try million times, only couple times yesterday. and it doesn't work.
: in Perl a system call can only be done by "exec","system" or "syscall"
: functions.

avatar
j*c
18
got it, I'm stupid. but I just find another complicated way to work out:
open(FH,'ls -l')

【在 w**n 的大作中提到】
: It works for sure. the reason you can not do it successfully, I doubt
: 1. can you tell the difference between `` and ''
: 2, have you set your system path correctly.
: BTW, be clear with the concept "system call", it is a special terminology
: I don't think it means what you want it mean here ... and you make other
: people confused.

avatar
c*e
19
"ls -l" shouldn't be working for sure, coz with '-l' the output will mess your
things up.
gotta be:
foreach my $file (`ls`){
#print "open FH, $file\n";
open (FH, $file) || die "can not open $file";
....... #your code goes here
.......
close (FH);
}

work.

【在 j*c 的大作中提到】
: got it, I'm stupid. but I just find another complicated way to work out:
: open(FH,'ls -l')

avatar
p*f
20
why not glob or readdir, why turn to external "ls"?

【在 c*****e 的大作中提到】
: "ls -l" shouldn't be working for sure, coz with '-l' the output will mess your
: things up.
: gotta be:
: foreach my $file (`ls`){
: #print "open FH, $file\n";
: open (FH, $file) || die "can not open $file";
: ....... #your code goes here
: .......
: close (FH);
: }

avatar
j*c
21
hehe, I was only giving an example of pipe in the output of an external command.

【在 p******f 的大作中提到】
: why not glob or readdir, why turn to external "ls"?
avatar
c*e
22
Good point!
Thanks for reminding.

your

【在 p******f 的大作中提到】
: why not glob or readdir, why turn to external "ls"?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。