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.)
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 copies 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
function addenv () {
xx=$(env | grep -w $1)
var=${xx#*=}
locate_field $2 :$var:
if [[ $? = 0 ]]
then
export $1=$2:$var
fi
}
locate_field () {
if [[ $2 = *:$1:* ]]
then
return 1
else
return 0
fi
}
You might also like to view...
______________ cryptanalysis is applicable to block ciphers that use a substitution-permutation network including Rijndael, Twofish, and IDEA.
Fill in the blank(s) with the appropriate word(s).
Microsoft Office contains a powerful programming language called Visual Basic for Applications, more commonly known as VBA
Indicate whether the statement is true or false
What were the marks of the user-friendly systems that emerged in the fourth generation of computers?
What will be an ideal response?
Give compelling reasons for a unified I- and D-caches at deeper levels of the cache hierarchy.
What will be an ideal response?