Question

In: Computer Science

Write a script to display the following patterns on the screen. Number of rows and columns...

Write a script to display the following patterns on the screen. Number of rows and columns are taken from the command arguments; if they are missing, set default to 3 (rows) and 4 (columns). Hint: you will use a nested loop.

****

****

****

a) Display the source code in an editor (#4-11)

b) Execute your script in the terminal, and display the command and the result (#4-12)

Solutions

Expert Solution

The shell script (pattern_script.sh): (The pictures are attached below)

# pattern_script.sh
#!/usr/bin/env sh 
# A shell script to print patterns on the screen.
# Rows and columns are taken from command arguments, if missing default values are taken.
defRow=3                # Default value for row
defCol=4                # Default value for column
row=${1:-$defRow}               #If first argument exists, row is assigned with given value else default value
column=${2:-$defCol}            #If first argument exists, column is assigned with given value else default value
for (( i = 1; i <= row; i++ ))
do

    for (( j = 1 ; j <= column; j++ ))
    do
          echo -n "*"           # Printing * for each iteration of inner loop
    done

  echo ""               #Printing new line
done

Output 1: (Arguments 10 and 10 are passed using command "sh pattern_script.sh 10 10", number of rows and columns will be 10 and 10).

Vinny@DESKTOP-1UC0IAM MINGW64 ~/Documents/shs
$ sh pattern_script.sh 10 10
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Output 2: (No arguments are passed in the command "sh pattern_script", hence default rows i.e. 3 and default columns i.e. 4 will be used).

Vinny@DESKTOP-1UC0IAM MINGW64 ~/Documents/shs
$ sh pattern_script.sh
****
****
****

Note: The reader has to make sure to save the shell script file in proper name and use the same name in the command with .sh extension. Passing the command line arguments is shown in the image below, reader has to make sure passing command line arguments properly.

a. Display the source code in an editor:

The editor used in this image is Visual Studio Code ( VS Code for short).

b. Execute your script in the terminal, and display the command and result.

The terminal used in this image is Git Bash: (Any shell executable terminal can be used by the reader)

Note: When code copied from above, student has to replace [NBSP] with space(' ') in the code, if found any.


Related Solutions

(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell...
(10pts) Write a shell script to display your name to the screen. (10pts) Write a shell script to take a directory as an argument and display the contents of that directory (10pts) Write a shell script that takes any command as a parameter and displays help on that command (e.g. the result of the execution of man <command>). (10pts) Write a shell script that requires two file names as parameters and copies content of one file into another without asking...
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given).
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given). Important: Next, the expression "a single line of code" implies a single command or equality. In other words, the code: X=A+1; X=X+B; is considered to be TWO lines of code, even though it can be written as one line. (a) (3%) Write a single line of code which saves the first two rows of array A...
Write a script that will output a table where the rows and cells are labeled. The...
Write a script that will output a table where the rows and cells are labeled. The script should be versatile, so one could easily change the number of rows and cells the script should output. For this exercise have the script create a table with 15 rows and 5 cells. Refer to the tables lesson for information on how to create a table. You need to write all the PHP functionality above the doctype and display the resulting HTML string...
1 How can we specify the number of rows and columns in a grid layout? 2....
1 How can we specify the number of rows and columns in a grid layout? 2. What happens when we don't have enough items to be displayed in a grid layout with a specified size? 3. How can we specify a grid layout component to occupy more than one columns or rows? 4. What happens if the components in a linear and grid layout exceed the layout size? How can we handle this problem so that all components can be...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result: 25 69 51 26 171 68 35...
9.) Write a MATLAB script that will read the Pressure and Temperature columns from the provided...
9.) Write a MATLAB script that will read the Pressure and Temperature columns from the provided Excel file. Use a loop to calculate the linear best fit line for the data. You may only use the built-in functions sum, size, and length. Plot both the individual data points (red) and the fit line (blue) on the same graph. Include a title and axes labels.
[PLEASE USE C++] Write a function to read values of a number of rows, number of...
[PLEASE USE C++] Write a function to read values of a number of rows, number of columns, 2 dimensional (2D) array elements and display the 2D array in a matrix form. Input 2 3 1 4 5 2 3 0 Where, First line of represents the number of rows. Second line of input represents the number of columns. Third line contains array elements of the 1st row and so on. Output 1 4 5 2 3 0 where There must...
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through...
Write a rudimentary spreadsheet program using C++. Display a grid of cells with columns A through H and rows 1 through 20. Accept input in the first row of the screen. The commands are of the form column row entry, where entry is a number, a cell address preceded by a plus (e.g., +A5), a string, or a function preceded by an at sign, @. The functions are: max, min, avg, and sum. During execution of your program, build and...
Write a query to display the columns listed below. For each customer the query should show...
Write a query to display the columns listed below. For each customer the query should show the current system date, the current day (when you do the problem the date and day will be different), the number of characters in the member last name, the last date the customer rented a video and how many total videos the person rented. /* Database Systems, 9th Ed., Coronel/MOrris/Rob */ /* Type of SQL : MySQL */ CREATE SCHEMA IF NOT EXISTS TINY_VIDEO;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT