Sponsored Content
Top Forums Shell Programming and Scripting Trouble executing piped shell commands in perl code Post 302629047 by sam05121988 on Tuesday 24th of April 2012 09:03:24 AM
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!
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
platform::shell(n)					       Tcl Bundled Packages						platform::shell(n)

__________________________________________________________________________________________________________________________________________________

NAME
platform::shell - System identification support code and utilities SYNOPSIS
package require platform::shell ?1.1.4? platform::shell::generic shell platform::shell::identify shell platform::shell::platform shell _________________________________________________________________ DESCRIPTION
The platform::shell package provides several utility commands useful for the identification of the architecture of a specific Tcl shell. This package allows the identification of the architecture of a specific Tcl shell different from the shell running the package. The only requirement is that the other shell (identified by its path), is actually executable on the current machine. While for most platform this means that the architecture of the interrogated shell is identical to the architecture of the running shell this is not generally true. A counter example are all platforms which have 32 and 64 bit variants and where a 64bit system is able to run 32bit code. For these running and interrogated shell may have different 32/64 bit settings and thus different identifiers. For applications like a code repository it is important to identify the architecture of the shell which will actually run the installed packages, versus the architecture of the shell running the repository software. COMMANDS
platform::shell::identify shell This command does the same identification as platform::identify, for the specified Tcl shell, in contrast to the running shell. platform::shell::generic shell This command does the same identification as platform::generic, for the specified Tcl shell, in contrast to the running shell. platform::shell::platform shell This command returns the contents of tcl_platform(platform) for the specified Tcl shell. KEYWORDS
operating system, cpu architecture, platform, architecture platform::shell 1.1.4 platform::shell(n)
All times are GMT -4. The time now is 10:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy