简体   繁体   中英

How would I use strings inside a for loop?

I apologize for my question not being specific enough but I have no choice. So I received an assignment that hasn't been completely covered in the learning material (even the person assigned to help students is having trouble helping me) since this is beyond basic bash scripting. I'm not expecting anybody to do my assignment but if I can get a clue or an idea it'll be very helpful!

My assignment:

Code a script in bash linux that will use user's input of number of rows and number of columns, and print 'hello' strong according to the user's input, like so:

For example:
User's input of number of columns:2
User's input of number of rows: 3

hello hello
hello hello
hello hello

I thought in this direction but I can't figure it out and will appreciate any help:)

echo -e 'Please enter number of rows: \n'
read rows
echo -e 'Please enter number of columns: \n'
read columns

string='hello'
for i in $columns
do
    echo $string
    string+=$string
done

(this is as far as I got with the first loop as what ive done here doesn't work)

Check this out:

#!/bin/bash

read -p 'Please enter number of rows and columns: ' rows columns # prompt and read both vars at once
string='hello' # set string

printf -v row "%${columns}s" # create   var $row consists on N(columns) spaces
row=${row//' '/"$string "}   # recreate var $row changing spaces to "$string "

printf -v col "%${rows}s"    # create var $col consists on N(rows) spaces
all=${col//' '/"$row\n"}     # create full set in var $all by changing spaces to "$row\n"

printf "$all" # print all

Testing:

$ ./ex
Please enter number of rows and columns: 3 5
hello hello hello hello hello 
hello hello hello hello hello 
hello hello hello hello hello 

To read inputs you can use read builtin. For example

read -r row column
  • Then you can use $row and $column variables.

  • You'd need a nested for loop to print row x column times.

  • To not print newlines, use -n option of echo .

Refer help read , help for , and help echo for details. You can obviously Google these terms, too;-)

With two loops:

#!/bin/bash

string='hello'
read -p "x:" x
read -p "y:" y

for ((j=0; j<$y; j++)); do
  for ((i=0; i<$x; i++)); do
    echo -n "$space$string"
    space=" "
  done
  space=""
  echo
done

See: man bash

Do yo want to golf it? :)

printf "%$((rows*columns))s" | fold -w "$columns" | sed 's/ /hello /g'

To prompt the user for rows and colums , use the read builtin:

read -p 'Enter rows: ' rows
read -p 'Enter columns: ' columns

I prefer to get my arguments on the command line.
Accordingly, one implementation (with no error checking...):

rows=$1                        # first arg is rows to output
cols=$2                        # second column is columns wanted
str=$3                         # third arg is the string to print

while (( rows-- ))             # post-decrement rows 
do c=$cols                     # reset a column count for each new row
   while (( c-- ))             # post-decrement columns done
   do printf "%s " "$str"      # print the string with a trailing space, NO newline
   done
   printf "\n"                 # print a newline at the end of each row
done

Make sure you understand (( ... )) arithmetic processing, printf , and command line argument parsing. All these are available in the documentation .

For extra credit, do proper error checking of your inputs.

If you need to read the inputs from stdin instead of the command line, replace

rows=$1                        # first arg is rows to output
cols=$2                        # second column is columns wanted
str=$3                         # third arg is the string to print

with

read rows cols str

Better, read each with an appropriate prompt - again, details available in the manual.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM