0

I have a perl program that accepts an argument (intent is to change directory "cd") without spawning a child process.

$FPATH/pcd.pl allows the user to choose among a number of possible directory candidates, etc.

The trick is to get to the desired directory without being left in a spawned process. The following is the invocation I can't get to work (ksh shell):

. $FPATH/pcd.pl $*|read dir . cd $dir

No matter what real directory is returned into "$dir" the ". cd $dir" never gets to the desired directory. Please advise. Thanks.

1 Answer 1

0

I don't quite understand your line of code there; perhaps it has been garbled.

My approach would be a bit different: First, create an alias that takes the output of a program and makes a cd to that:

alias mycd='cd $(select-dir)'

Then make the program print the menu to stderr and the choice to stdout, for example like this:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

my @entries = ('..', grep {-d} glob("*"));
for my $i (0..$#entries) {
        printf STDERR (" %2d: $entries[$i]/\n", $i);
}
$| = 1;
print STDERR ("cd to: ");
say($entries[<>]);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .