?
Solved

Need help on Unix

Posted on 2012-09-04
9
Medium Priority
?
359 Views
Last Modified: 2012-09-07
Hi ,

I have write a shell script which extracts the folder info from an environmental variable.
BASE_DIR="/home/app/goo/collecto/output" is the folder path stored in the BASE_DIR variable.

I need to extract /home/app/goo/collecto from the BASE_DIR variable, below is the commands which I have used , but Iam not getting the actual result

pd= `echo $BASE_DIR | gawk '{print substr($0,1,match($0,"collecto"))}'

the output Iam getting is /home/app/goo/c and not /home/app/goo/collecto.
kindly help me in this regard

thanks in advance
sam
0
Comment
Question by:sam_2012
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 38364731
#!/bin/bash
BASE_DIR="/home/app/goo/collecto/output"
pd=${BASE_DIR%/*}
0
 
LVL 19

Expert Comment

by:Ken Butters
ID: 38364771
pd= `echo $BASE_DIR | gawk '{{match($1,"Collecto");print substr($0,1,RSTART+RLENGTH))}'
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38364857
pd=$(dirname $BASE_DIR)

pd=$(echo $BASE_DIR | awk -F"collecto" '{print $1 FS}')

pd=$(echo $BASE_DIR | sed 's/\(^\/.*collecto\).*/\1/')
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

Expert Comment

by:ozo
ID: 38364982
What would you want $pd to contain if "collecto" did not occur in $BASE_DIR
or "collecto" occurred multiple times in  $BASE_DIR, or if "collectorship" occurs in $BASE_DIR
or if there were more levels of directories under collecto?
Some of the suggested solutions produce different results in those cases.
0
 

Author Comment

by:sam_2012
ID: 38366021
hi ozo

if collecto is not present in $BASE_DIR it should throw an error and exit,  if collecto appears multiple times in basedir , the one after goo should be used , no collectorship should not be selected , it should error out , even if there are more levels of directories uner collecto , the variable pd should contain only the path upto collecto.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 800 total points
ID: 38367026
if echo $BASE_DIR | grep -q "goo/collecto/"; then
    pd=$(echo $BASE_DIR | awk -F"goo/collecto" '{print $1 FS}')
  else
     echo "Error. Invalid Path!"
     exit 22
fi

The above will return an error message if "goo/collecto/" is not found in BASE_DIR and exit with a return code of 22 ("Invalid Argument"),
else it will retiurn the path from the left up to and including the first occurrence of "goo/collecto".
Things like "collectorship" etc. will induce an error as well.

Two questions:

What should be done if "goo/collecto" is the last element in BASE_DIR?
What should be done if "/collecto/" is present in BASE_DIR, but not "goo/collecto/"?
0
 

Author Comment

by:sam_2012
ID: 38370669
hi woolmilkproc

question 1: when collecto is the last element then , I have to store the entire path in my pd variable.
Question 2: if only collecto is present then store the entire path in pd variable upto collecto.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 38371259
OK. A bit more code, but here you go:

if echo "$BASE_DIR" | grep -qw "collecto"; then
   SEP="collecto"
   if echo "$BASE_DIR" | grep -q "goo/collecto"; then
      SEP="goo/collecto"
   fi
    pd=$(echo "$BASE_DIR" | awk -F"$SEP" '{print $1 FS}')
  else
     echo "Error. Invalid Path!"
     exit 22
fi
0
 

Author Closing Comment

by:sam_2012
ID: 38377191
thanks , that really met my requirement
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question