Need some help with the following linuxUNIX activities When
Need some help with the following linux/UNIX activities:
When you use env or printenv, there may be so many environment variables that they scroll of the screen; and you\'ll also notice that they are not in alphabetical order.
Part 1: Provide the command to display the list of environment variables in alphabetical order (using the sort command) and one page at a time.
Part 2: With the Bourne shell family, the shell variables all have uppercase case names; which means you can\'t tell if a particular variable is global or local just by looking at its name. how do you determine which Bourne shell variables are local?
Part 3: List 4 shells of the Bourne shell family and 2 shells of the C-Shell family
Part 4: Explain why the shell variable naming convention is different between the Bourne Shell family and C-Shell family.
Solution
Part 1: sort is a very simple and useful command which is used to rearrange the lines in a text file so that they can be sorted, alphabetically and numerically.
The command that is used to display the list of environment variables in alphabetical order(using sort command) is:
sort data.txt
for example- b, d, c, a, e
the sort command will sort the lines using sort data.txt and which will produce the following output: a, b, c, d, e,
Part 2: The only way to determine which Bourne shell are not exported, in this case it is to be compare with the output “set” to the output “env” . if a variable is listed by set but not by env then in that case it is a sell variable. If the variable is listed by a set and by env then both of them are shell variable and an environment variable.
Part 3: shell family :
There are two families of shell
I)Bourne shell: there are four shell in the Bourne family and they are as follows
II)C shell: There are two shell in the Berkeley/c family and they are as follows
Part 4:
The shell variable naming convention is different between the Bourne Shell family and C-Shell family.
Bourne Shell Family:
The Bourne shell family does not really distinguish between shell and environment variables. When a shell starts up, it reads the information in the table of environment variables, defines itself a shell variable for each one, using the same name (also uppercase by convention), and copies the values. From that point on, the shell only refers to its shell variables. If a change is made to a shell variable, it must be explicitly \"exported\" to the corresponding environment variable in order for any forked subprocesses to see the change. Recall that shell variables are local to the shell in which they were defined.
Shell variables are defined by assignment statements and are unset by the unset command. The format of the assignment statement is:
$ NAME=value; export NAME
where there are no spaces around the equal sign (=). In most cases you will want to include the optional export portion of the command (following the semicolon), but this is optional. The unset command format is:
$ unset NAME
where NAME is the variable name, and value is a character string that is the value of the variable. If the string includes blanks (i.e. if it encompasses multiple values), enclose the string in double quotes, e.g.,
$ NAME=\"value1 value2 ...\"
The values of all the current variables may be displayed with the set command.To use a variable in a command, preface it with a dollar sign ($). This tells the command interpreter that we want the variable\'s value, not its name, to be used. For example, to see the value of a single variable, enter:
$ echo $NAME
we can also use ${NAME}, which avoids confusion when concatenated with text.
To prepend or append a value to an existing environment variable, use the following syntax:
$ NAME=prepend_value$NAME
or
$ NAME=$NAMEappend_value
Appending and prepending is commonly used with the PATH variable, and a colon is used as a separator, e.g.,
$ PATH=${PATH}:${XYZ_DIR}
C Shell Family:
A shell variable is defined by the set command and deleted by the unset command. The main purpose of your .cshrc file is to define such variables for each process. To define a new variable or change the value of one that is already defined, enter:
% set name=value
where name is the variable name, and value is a character string that is the value of the variable. If value is a list of text strings, use parentheses around the list when defining the variable, e.g.,
% set name=(value1 value2 value3)
The set command issued without arguments will display all your shell variables. You cannot check the value of a particular variable by using set name, omitting =value in the command; this will effectively assign an empty value to the variable.
To delete, or unset, a shell variable, enter:
% unset name
To use a shell variable in a command, preface it with a dollar sign ($), for example $name. This tells the command interpreter that you want the variable\'s value, not its name, to be used. You can also use ${name}, which avoids confusion when concatenated with text.
To see the value of a single variable, use the echo command:
% echo $name
If the value is a list, to see the value of the nth string in the list enter:
% echo $name[n]
The square brackets are required, and there is no space between the name and the opening bracket.
To prepend or append a value to an existing shell variable, use the following syntax:
% set name=prepend_value${name}
or
% set name=${name}append_value
Note that when a shell is started up, four important shell variables are automatically initialized to contain the same values as the corresponding environment variables. These are user, term, home and path. If any of these are changed, the corresponding environment variables will also be changed.
Environment Variables Environment variables are set by the setenv command, and displayed by the printenv or env commands, or by the echo command as individual shell variables are. Some environment variables are set by default (e.g., HOME, PATH).The formats of the commands are (note the difference between set and setenv):
% setenv [NAME value]
% unsetenv NAME
where value is interpreted as a character string. If the string includes blanks (i.e. if it encompasses multiple values), enclose the string in double quotes (\"), e.g.,
% setenv NAME \"value1 value2 ...\"
The current environment variable settings can be displayed using the setenv command with no arguments.
To use an environment variable in a command, preface it with a dollar sign ($), for example $NAME. This tells the command interpreter that you want the variable\'s value, not its name, to be used. You can also use ${NAME}, which avoids confusion when concatenated with text.
To prepend or append a value to an existing environment variable, use the following syntax:
% setenv NAME \"prepend_value${NAME}\"
or
% setenv NAME \"${NAME}append_value\"
If the pre- or appended value is the value of a preexisting environment variable, enclose the variable name in braces, too, e.g.,
% setenv NAME \"${NAME}${XYZ_VAR}\"
Appending and prepending is commonly used with the PATH variable, and a colon is used as a separator, e.g.,
% setenv PATH \"${PATH}:${XYZ_DIR}\"


