- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: I need to create a script as per below require...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2011 10:53 PM
02-14-2011 10:53 PM
• 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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2011 11:46 PM
02-14-2011 11:46 PM
SolutionI 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2011 11:58 PM
02-14-2011 11:58 PM
Re: I need to create a script as per below requirement
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}: $!";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2011 11:59 PM
02-14-2011 11:59 PM
Re: I need to create a script as per below requirement
My requirement is that directory should be created also like in reverse order.f/e/d/c/b/a/ through script only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2011 12:07 AM
02-15-2011 12:07 AM
Re: I need to create a script as per below requirement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2011 12:21 AM
02-15-2011 12:21 AM
Re: I need to create a script as per below requirement
MYCOMM="$(basename $0)"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2011 12:26 AM
02-15-2011 12:26 AM
Re: I need to create a script as per below requirement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2011 12:37 AM
02-15-2011 12:37 AM
Re: I need to create a script as per below requirement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2011 01:39 AM
02-15-2011 01:39 AM
Re: I need to create a script as per below requirement
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2011 03:24 AM
02-15-2011 03:24 AM
Re: I need to create a script as per below requirement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2011 06:15 AM
02-15-2011 06:15 AM
Re: I need to create a script as per below requirement
> 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...