Google Answers Logo
View Question
 
Q: Bash shell script assignment help ( Answered,   0 Comments )
Question  
Subject: Bash shell script assignment help
Category: Computers > Programming
Asked by: derekwtp-ga
List Price: $15.00
Posted: 28 Apr 2003 08:00 PDT
Expires: 28 May 2003 08:00 PDT
Question ID: 196520
scope of the assignment

Write a shell script to concatenate lists together, and output the
resulting list. Do not include any argument that is a sub-list of the
entire list. (The script will clean the list of any redundant items.)
You must preserve the original order of the list. Remember to account
for the following situations:
# a : at the beginning of the list is the same as a . at the beginning
of the "list" (:/bin is the same a .:/bin)
# a :: any where in the list is the same as :.: (/bin::/etc is the
same as (/bin:.:/etc)
# a : at the end of the list is the same a :. ( /bin: is the same as
/bin:. )

 
# Project usings temporary files will not be graded.

 

The input to the script will be colon or space separated lists and the
output will be a single colon separated list with all redundant items
removed.
usage: clean_list list ....
where list is a colon or whitespace separated list.
Examples:

prompt> clean_list a a:b a:b:c :x: y:z
a:b:c:.:x:y:z

prompt>clean_list /bin:/usr/bin:/usr/openwin/bin \
/usr/bin:/usr/etc:/etc: /usr/bin/X11 .:/bin

/bin:/usr/bin:/usr/openwin/bin:/usr/etc:/etc:.:/usr/bin/X11

prompt>clean_list apple:orange:apple pear orange peach
apple:orange:pear:peach
 


script that I have so far. AS you can see my output doesnt acheive the
desired result and I cant understand why.

[root@win186821wks unix_class]# sh ass2.sh
:/usr/bin:/usr/bin/:/usr/not_dup
.:/usr/bin:/usr/bin/

script

#!/bin/sh

for P in `echo $1 | sed -e 's/^:/.:/' -e 's/::/:.:/' -e 's/:$/:./' -e
's/:/ /g'`
      do

      case $NP in

      "")
            if [ -d "$P" ]
                  then

                  NP="$P"

            fi
      ;;$P|$P:*|*:$P:*|*:$P)
            continue

      ;;
      *)
            if [ -d "$P" ]
                  then

                  NP="$NP:$P"

            fi
      ;;esac

done

echo $NP
Answer  
Subject: Re: Bash shell script assignment help
Answered By: voyager-ga on 28 Apr 2003 09:16 PDT
 
Hi derekwtp,

as far as I can see, your script has only three little errors - I'll
try to leave it mostly the way you started to make the corrections
easier to follow:

1) You are using /bin/sh - that's a common mistake and your script
will run with that mistake on most systems... however /bin/bash !=
/bin/sh !!!

2) Your echo statement only captures the input up to the first white
space.

3) You are using -d which is checking if your path points to valid
directories... that is not the task you set out to complete - you just
wanted to check if a path can be condensed (/usr/not_dup does not
exist on your system, so it is crossed off your list). So you actually
solved a more complex problem than you were required to solve... just
eliminate the if statements and your code runs as required.

#!/bin/bash

for P in `echo $* | sed -e 's/^:/.:/' -e 's/::/:.:/' -e 's/:$/:./' -e
's/:/ /g'`
      do

      case $NP in

      "")
                  NP="$P"

      ;;
      $P|$P:*|*:$P:*|*:$P)
            continue

      ;;
      *)
                  NP="$NP:$P"

      ;;esac

done

echo $NP

I hope this helps - if I somehow misunderstood the requirements of
your task, or if you have further questions, please feel free to
request a clarification.

voyager-ga

Request for Answer Clarification by derekwtp-ga on 28 Apr 2003 12:10 PDT
Thanks voyager..could you help me on my other assignment also?

Request for Answer Clarification by derekwtp-ga on 28 Apr 2003 12:11 PDT
it's already listed

Clarification of Answer by voyager-ga on 28 Apr 2003 15:22 PDT
Hi derekwtp,

your other assignment was already locked by the time I started
answering this one. I'm sure the other researcher will give you an
adequate answer and am happy that mine was satisfactory.

I hope to be able to help you again in the future!

voyager-ga
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy