Operating System - HP-UX
1834127 Members
1908 Online
110064 Solutions
New Discussion

Re: I need to create a script as per below requirement

 
SOLVED
Go to solution
shikhar_1
Regular Advisor

I need to create a script as per below requirement

. Create folder structure reverse to one supplied. i.e. If “/a/b/c/d/e/f” is supplied as argument to script it will create “/f/e/d/c/b/a” folder in file-system.
• Folder path to be passed to script as argument
• Add validation in script, if no arguments are supplied and for invalid argument values.
• Use functions in script (or different script files) to modularize code

Appreciating your response.
10 REPLIES 10
VK2COT
Honored Contributor
Solution

Re: I need to create a script as per below requirement

Hello,

I hope this is not a school assignment :)

Just to give you a helping hand, here is the start in plain Shell:

#!/bin/sh

PATH=/bin:/usr/bin:/sbin; export PATH
MYCOMM="$(basename $0)"

helpfile () {
echo "USAGE: $MYCOMM revpath"
echo "EXAMPLE: $MYCOMM /a/b/c/d/e/f"
}

if [ $# -ne 1 ]
then
helpfile
exit 1
fi

MYPATH=$1
MYREVPATH="$(echo $MYPATH | rev)"

print "RESULT $MYREVPATH"

When run, it would do this:

# my.sh /a/b/c/d/e/f
RESULT f/e/d/c/b/a/

If you do not provide an argument:

# my.sh
USAGE: my.sh revpath
EXAMPLE: my.sh /a/b/c/d/e/f

You can extend the script in many ways.

Cheers,

VK2COT
VK2COT - Dusan Baljevic
Richard Hepworth
Esteemed Contributor

Re: I need to create a script as per below requirement

Hi,

Using perl this should do what you require:

#!/usr/bin/perl

use strict;
use warnings;
use File::Path;


my $dir = shift || die "You must specify a directory\n";
my @dirs = split('/', $dir);
my @rev_dirs = reverse(@dirs);
my $result = join('/', @rev_dirs);

mkpath("/${result}") or die "Cannot create ${result}: $!";
shikhar_1
Regular Advisor

Re: I need to create a script as per below requirement

Thanks for your reply.

My requirement is that directory should be created also like in reverse order.f/e/d/c/b/a/ through script only.
shikhar_1
Regular Advisor

Re: I need to create a script as per below requirement

And pls sxplain the script also if possible..
shikhar_1
Regular Advisor

Re: I need to create a script as per below requirement

Pls explain this line
MYCOMM="$(basename $0)"
Richard Hepworth
Esteemed Contributor

Re: I need to create a script as per below requirement

Shikhar,

The perl script above will create the directory in reverse order.
mkpath is used to make sure that the directory tree is created recursively.
The lines above that basically create an array of the specified paths without the '/', then reverses it, then joins it back together with '/'

# ./mkpath.pl /a/b/c
# ll -d /c/b/a
drwxr-x--- 2 root sys 96 Feb 15 08:25 /c/b/a

regards,

Richard
shikhar_1
Regular Advisor

Re: I need to create a script as per below requirement

Hi, I dont need perl language. I need only in shell scripting.
Dennis Handly
Acclaimed Contributor

Re: I need to create a script as per below requirement

>I don't need perl language. I need only in shell scripting.

The reason you use perl because it is easier and more powerful than the shell.

To use the shell you could put the directory components in an array and then write them out in reverse order.
I suppose you could just use recursion with basename(1) and dirname(1).
function one_component {
if [ "$1" = "/" ]; then echo; return; fi
base=$(basename $1)
dir=$(dirname $1)
echo "/$base\c"
one_component $dir
}

Note: rev(1) won't work for more than 1 char component names.
Jean-Luc Oudart
Honored Contributor

Re: I need to create a script as per below requirement

Hi

using awk (as indicated the shell solution would not work for directory names > 1 char with rev.

I assume you already check the validity and numbers of parameters to your script:
....
echo $1 | awk -F/ '{
split($0,tab);
for(i=NF;i>0;i--) printf("/%s",tab[i]);
printf("\n");
}'

Et voila !

Regards
Jean-Luc
fiat lux
James R. Ferguson
Acclaimed Contributor

Re: I need to create a script as per below requirement

Hi:

> Hi, I dont need perl language. I need only in shell scripting.

And, you don't need a hammer to drive a nail, either. You can use the butt end of a screwdriver, though it's harder to accomplish the task.

...JRF...