Computer Science Assignment – pro bash programming 40



1. Write a script that creates a directory called bpl inside $HOMEPopulate this directory with two sub directories, bin and scripts.


2. Write a script to create the “Hello, World!” script, hw, in$HOME/bpl/bin/; make it executable; and then execute it.



chp 2


1. What is wrong with this command?


tr A Z < $HOME/temp > $HOME/temp


2. Write a script, using $RANDOM, to write the following output both to a file and to a variable. The following numbers are only to show the format; your script should produce different numbers:











chp 3


1. Write a script that asks the user to enter a number between 20 and 30. If the user enters an invalid number or a non-number, ask again. Repeat until a satisfactory number is entered.


2. Write a script that prompts the user to enter the name of a file. Repeat until the user enters a file that exists.



chp 4


1. How many arguments are there on this command line?


sa $# $(date “+%Y %m %d”) John Doe


2. What potential problem exists with the following snippet?


year=$( date +%Y )


month=$( date +%m )


day=$( date +%d )


hour=$( date +%H )


minute=$( date +%M )


second=$( date +%S )



chp 5


1. By default, where can a variable assigned in a script be accessed? Select all that apply:


•In the current script


•In functions defined in the current script


•In the script that called the current script


•In scripts called by the current script


•In subshells of the current script


2. I advise against using single-letter variables names but give a couple of places where they are reasonable. Can you think of any other legitimate uses for them?


3. Given var=, write a script that uses parameter expansion to extract the second number,168.



chp 6


1. Rewrite function is validipusing parameter expansion instead of changing IFS.


2. Add a check to_max3to verify that all three arguments are integers.



chp 7


1. What is wrong with this code (besides the inefficiency noted at the beginning of the chapter)?


if ! echo $PATH |grep -q /usr/games






2. Write a function called to _ lower that does the opposite of the to _ upper function in


Listing 7-4.


3. Write a function, palindrome that checks whether its command-line argument is a palindrome (that is, a word or phrase that is spelled the same backward and forward). Note that spaces and punctuation are ignored in the test. Exit successfully if it is a palindrome.


Include an option to print a message as well as set the return code.


4. Write two functions, ltrimand rtrim, that trim characters in the same manner as trim but from only one side of the string, left and right, respectively.



chp 8


1. Modify the kjv firsts script accept a command-line argument that specifies how many chapters are to be printed.


2. Why are the chapter and verse numbers in kjv firsts formatted with %s instead of %d?


3. Write an awk script to find the longest verse in KJV.



chp 9


1. Write a script that stores the time it takes a command (your choice of command) to run in three variables, real, user, and system, corresponding to the three default times that time prints.



chp 10


1. What is wrong withif [ $var=x ]? What should it be? Why does it give the result it does?


2. Write a function, valid_square(), that returns successfully if its sole argument is a valid SAN chessboard square or fails if it is not. Write a function to test that it works.



chp 11


1. Modify the menu function to accept its parameters from a file.


2. Rewrite thepr1function asprx that will behave in the manner ofpr4from Chapter 8 but will take an option for any number of columns.


3. Add age top ts section to the fifteen game that allows the user to select between three different board formats. Write a third format.



chp 12


1. Add code to the upload script that checks that all variables have been set to legitimate values (for example, that port is an integer).


2. Write a usage or help function, and add it to the upload script.


3. Add an option to the upload script to save the configuration if it has been saved.


4. Write a script that creates a configuration file in the same form as words. cfg, prompting the user for the information to put in it.



chp 13


1. Modify the is or tand a search functions to use sort and grep, respectively, if the array exceeds a certain size.


2. Write a function that transposes rows and columns in a grid (either a single-string grid or an array). For example, transform this:








into this:








3. Convert some of the grid functions, either string or array versions, to work with grids that are not square, for example, 6×3.


4. Convert the code that parses fixed-width records into a function that accepts the line of data as the first argument, followed by thevarname=width list.



chp 14


1. Write a function, hbar, that accepts two integer arguments, a width and a color, and prints a bar of that color and width. Write a second function, hbar_ at, that accepts four arguments, row, column, width and color; moves the cursor to the row and column; and passes the remaining arguments tohbar.


2. White a function, clear_ area, that accepts two integer arguments, rows and columns, and clears a rectangular area of that many rows and columns.



chp 15.


1. Using the key-funcslibrary, write a menu script that uses the function keys for selection.


2. Rewrite the key-funcslibrary to include mouse handling, and incorporate the function into the mouse-demo script.


3. The password script does minimal checking for invalid entries. What checking would you add?



How would you code it?

Scroll to Top