Lana
Lana

Reputation: 47

How would I use strings inside a for loop?

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 I've done here doesn't work.

Upvotes: 0

Views: 339

Answers (6)

Subham Behera
Subham Behera

Reputation: 50

echo -e 'Enter the number of rows:'
read rows

echo -e 'Enter the number of columns:'
read columns

string='helloworld'

i=1
while [ $i -le $rows ]; do
  j=1
  while [ $j -le $columns ]; do
    echo -n "$string "
    ((j++))
  done
  echo ""
  ((i++))
done

The script first prompts the user to enter the number of rows and columns. It initializes a string variable with the value "hello". The outer loop runs for the specified number of rows. Within the outer loop, the inner loop runs for the specified number of columns. During each iteration of the inner loop, "hello" is printed followed by a space. After completing the inner loop, a newline is printed to start a new row. The outer loop continues until all rows have been printed.

Upvotes: 0

Ivan
Ivan

Reputation: 7277

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 

Upvotes: 2

Paul Hodges
Paul Hodges

Reputation: 15273

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.

Upvotes: 0

knittl
knittl

Reputation: 265211

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

Upvotes: 0

Cyrus
Cyrus

Reputation: 88581

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

Upvotes: 0

P.P
P.P

Reputation: 121387

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 ;-)

Upvotes: 0

Related Questions