英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论