问题很简单。Bash中的空白连接
for i in `seq $begin $progress_length` ; do
progress_bar=$progress_bar'#'
done
for i in `seq $middle $end` ; do
empty_space=$empty_space' '
done
我需要empty_space
来定位进度条后的内容。我期望它会是x空白字符串。但最后字符串是空的。我如何创建x空白字符串?
问题很简单。Bash中的空白连接
for i in `seq $begin $progress_length` ; do
progress_bar=$progress_bar'#'
done
for i in `seq $middle $end` ; do
empty_space=$empty_space' '
done
我需要empty_space
来定位进度条后的内容。我期望它会是x空白字符串。但最后字符串是空的。我如何创建x空白字符串?
问题可能是因为$empty_space
只有空格。然后,将它们输出,你必须围绕着它在双引号:
echo "${empty_space}some_other_thing"
你可以尝试用printf
例如获得数个空间更有趣的输出。例如,要写20个空格:
v=`printf '%20s' ' '`
可以使用参数替换创建字符串。 取代$ {STR:偏移量:长度}返回str的子字符串:
space80=' '
hash80='################################################################################'
progress_bar=${hash80:0:$progress_length-$begin+1}
empty_space=${space80:0:$end-$middle+1}
echo -n "$empty_space$progress_bar"
我理解你的问题,因为我有完全一样的。
我的解决办法来连接一个临时的字符,而不是一个空白的,比方说☼
,然后,到了最后,在一个空格与sed
更换他们所有出现:
echo $myString | sed 's/☼/ /g'
我希望它会帮你 !
..并且进度条'#'也可以用'printf(1)'写入,可能会简化这两个例程。 – sarnold
谢谢你的回答。我之前尝试过“$ {empty_space}”,但它不起作用。事件empty_space ='printf'%20s''''不起作用:(。bash很奇怪。 – ciembor
@ciembor:你是什么意思,'empty_space =“$ {empty_space}”'不起作用?它追加一个空间到'empty_space'。你以后如何检查这个值?推测是将它打印到屏幕上?然后你是在引用它吗?也就是说,你需要执行'echo {$ {progress_bar} $ {empty_space}]''' ,'echo {$ {progress_bar} $ {empty_space}]' – Cascabel