Question: I need help with my Linux / Unix programming class. Please teach me how to write code and execute the commands following the procedure guidelines.

I need help with my Linux / Unix programming class. Please teach me how to write code and execute the commands following the procedure guidelines. If you can explain all steps appreciate it. Also attach output please. Thank you

OBJECTIVE : In this lab you will learn to write more advanced shell scripts that work with command-line arguments, control structures, and temporary files. Using read, these scripts will read information the user enters and, using echo, display information based on what the user enters. You will also learn about processes, PIDs, and using export to modify the environment.

Procedure The shell expands $0 to the name of the calling program, $1$n to the individual command-line arguments (positional parameters; Sobell, page 462), $* 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 and a comment. Remember to make the file executable. Test the script with 0, 1, and 5 positional parameters.

2. Make a symbolic link 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 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 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 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 to implement the two-way branch in the script. Use the test built-in 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 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 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 472) 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 473) to unexport myvar. Show that myvar is a shell variable but is no longer in the environment.

Use the unset 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!