Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl user input for unix command

Tags:

perl

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.

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

like image 967
NeonLines Avatar asked Jun 02 '16 19:06

NeonLines


1 Answers

Actually when you are using <STDIN>, a new line character is coming. so you need to remove the new line character. you can use chomp to remove the new line character

Code:

   $selectedname = <STDIN>;
   chomp($selectedname);
   $Nameslist = `grep -i -w '$selectedname'~/file`;
like image 94
Arijit Panda Avatar answered Nov 08 '22 15:11

Arijit Panda