[英]How to use a while loop in shell scripting to print on the same line
Hello i am trying to create a shell script in bash that will print a box with a height and width given by the user. 您好,我正在尝试用bash创建一个shell脚本,该脚本将打印一个具有用户指定高度和宽度的框。 so far my code looks like this 到目前为止,我的代码看起来像这样
#!/bin/bash
read height
read width
if [ $height -le 2 ];then
echo "error"
fi
if [ $width -le 2 ];then
echo "error"
fi
#this is where i need help
if [ $height -gt 1];then
if [ $width -gt 1];then
echo "+"
counter=$width
until [ $counter == 0 ]
do
echo "-"
let counter-=1
done
fi
fi
Currently it will print each "-" on a new line, how do i print them on the same line? 目前它将在新行上打印每个“-”,我如何在同一行上打印它们? Thank you 谢谢
Try using printf
instead: 尝试改用printf
:
printf "-"
To pass arguments during running script, using: 要在运行脚本期间传递参数,请使用:
$./shell-script-name.sh argument1 argument2 argument3
Then argument1
, argument2
and argument3
becomes $1
, $2
and $3
respectively inside your shell script. 然后,您的shell脚本中的argument1
, argument2
和argument3
变为$1
, $2
和$3
。
In your case: 在您的情况下:
#!/bin/bash
height=$1
width=$2
# ... The rest of the file ...
Less overhead than printf
is: echo -n "-"
比printf
少的开销是: echo -n "-"
Example: 例:
for f in {1..10} ; do echo -n - ; done ; echo
Output is 10 hyphens: 输出为10个连字符:
----------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.