#!/bin/bash # $1 results directory # to run, e.g., # step1 /your/path/to/results # need one argument ! if [ $# == 1 ]; then # scripts directory d=$( dirname $( readlink -m $0 ) ) #results directory res=$( readlink -m $1 ) # loop through the cases in data for sample in $( ls ${res}/ );do echo $sample; done; else echo "error - need to supply an argument" fi
What would the best way to write this in perl be ? Is there anyway to avoid using opendir, readdir, and closedir? and to avoid having to use CWD to get the absolute path ( as readlink -m . does in bash, or a way to write the for loop without having to first read into an array?
. The following perl script does the same thing, but I want to streamline it, even if that means really obfuscating it, any suggestions?#!/usr/bin/perl -w use strict; use Cwd 'abs_path'; use Cwd; use File::Basename; if (defined ($ARGV[0])){ #scripts directory my $d= getcwd();; #results directory my $res= abs_path($ARGV[0]); # loop through the cases in data opendir( DIR, $res ) || die "Error in opening dir $res\n"; my @files = readdir(DIR); close(DIR); foreach my $sample (@files){ print $sample . "\n"; } } else {print "Need to supply argument\n"};
In reply to Most effective bash to perl for directory listing by ZWcarp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |