Trouble executing piped shell commands in perl code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble executing piped shell commands in perl code
# 1  
Old 04-24-2012
Lightbulb Trouble executing piped shell commands in perl code

I am trying to execute a piped combination of shell commands inside a perl program.
However, it is not working as desired.

This is my program, i am trying to print only filenames from the output of ls -l

Code:
$ cat list_test
#!/usr/bin/perl -w
use strict;
my $count=0;
my @list=`ls -l|awk '{print $9}'`;
print "@list";

it produces output as

Code:
$ ./list_test
Use of uninitialized value in concatenation (.) or string at ./list_test line 5.
total 104
 -rwxr-xr-x 1 sam ugroup  59 Mar 28 08:13 ch2_p1
 -rwxr-xr-x 1 sam ugroup 127 Mar 28 08:18 ch2_p2
 -rwxr-xr-x 1 sam ugroup 202 Mar 28 08:21 ch2_p3
 -rwxr-xr-x 1 sam ugroup 175 Mar 28 08:28 ch2_p4
 -rwxr-xr-x 1 sam ugroup 170 Mar 28 09:52 ch2_p5
 -rwxr-xr-x 1 sam ugroup  86 Apr 10 09:20 ch3_p1
 -rwxr-xr-x 1 sam ugroup 193 Apr 10 09:34 ch3_p2
 -rwxr-xr-x 1 sam ugroup 232 Apr 10 10:16 ch3_p3
 -rwxr-xr-x 1 sam ugroup 334 Apr 17 03:16 ch4_p1
 -rwxr-xr-x 1 sam ugroup 142 Apr 17 03:20 ch4_p2
 -rwxr-xr-x 1 sam ugroup 418 Apr 17 03:34 ch4_p3
 -rwxr-xr-x 1 sam ugroup  38 Apr 18 02:29 ch5_p1
 -rwxr-xr-x 1 sam ugroup 180 Apr 18 02:49 ch5_p2
 -rwxr-xr-x 1 sam ugroup 259 Apr 18 02:53 ch5_p3
 -rwxr-xr-x 1 sam ugroup 308 Apr 20 00:46 ch6_p1
 -rwxr-xr-x 1 sam ugroup 261 Apr 20 00:56 ch6_p2
 -rwxr-xr-x 1 sam ugroup 220 Apr 20 01:17 ch6_p3
 -rwxr-xr-x 1 sam ugroup  77 Apr 20 03:35 ch7_p1
 -rwxr-xr-x 1 sam ugroup  81 Apr 20 03:36 ch7_p2
 -rwxr-xr-x 1 sam ugroup  75 Apr 20 03:36 ch7_p3
 -rwxr-xr-x 1 sam ugroup  84 Apr 20 04:42 ch7_p4
 -rwxr-xr-x 1 sam ugroup  82 Apr 20 04:42 ch7_p5
 -rwxr-xr-x 1 sam ugroup  92 Apr 20 04:45 ch7_p6
 -rw-rw-rw- 1 sam ugroup 218 Apr 20 04:46 ch7_sample1
 -rwxr-xr-x 1 sam ugroup  82 Apr 23 05:33 list.pl
 -rwxr-xr-x 1 sam ugroup 155 Apr 24 08:52 list_test

however, when i execute those commands on command line it executes perfectly(this is how the output should be of the perl code)

Code:
$ ls -l | awk '{print $9}'
ch2_p1
ch2_p2
ch2_p3
ch2_p4
ch2_p5
ch3_p1
ch3_p2
ch3_p3
ch4_p1
ch4_p2
ch4_p3
ch5_p1
ch5_p2
ch5_p3
ch6_p1
ch6_p2
ch6_p3
ch7_p1
ch7_p2
ch7_p3
ch7_p4
ch7_p5
ch7_p6
ch7_sample1
list.pl
list_test

please help!
# 2  
Old 04-24-2012
Use -1 switch of ls instead of awk

Code:
 
#!/usr/bin/perl -w
use strict;
my $count=0;
my @list=`ls -1`;
print "@list";

This User Gave Thanks to asterisk-ix_use For This Post:
# 3  
Old 04-24-2012
That worked thanks,
but can you explain why awk didn't work inside the code
# 4  
Old 04-24-2012
Just like shell, perl also uses the positional parameters $1,$2 etc. So, when you are using $9 in the awk command syntax, perl interprets $9 as one of the other variables in the perl script. Hence the error.

If you still want to use awk command, escape the $ sign like below:

Code:
 
#!/usr/bin/perl
use strict;
my $count=0;
my @list=`ls -l|awk '{print \$9}'`;
print "@list";

# 5  
Old 04-24-2012
If your perl code is nothing but a thin membrane atop 90% shell scripting, it might be time to learn shell, as well. I used to write enormous perl scripts before realizing, if I removed the perl, I'd be left with something simpler and more functional.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Executing MQ commands in Perl

If you want to capture the output of any command, we then will be writing the system command in `` or qx. `` an qx works fine with all linux and windows system commands. But when I execute the below code.. it is displaying the output on the screen directly instead of storing to variable ... (3 Replies)
Discussion started by: giridhar276
3 Replies

2. Shell Programming and Scripting

Executing multiple ssh commands inside a shell simultaneously

I would like to execute a commands in four different servers through ssh at a single instance(simultaneously). Below are the details with examples, ssh user1@server1 "grep xxxx logs" ssh user1@server2 "grep xxxx logs" ssh user1@server3 "grep xxxx logs" Each statement will take some... (4 Replies)
Discussion started by: Amutha
4 Replies

3. Shell Programming and Scripting

Commands not executing after FTP in shell script

Hello In the shell script we have a FTP command like below ftp -n -v -q winftp.principal.com >/infa/datafiles/GRP/Scripts/ftp_from_infa_dvcn.log<<END_SCRIPT   quote USER $FTP_USER quote PASS $FTP_PASS ascii lcd $FTP_LOCALDIR cd $FTP_FLDR put $FTP_FILE   bye exit If i... (1 Reply)
Discussion started by: Pratik4891
1 Replies

4. Homework & Coursework Questions

What are different ways of executing shell commands?

I am a student in BCIT in vancouver and taking comp2771 course.(Shell scripting). I would like to know what are different ways of executing shell commands? Thanks (1 Reply)
Discussion started by: adam25ca
1 Replies

5. Shell Programming and Scripting

Executing a shell script containing awk commands

Hi All, I am trying to execute a shell script containg awk commands. But unable to do so. Below is my script. Please help. The name of the script is scan.sh and I have tried executing it using the command sh scan.sh It is giving an error which reads like: awk:syntax error near line 7... (3 Replies)
Discussion started by: misb
3 Replies

6. UNIX for Dummies Questions & Answers

How to run multiple piped commands in a find -exec statement?

I can't get this to work. Running a single command works fine: find . -name "*.dat" -exec wc -l '{}' \; gives me the file name and number of lines in each .dat file in the directory. But what if I want to pipe commands, e.g. to grep something and get the number of lines with that pattern... (3 Replies)
Discussion started by: DJR
3 Replies

7. UNIX for Dummies Questions & Answers

how to stay in remote shell after executing commands in ssh?

the ssh calling convention: ssh <server> If I put commands in the section, ssh will execute them immediately after logging in and return to local shell. I want to stay in the remote shell after executing these commands. How can I achieve this? Thanks for all. (1 Reply)
Discussion started by: hplonlien
1 Replies

8. Shell Programming and Scripting

KSH variable containing piped commands

Hi, I am trying to execute a bunch of piped command which are stored in a variable, but only one command executed at a time, and rest of the pipes, commands and their arguments are considered as argument to the very first command. Can you please help me in this? bash-2.05$ cat test.sh... (1 Reply)
Discussion started by: prashant.ladha
1 Replies

9. Shell Programming and Scripting

Executing commands through shell variable

Hey there, I am trying to pass a command as argument to a function. The command shows up in $1. Now I want to execute this command, but if I do a $1 ./sample "bla/blaprintf: warning: ignoring excess arguments, starting with `bla/bla' The code is : #!/bin/ksh fn() { $1 } fn... (3 Replies)
Discussion started by: shriyer
3 Replies

10. UNIX for Advanced & Expert Users

executing commands in child shell

I have to execute some commands after executing one command ( cleartool setview Tagname) Problem is that I write commands in script like this. echo "test1" cleartool setview tagname echo "test2" copy file1 file2 echo "test3" but when I execute script. Output --------- test1 If I... (1 Reply)
Discussion started by: udaykishore
1 Replies
Login or Register to Ask a Question