Question: please help with ths lab The shell expands $0 to the name of the calling program (Sobell, page 474), $1$n to the individual command-line arguments

please help with ths lab

The shell expands $0 to the name of the calling program (Sobell, page 474), $1$n to the individual command-line arguments (positional parameters; Sobell, page 475), $* (Sobell, page 478) to all positional parameters, and $# (Sobell, page 479) 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 297) and a comment (Sobell, page 298). 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 114) 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 Chapter 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 displays 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 480) 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 449) 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 447) 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 439) to implement the two-way branch in the script. Use the test (Sobell, pages 435 and 1011) 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 480) 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 486, 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 echomyvar. 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 485) builtin in the interactive shell to place myvar in the environment and run echomyvar. (You can export myvar without 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 display the export attribute for myvar. The export builtin and declare x perform the same function. Use export n (Sobell, page 486) to unexport myvar. Show that myvar is a shell variable but is no longer in the environment. Use the unset (Sobell, page 314) builtin to remove myvar. Show that myvar is not available in the interactive shell or in the environment. Deliverables This lab gives you practice using positional parameters, comments, temporary files, and control structures in shell scripts that query the user and read information the user enters. It also has you work with PIDs, the export builtin, and the environment.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!