Linux questions 3. What is the purpose of the PATH variable? a. Set the PATH var
ID: 3704283 • Letter: L
Question
Linux questions
3. What is the purpose of the PATH variable?
a. Set the PATH variable so that it causes the shell to search the following directories in order:
-- /usr/local/bin
-- /usr/bin
-- /bin
-- /usr/kerberos/bin
-- The bin directory in your home directory
-- The working directory
b. If there is a file named doit in /usr/bin and another file with the same name in your ~/bin, which one will be executed? (Assume that you have execute permission for both files.)
c. If your PATH variable is not set to search the working directory, how can you execute a program located there?
d. Which command can you use to add the directory /usr/games to the end of the list of directories in PATH?
4. Assume that you have made the following assignment:
$ person=jenny
Give the output of each of the following commands:
a. echo $person
b. echo ‘$person’
c. echo “$person”
5. The following shell script adds entries to a file named journal-file in your home directory. This script helps you keep track of phone conversations and meetings. (The first line is the command to show you what is in the script.)
$ cat journal
# journal: add journal entries to the file
# $HOME/journal-file
file=$HOME/journal-file
date >> $file
echo -n "Enter name of person or group:
" read name
echo "$name" >> $file
echo >> $file
cat >> $file
echo "----------------------------------------------------" >> $file
echo >> $file
a. What do you have to do to the script to be able to execute it?
b. Why does the script use the read builtin (page 953) the first time it accepts input from the terminal and the cat utility the second time?
6. Assume that the /home/jenny/grants/biblios and /home/jenny/biblios directories exist. Give Jenny’s working directory after she executes each sequence of commands given. Explain what happens in each case.
a. $ pwd
/home/jenny/grants
$ CDPATH=$(pwd)
$ cd
$ cd biblios
b. $ pwd
/home/jenny/grants
$ CDPATH=$(pwd)
$ cd $HOME/biblios
7. Name two ways you can identify the PID number of your login shell.
8. Give the following command:
$ sleep 30 | cat /etc/inittab
Is there any output from sleep? Where does cat get its input from? What has to happen before the shell displays another prompt?
9. Write a sequence of commands or a script that demonstrates that variable expansion occurs before pathname expansion.
10. Write a shell script that outputs the name of the shell that is executing it.
Explanation / Answer
4)Answer:
a. echo $person
output:
jenny
b. echo '$person'
output:
$person
c. echo "$person"
output:
jenny
6)Answer:
a)After executing the preceding commands, Jenny’s working directory is /home/jenny/grants/biblios. When CDPATH is set and the working directory is not specified in CDPATH, cd searches the working directory only after it searches the directories specified by CDPATH.
b)After executing the preceding commands, Jenny’s working directory is /home/jenny/biblios. When you give cd an absolute pathname as an argument, cd does not use CDPATH
7)Answer:
PPID identifies the PID number
Pstree , ps –f
8)Answer:
There is no output from sleep (try giving the command sleep 30 by itself). The /etc/inittab file provides input for cat (when cat has an argument, it does not check standard input). The sleep command has to run to completion before the shell displays another prompt
10)Answer:
There are many ways to solve this problem. The following solutions are all basically the same. These scripts take advantage of the PPID shell variable, which holds the PID number of the shell that is the parent of the process using the variable. They also use the fact that echo changes multiple sequential SPACEs to a single SPACE. The cut utility interprets multiple sequential SPACEs as multiple delimiters so, without echo, the script does not work properly
$ cat a
pid=$PPID
line=$(ps | grep $pid)
echo $line | cut --delimiter=" " --fields=4
$ cat a2
pid=$PPID
echo $(ps | grep $pid) | cut --delimiter=" " --fields=4
$ cat a3
echo $(ps | grep $PPID) | cut --delimiter=" " --fields=4
The easy solution is to give the following command:
$ echo $0
The $0 is the first command line token, which is usually the name of the script or program that is running (page 481). In some cases, such as when you call the script with a relative or absolute pathname, this outcome may not be exactly what you want.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.