简体   繁体   English

用于unix命令的Perl用户输入

[英]Perl user input for unix command

Can someone please help me out with what I am doing wrong here, been scratching my head for a bit. 有人可以帮助我解决我在这里做错了什么,一直在摸不着头脑。 I don't know much perl just making a small change to a irssi script. 我只是对irssi脚本做了一些小改动,我不太了解perl。

  $selectedname = <STDIN>; $Nameslist = `grep -i -w '$selectedname'~/file`; 

It is working correctly the issue I am having is it looks like the variable isn't being inserted correctly. 它正常工作我遇到的问题是看起来变量未正确插入。 For example if I type in the same command using the word test in the command line I get 3 lines that show up. 例如,如果我在命令行中使用单词test键入相同的命令,则会显示3行。 But if I use the same word with this script it comes up empty. 但是,如果我在这个脚本中使用相同的单词,那么它就会变空。

I know it is working correctly because I can do * as an input and it will show me the whole file. 我知道它工作正常,因为我可以做*作为输入,它会显示整个文件。 It is just not working with the inputed strings. 它只是不使用输入的字符串。

Any help would be much appreciated this has really been stumping me. 任何帮助将非常感谢,这真的让我很难过。

Actually when you are using <STDIN> , a new line character is coming. 实际上当你使用<STDIN> ,会出现一个新的行字符。 so you need to remove the new line character. 所以你需要删除换行符。 you can use chomp to remove the new line character 你可以使用chomp删除换行符

Code: 码:

   $selectedname = <STDIN>;
   chomp($selectedname);
   $Nameslist = `grep -i -w '$selectedname'~/file`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM