Lists are commonly stored in environment variables by putting a colon (:) between each of the list
Question:
Lists are commonly stored in environment variables by putting a colon (:) between each of the list elements. (The value of the PATH variable is an example.) You can add an element to such a list by catenating the new element to the front of the list, as in
PATH=/opt/bin:$PATH
If the element you add is already in the list, you now have two cop:wqies of it in the list.
Write a shell function named addenv that takes two arguments: (1) the name of a shell variable and (2) a string to prepend to the list that is the value of the shell variable only if that string is not already an element of the list. For example, the call
addenv PATH /opt/bin
would add /opt/bin to PATH only if that pathname is not already in PATH. Be sure your solution works even if the shell variable starts out empty. Also make sure you check the list elements carefully. If /usr/opt/bin is in PATH but /opt/bin is not, the example just given should still add /opt/bin to PATH. (Hint:You might find this exercise easier to complete if you first write a function locate_field that tells you whether a string is an element in the value of a variable.)
Business
ISBN: 978-0324829556
10th Edition
Authors: Willian M Pride, Robert J. Hughes, Jack R Kapoor