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...
JAVA If a user inputs a specific number of rows, columns and sections for a rectangle...
JAVA If a user inputs a specific number of rows, columns and sections for a rectangle using three symbols, how would you print using for loops and a string? Not using an array Example: If 4 was inputted for rows, 12 was inputted for columns, 6 was inputted for sections, and the following symbols (that are all chosen by the user) were “$” “*” and “%” the following would print: $$**%%$$**%% $$**%%$$**%% $$**%%$$**%% $$**%%$$**%% Has to account for all numbers...
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...
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 can change the number of rows and cells the script outputs have the script create a table with 15 rows and 5 cells refer to the tables lesson for info on how to create a table you need to write all the PHP functionality above the doctype and display the remaining HTML string via a variable in the...
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT