在Perl中如何依次运行两个命令:

huangapple go评论63阅读模式
英文:

How to run 2 commands one after other in perl

问题

I am trying to run 2 perl commands with sudo-like dzdo, but somehow i am not able to achieve the result. Can someone help me with it.

This is the sample of my requirement. I have 2 scripts named /home/user1/script1.pl and /home/user1/script2.pl

Here, I have the condition to run the second script once the first script is executed successfully. So i tried using below ways but still fails.

cmd="dzdo -iHu admin /home/user1/script1.pl && dzdo -iHu admin /home/user1/script2.pl";
cmd="dzdo -iHu admin \"/home/user1/script1.pl\" ; \"/home/user1/script2.pl\"";
cmd="dzdo -iHu admin /home/user1/script1.pl && \"dzdo -iHu admin /home/user1/script2.pl\"";
cmd="dzdo -iHu admin \"/home/user1/script1.pl ; /home/user1/script2.pl\"";
cmd="$cmd1 && $cmd2";
cmd="${cmd1} && ${cmd2}";

I am getting the error as "Unknown option: ihu" for all the above methods. I even tried with many combinations but end with same error. I think i am missing some basic declaration. If i mention something as below, it runs the second script but not the first.

cmd1="dzdo -iHu admin /home/user1/script1.pl";
cmd2="dzdo -iHu admin /home/user1/script2.pl";

Both the scripts are running as expected if I run the scripts manually outside the perl scripting.

user1@123abc.dev.com > dzdo -iHu admin /home/user1/script1.pl ; dzdo -iHu admin /home/user1/script2.pl
英文:

I am trying to run 2 perl commands with sudo-like dzdo, but somehow i am not able to achieve the result. Can someone help me with it.

This is the sample of my requirement. I have 2 scripts named /home/user1/script1.pl and /home/user1/script2.pl

Here, I have the condition to run the second script once the first script is executed successfully. So i tried using below ways but still fails.

cmd="dzdo -iHu admin /home/user1/script1.pl && dzdo -iHu admin /home/user1/script2.pl";
cmd="dzdo -iHu admin "/home/user1/script1.pl" ; "/home/user1/script2.pl"";
cmd="dzdo -iHu admin /home/user1/script1.pl" && "dzdo -iHu admin /home/user1/script2.pl";
cmd="dzdo -iHu admin "/home/user1/script1.pl ; /home/user1/script2.pl"";
cmd="$cmd1 && $cmd2";
cmd="${cmd1} && ${cmd2}";

I am getting the error as "Unknown option: ihu" for all the above methods. I even tried with many combinations but end with same error. I think i am missing some basic declaration. If i mention something as below, it runs the second script but not the first.

cmd1="dzdo -iHu admin /home/user1/script1.pl";
cmd2="dzdo -iHu admin /home/user1/script2.pl";

Both the scripts are running as expected if I run the scripts manually outside the perl scripting.

user1@123abc.dev.com > dzdo -iHu admin /home/user1/script1.pl ; dzdo -iHu admin /home/user1/script2.pl

答案1

得分: 2

首先,我不熟悉dzdo,并且找不到相关文档。

如果它的选项与sudo相同,那么-ihu并不太合理。你之前说的-iHu更合理。如果这不是问题的原因,我无法提供更多帮助。请查阅相关文档(man dzdo?)你可以尝试将选项分开传递(-i -H -u)。

如果它类似于sudo,它会接受一个程序和要传递给程序的参数。所以你想要的可以通过以下方式实现(一旦你解决了选项的问题):

dzdo -iHu admin /usr/bin/sh -c '/home/user1/script1.pl && /home/user1/script2.pl'
英文:

First of all, I'm not familiar with dzdo, and I can't find documentation for it.


> I am getting the error as "Unknown option: ihu"

If it's the same options as sudo, -ihu doesn't make much sense. -iHu, which you said you were using, would make more sense.

If that's not the issue, I can't help more with this part. Consult its documentation. (man dzdo?) You could try passing them separately (-i -H -u).


> I am trying to run 2 perl commands with sudo-like dzdo

If it's like sudo, it takes a program and arguments to pass to the program.

So what you want could be achieved using the following (once you get the issue with the options resolved):

dzdo -iHu admin /usr/bin/sh -c '/home/user1/script1.pl && /home/user1/script2.pl'

答案2

得分: -1

I separated both the scripts as CMD1 and CMD2 and executed both the scripts after declaring CMD1 and CMD2.

CMD1 = "dzdo -iHu admin /home/user1/script1.pl";
$CMD1
CMD2 = "dzdo -iHu admin /home/user1/script2.pl";
$CMD2
英文:

I separated both the scripts as CMD1 and CMD2 and executed both the scripts after declaring CMD1 and CMD2.

CMD1="dzdo -iHu admin /home/user1/script1.pl"; 
$CMD1
CMD2="dzdo -iHu admin /home/user1/script2.pl"; 
$CMD2

huangapple
  • 本文由 发表于 2023年7月6日 20:56:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定