Question

How to write a bash shell script that prints a hollow box after user enters number...

How to write a bash shell script that prints a hollow box after user enters number of lines, ie "bash hbox 4" , prints:

****
*   *
*   *
****

My code doesnt work. i need it to take in the argument from the command line: ./hbox 4 "*"

echo "Enter number of rows"
read sz

clear

for (( x = 1; x <= $sz; x++ )); do
    for (( y = 1; y <= $sz; y++ )); do
        if [ "$x" == 1 ] || [ "$x" == "$sz" ] || [ "$y" == 1 ] || [ "$y" == "$sz" ]
         then
            echo "* "
        else
            echo " "
        fi
     done
     echo -ne ""
done

0 0
Add a comment Improve this question Transcribed image text
Answer #1

WIthout Command Line Arguments:

echo "Enter number of rows"
read sz

clear

echo "$sz"

for (( x = 1; x <= $sz; x++ )); do
for (( y = 1; y <= $sz; y++ )); do
if [ "$x" == 1 ] || [ "$x" == "$sz" ] || [ "$y" == 1 ] || [ "$y" == "$sz" ]
then
printf "* "
else
printf " "
fi
done
echo -n ""
done

Here is the screenshot below:

BashScript with command line arguments:

First argument is contained in $1 and second argument in $2

for (( x = 1; x <= $1; x++ )); do
for (( y = 1; y <= $1; y++ )); do
if [ "$x" == 1 ] || [ "$x" == "$1" ] || [ "$y" == 1 ] || [ "$y" == "$1" ]
then
printf "$2 "
else
printf " "
fi
done
echo -n ""
done

Add a comment
Know the answer?
Add Answer to:
How to write a bash shell script that prints a hollow box after user enters number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a bash shell script called direct.sh. This script will take an arbitrary number of command...

    Write a bash shell script called direct.sh. This script will take an arbitrary number of command line arguments. Your script should create directories starting with the first argument, then the second directory inside the first one, and then the next one inside the second one, and so on.

  • I am having trouble on what exactly to write so I can make the script work,...

    I am having trouble on what exactly to write so I can make the script work, can someone show how to properly do it? Use the vi editor to enter the following script (name this file testscript): #!/bin/bash #script to demonstrate : reading arguments from a command line & # interactively from within script # selection (if… then… else…. fi) # iteration (for loop) # USE FILES ENTERED ON THE COMMAND LINE echo The script you are running is $0...

  • I need this done ASAP. Please write a bash shell script that does the following: Prints...

    I need this done ASAP. Please write a bash shell script that does the following: Prints out how many users are logged on. This can be accomplished using who and wc. Prints out a list of currently logged on users. This can be accomplished using who, grep, regular expressions, and echo. Prints out how many processes you have running from past days, and a list of those jobs. Use `whoami` to get your username. Run ps -ef and see how...

  • This assignment will assess your ability to write bash scripts. Drop​ ​down​ ​to​ ​the​ ​root​ ​user​...

    This assignment will assess your ability to write bash scripts. Drop​ ​down​ ​to​ ​the​ ​root​ ​user​ ​using​ ​the​ ​command​ ​sudo su -.​ Write a bash script named /root/exam02.sh that does the following: Takes exactly 1 argument. If the number of arguments is not 1: Print "Exiting!" and exit with return code 40. If the number of arguments is exactly 1: Print the first argument as per the example below. Example output for your reference: The script should have the following...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • Create a shell program called "myprog" that prints "Hello" (use echo). Remember it must start with...

    Create a shell program called "myprog" that prints "Hello" (use echo). Remember it must start with "Shebang" and the entire path to a bash shell. So you need at least 2 lines of codes, though use a blank line between the two. Give the program rights so you have all the rights, your group reading and driving rights, and everyone else just driving rights. Now run your shell program. How can you run the program? Why is just the name...

  • BCS 215 (Unix Operating Systems) Create a bash shell script that displays a coffee vending machine...

    BCS 215 (Unix Operating Systems) Create a bash shell script that displays a coffee vending machine menu and asks the user to make a choice - 1. Print "Please enter your choice: decaf|regular|latte|cappuccino" 2. Depending on the user input, print out the related price - decaf: 2 dollars regular: 2 dollars latte: 3 dollars cappuccino: 3 dollars For anything else, please display "Please make a valid choice." 3. Upon a successful execution of the script, display "Thank you for using...

  • Exercise 1: Write a shell script that loops through the /etc/passwd file one line at a...

    Exercise 1: Write a shell script that loops through the /etc/passwd file one line at a time. Prepend each line with a line number followed by a colon and then a space. Example output: 1: root:x:0:0:root:/root:/bin/bash 2: daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin 3: bin:x:2:2:bin:/bin:/usr/sbin/nologin 4: sys:x:3:3:sys:/dev:/usr/sbin/nologin Exercise 2: Write a shell script that asks the user for the number of lines they would like to display from the /etc/passwd file and display those lines. Example output: How many lines of /etc/passwd would you like...

  • Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment...

    Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment to tell us what you did, why and how. 3. allows a user to enter their name and a number between 1 than 100 (this must be prompted so the user knows what to do) 4. creates a random number between 1 and 100 for you to guess. The command to create a random number is shown below. (if you find a better one...use...

  • 1. Write 4 simple shell scripts. Each question will ask you to write one or more...

    1. Write 4 simple shell scripts. Each question will ask you to write one or more command lines to solve a problem. Put the command(s) from each problem into a file named problem1.sh, problem2.sh Note that if you want your script to be interpreted by bash, each file should start with the line: #!/bin/bash and will have one (or more, as needed) commands following it. To execute each script in bash, so you can determine if it is working or...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT