Lab 11 Reading Read the sections on parameters Sobell pages

Lab 11

Reading Read the sections on parameters (Sobell, pages 460–470), control structures (Sobell, pages 419–435), and environment (Sobell, pages 471–474).

Procedure

The shell expands $0 to the name of the calling program (Sobell, page 461), $1–$n to the individual command-line arguments (positional parameters; Sobell, page 462), $* (Sobell, page 465) to all positional parameters, and $# (Sobell, page 466) to the number of positional parameters.

1. Write a script named all that displays (sends to standard output) the name of the calling program, the number of positional parameters, and a list of positional parameters. Include the #! line (Sobell, page 287) and a comment (Sobell, page 288). Remember to make the file executable (Sobell, page 100). Test the script with 0, 1, and 5 positional parameters.

2. Make a symbolic link (Sobell, page 113) named linkto to the all script you wrote in the previous step. Call linkto with two arguments. What does the script report as the name it was called as?

3. Write a script named myname that uses echo (most of the examples in Chap- ter 10 use this utility) to prompt the user with Enter your name: , reads into a variable the string the user types in response to the prompt, and then dis- plays Hello followed by the string the user typed. When you run the program it should look like this: $ ./myname Enter your name: Max Wild Hello Max Wild

4. Rewrite myname from the previous step, calling it myname2. Have this version of the program prompt the user for a string, but instead of displaying Hello and the string on the screen (sending it to standard output) redirect the output so the program writes only the string the user entered (and not Hello) to the temporary file named PID.name where PID is the process ID number (Sobell, page 467) of the process running the script. Display the contents of the PID.name file.

5. Write and run a script named looper that uses the for (Sobell, page 434) control structure to loop through the command-line arguments and display each argument on a separate line.

6. Rewrite looper from the previous step, calling it looper2. Have this version of the program use the for...in control structure (Sobell, page 432) to perform the same task.

7. Write a script named ifthen that prompts the user with >> and reads a string of text from the user. If the user enters a nonnull string, the script displays You entered: followed by the string; otherwise it displays Where is your input?. Use an if...then...else control structure (Sobell, page 424) to imple- ment the two-way branch in the script. Use the test (Sobell, pages 420 and 978) builtin to determine if the user enters a null string. What do you have to do to avoid getting an error message when you prompt with >>? (There are several ways to construct the test statement.)

8. Write and run a simple shell script named echomyvar that displays the PID (Sobell, page 467) of the process running the script and value of the variable named myvar. Display the PID of the interactive shell you are working with. What is the value of the variable within the process running the shell script? Are the interactive shell and the shell running the script run by the same or different processes (do they have the same PID)?

$ cat echomyvar

echo The PID of this process is $$

echo The value of myvar is: $myvar

$ echo $$

2651

$ ./echomyvar

The PID of this process is 4392

The value of myvar is:

The example near the top of Sobell, page 473, demonstrates a way to put a variable in the environment of a script you are calling without declaring the variable in the interactive shell you are running. Use this technique to assign a value to myvar, place it in the environment of echomyvar, and run echo myvar. After running the script, is myvar set in the interactive shell?

On the command line, assign a value to the variable named myvar and then run echomyvar again. Which value does the script display?

Use the export (Sobell, page 472) builtin in the interactive shell to place myvar in the environment and run echomyvar. (You can export myvar with out assigning a new value to it.) Did the script display the value of myvar this time?

Is the PID of the interactive shell the same or different from the PID of the script?

Call export without an argument and send the output through grep to dis- play the export attribute for myvar. The export builtin and declare –x perform the same function.

Use export –n (Sobell, page 473) to unexport myvar. Show that myvar is a shell variable but is no longer in the environment.

Use the unset (Sobell, page 304) builtin to remove myvar. Show that myvar is not available in the interactive shell or in the environment.

Deliverables Please submit the contents of scripts written, the command line calls that you make and the answers to any questions asked

Solution

1) ANSWER
vi all
==
#!/bin/ksh
#$0 is the variable which stores the name of calling program
echo \"NAME OF CALLING PROGRAM IS $0\"
echo \"THE NUMBER OF POSITONAL PARAMETERS ARE $#\"
echo \"LIST OF POSTIONAL PARAMETERS ARE $@\"

save this as all
after that
type

chmod 755 all
./all 0 1 5
NAME OF CALLING PROGRAM IS ./all
THE NUMBER OF POSITONAL PARAMETERS ARE 3
LIST OF POSTIONAL PARAMETERS ARE 0 1 5


2)ANSWER SYMBOLIC LINK
ln -s all symbolic_all
the above command will create symblic link {ls -s target linkname }
OUTPUT
=====
./symbolic_all 1 2
NAME OF CALLING PROGRAM IS ./symbolic_all
THE NUMBER OF POSITONAL PARAMETERS ARE 2
LIST OF POSTIONAL PARAMETERS ARE 1 2


3)ANSWER
vi myname
#!/bin/bash
echo \"Enter your name:\"
read name #this will read date from keyboard
echo \"Hello $name\"

output
========
chmod 755 myname
./myname
Enter your name:
Max Wild
Hello Max Wild


4)ANSWER
===========

#!/bin/bash
echo \"Enter your name:\"
read name #this will read date from keyboard
echo \"Hello $name\" > $$.name
cat $$.name


===
$$ is the PID for current process

5)ANSWER
for i in \"$@\" #this loop iterate the entire list of positional parameter
do
echo $i
done

output
==
./looper 1 2 3 4
1
2
3
4


6)ANSWER
vi looper2

for i in {1..$#} #this loop iterate from 1 to toal number of positional parameter $#
do
echo $i
done


First chmod 755 looper2
output:
./looper2 1 2 3 4
1
2
3
4

===
7)
#!/bin/bash
echo \">>\"
read name
if [ ! -z \"$name\" ]
then
echo $name
else
echo \"What is your input ?\"
fi

output: for null
./ifthen
>>

What is your input ?

==
output for not null
./iften
>>
abc
abc

==============
8)
$cat echomyvar
echo \"The PID of the process is $$ \"
echo \"The value of myvae is : $myvar \"
$./echomyvar
The PID of the process is 111584
The value of myvae is :
$echo $$
109222
$myvar=10
$echo $myvar
10
$./echomyvar
The PID of the process is 112107
The value of myvae is :
$export myvar
$./echomyvar
The PID of the process is 112130
The value of myvae is : 10

PID for parent and chile will be different
export will export the variable to child also

Lab 11 Reading Read the sections on parameters (Sobell, pages 460–470), control structures (Sobell, pages 419–435), and environment (Sobell, pages 471–474). Pro
Lab 11 Reading Read the sections on parameters (Sobell, pages 460–470), control structures (Sobell, pages 419–435), and environment (Sobell, pages 471–474). Pro
Lab 11 Reading Read the sections on parameters (Sobell, pages 460–470), control structures (Sobell, pages 419–435), and environment (Sobell, pages 471–474). Pro
Lab 11 Reading Read the sections on parameters (Sobell, pages 460–470), control structures (Sobell, pages 419–435), and environment (Sobell, pages 471–474). Pro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site