sh - Center a Text in terminal -
i write follow script, print 1 name center of terminal. in last command, when use numbers, ok. however, when use variables x_center , y_center have trouble...
#!/bin/sh `clear` num_lines=`tput lines` num_cols=`tput cols` echo "enter name: " read name length_name=`echo $name | wc -c ` length_name=`expr $length_name - 1` offset=`expr $length_name / 2` x_center=`expr $num_lines / 2` y_center=`expr $num_cols / 2` y_center=`expr $offset + $x_center` printf "%s = %d, %s = %d\n" "x" "$x_center" "y" "$y_center" echo -n "\033[$x_center;$y_centerf" $name
that last line looks if intended move cursor:
echo -n "\033[$x_center;$y_centerf" $name
however, not because fragment $y_centerf
not defined, , not end appropriate final character of control sequence. rather this, 1 can do
tput cup $x_center $y_center echo "$name"
the cup
means "cursor position", , can found in terminfo(5) manual page. cup
likewise can found in xterm control sequences. fragment indicated copied example using similar hvp
:
csi ps ; ps f horizontal , vertical position [row;column] (default = [1,1]) (hvp).
curly braces repair it, e.g., ${y_center}f
), (a) hvp
less common cup
, (b) using hard-coded escapes when tput
working problematic.
Comments
Post a Comment