0

I've been trying to learn shell script recently, and the one thing that's never been very clear to me in any of the tutorials i've read is how to concatenate integers and strings for the printf command. Specifically, I was wondering how to use this to display a character in a certain position on the Terminal(command prompt) window. For example, in python, when I'm not using curses or pygame, i'd do something like:

x = 40
y = 12
ship = "|-^-|"
print(("\n"*y)+(" "*x)+ship)

if the way I am approaching this is the "correct" way for shell, then I would like some help and maybe a link to a good tutorial for any problems I might run into later. However, if this is not what I should be doing, then I would really like an explanation on why not and what I should be doing instead. Many thanks in advance

dakatk
  • 99
  • 1
  • 1
  • 13
  • The Python example shown doesn't concatenate strings and integers; it repeats strings an integer number of times. – Ramchandra Apte Dec 18 '13 at 04:04
  • See [this question](http://stackoverflow.com/questions/3211891/shell-script-create-string-of-repeated-characters) on how to repeat a string in shell script. – Ramchandra Apte Dec 18 '13 at 04:13
  • @Ramchandra sorry for the confusion, that was the closest example I could find within my collection of scripts. – dakatk Dec 18 '13 at 04:31
  • To print a character at a certain position in shell script one could simply repeat newlines y number of times and spaces x numbers of times and then print the character, like your Python solution. To print integers and strings together, simply use `printf $number $string`; the shell doesn't have the notion of a number; variables holding numbers are actually strings. – Ramchandra Apte Dec 18 '13 at 04:35

2 Answers2

0

Call this script with two arguments. The first is the number of rows (x), and the second is the number of columns (y):

#!/bin/sh
for x in $(seq $1)
do
    echo ""
done
for y in $(seq $2)
do
    printf " "
done
echo "|-^-|"

(Since this question was tagged shell and sh but not bash, the above avoids the bashisms.)

John1024
  • 109,961
  • 14
  • 137
  • 171
0

The proper way to do cursor addressing in a terminal is with ncurses. The tput command is a common tool for this. The manual page is not useful as a beginner's documentation, though; but see e.g. the introduction at http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html

tripleee
  • 175,061
  • 34
  • 275
  • 318