Hi folks,

I'm prepared to write a simple perl scripts as follow

There are 25 commands executed in following sequence

export LFS=/mnt/lfs
mount /dev/hda6 /mnt/lfs
mkdir -p $LFS
echo $LFS
/mnt/lfs (output)
/usr/sbin/chroot "$LFS" /tools/bin/env -i HOME=/root TERM="$TERM" PS1='\u:\w\$ '     PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin /tools/bin/bash --login +h
mknod -m 600 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3
mount -n -t tmpfs none /dev
mknod -m 622 /dev/console c 5 1
mknod -m 666 /dev/null c 1 3
mknod -m 666 /dev/zero c 1 5
mknod -m 666 /dev/ptmx c 5 2
mknod -m 666 /dev/tty c 5 0
mknod -m 444 /dev/random c 1 8
mknod -m 444 /dev/urandom c 1 9
chown root:tty /dev/{console,ptmx,tty}
ln -s /proc/self/fd /dev/fd
ln -s /proc/self/fd/0 /dev/stdin
ln -s /proc/self/fd/1 /dev/stdout
ln -s /proc/self/fd/2 /dev/stderr
ln -s /proc/kcore /dev/core
mkdir /dev/pts
mkdir /dev/shm
mount -t devpts -o gid=4,mode=620 none /dev/pts
mount -t tmpfs none /dev/shm

In normal circumstance there is no output on all commands except command-4 with "/mnt/lfs" as output. If not considering the output it will be quite simple just listing all commands on a shell script. Now I expect to arrange the script as follows;

1) The script will automatically continue to execute command-5 if the output of command-4 is "/mnt/lfs" otherwise exit the shell.
2) If there is an output on any command it needs to confirm "yes/no" with the corresponding command displayed on the terminal. 'yes" will continue on next command. "no" will exit the shell stopping the script.

I'm considering whether shell script or perl script will do the job better. If perl script then how to start. Which module/modules will be more appropriate, TIA

B.R.
satimis

Dani AI

Generated

— Either a short shell loop or a small Perl driver will do this, but Perl makes it easier to capture stdout/stderr reliably and to implement prompts, logging and options (dry-run / auto-yes). Key points for your sequence: many of those steps (mount, device-node creation, chroot) normally produce no stdout but will return non‑zero exit codes on failure, so check both output and exit status. Also run the script as root in a safe environment (VM or snapshot) because the commands are destructive.

Recommended approach in Perl

  • Use a capture helper to grab stdout/stderr (Capture::Tiny is lightweight and reliable).
  • Keep commands in an array or external file and iterate with an index so you can treat the 4th command specially. (Note: Perl arrays are 0-based, so command 4 is index 3.)
  • After running a command, check: exit code, stdout, stderr. If index==3, compare the captured stdout to the expected value and abort if it does not match. If there is any output or a non-zero exit code, show it and prompt for yes/no before continuing.

Example (replace the placeholder commands and expected string with your real values):

use strict;
use warnings;
use Capture::Tiny qw(capture);

my @cmds = ('command_one', 'command_two', 'command_three', 'command_four_to_check', 'command_five');
my $expected_for_fourth = '/path/to/expected';  # change this
for my $i (0..$#cmds) {
    my $cmd = $cmds[$i];
    print "Running: $cmd\n";
    my ($out, $err) = capture { system($cmd) };
    my $exit = $? >> 8;
    chomp($out); chomp($err);
    if ($i == 3 && $out ne $expected_for_fourth) {
        die "Fourth command produced '$out' (expected '$expected_for_fourth') — aborting.\n";
    }
    if ($out ne '' || $err ne '' || $exit != 0) {
        print "STDOUT:\n$out\n" if $out;
        print "STDERR:\n$err\n" if $err;
        print "Exit code: $exit\nContinue? (y/N): ";
        chomp(my $ans = <STDIN>);
        die "Stopped by user\n" unless $ans =~ /^y/i;
    }
}

Extra tips

  • Prefer list-form execution (or IPC::Run) if you need to avoid shell interpolation and improve safety.
  • Add a dry-run flag, timestamped logging, and a timeout for long-running commands.
  • Test the script in a disposable environment before using it on real systems.

I am trying to list out a bus as individual names.

For instance - When I come across a section in a file that lists a bus like this:

[0:2] <busname>

I would like to print out this:

<busname>[0]
<busname>[1]
<busname>[2]

Any ideas in PERL or UNIX would be appreciated.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.