BASH文本编辑与seq

huangapple go评论63阅读模式
英文:

BASH text edit with seq

问题

这样我就可以运行 callmyscrip.sh 100,它将打印由seq生成的100行内容,但是为了可读性,如何最好地将内容 TEXT="xxx yyy ${this}" 与变量分开呢?

#!/bin/bash
howmanytimes=$1
for this in $(seq -w ${howmanytimes}); do
    echo " / -- ${this}"
done

以下的写法不会起作用,因为 $this 没有被替换:

#!/bin/bash
howmanytimes=$1
TEXT="THIS WOULD NOT WORK: ${this}"
for this in $(seq -w ${howmanytimes}); do
    echo ${TEXT}
done
export $TEXT
英文:

With this I can callmyscrip.sh 100 and this will print 100 rows with the content generated by seq, but what's the best way to separate the content TEXT="xxx yyy ${this}" for readability with a variable?

#!/bin/bash
howmanytimes=$1
for this in $(seq -w ${howmanytimes}); do echo " /
-- ${this}

"; done

this instead would not work as $this isn't replaced:

#!/bin/bash
howmanytimes=$1
TEXT="THIS WOULD NOT WORK: ${this}"
for this in $(seq -w ${howmanytimes}); do echo ${TEXT} ; done
export $TEXT

答案1

得分: 1

seq(1) 不标准、低效且无用。

参考http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Conditional_Loops

对于 [tag:ksh]:

#!/bin/ksh

txt='this should work with int: '
for i in {0..$1}; do
    echo "$txt $i"
done

对于 [tag:bash]:

#!/bin/bash

txt='this should work with int: '
for ((i=0; i<=$1; i++)) {
    echo "$txt $i"
}
英文:

seq(1) is nonstandard, inefficient and useless.

Check http://mywiki.wooledge.org/BashGuide/TestsAndConditionals#Conditional_Loops

With [tag:ksh]:

#!/bin/ksh

txt=&#39;this should work with int: &#39;
for i in {0..$1}; do
    echo &quot;$txt $i&quot;
done

With [tag:bash]:

#!/bin/bash

txt=&#39;this should work with int: &#39;
for ((i=0; i&lt;=$1; i++)) {
    echo &quot;$txt $i&quot;
}

答案2

得分: 1

你可以将动态文本包装在一个bash函数中:

#!/bin/bash

get_content() {
    echo "这将起作用:$1"
}

how_many_times=$1

for i in $(seq -w ${how_many_times}); do
    echo "$(get_content $i)"
done

如果你只需要输出内容,可以简化如下:

#!/bin/bash

get_content() {
    echo "这将起作用:$1"
}

how_many_times=$1

for i in $(seq -w ${how_many_times}); do
    get_content $i
done
英文:

You can wrap your dynamic text in a bash function:

#!/bin/bash

get_content() {
	echo &quot;THIS WOULD WORK: $1&quot;
}

how_many_times=$1

for i in $(seq -w ${how_many_times}); do
	echo &quot;$(get_content $i)&quot;
done

If you just need to output the content, can simplify it like this:

#!/bin/bash

get_content() {
	echo &quot;THIS WOULD WORK: $1&quot;
}

how_many_times=$1

for i in $(seq -w ${how_many_times}); do
	get_content $i
done

答案3

得分: 1

你可以直接使用 printf,跳过循环部分:

#!/bin/bash
howmanytimes=$1
text="This WILL work: %s"
printf "${text}\n" $(seq -w ${howmanytimes})

请注意,需要在格式字符串中添加 \n,因为 printf 不会自动添加换行符,就像 echo 会自动添加一样。如果你想要额外的换行符(就像示例中一样),你可以将它们添加为\n或实际的换行符,无论是在格式变量中还是在 printf 参数中使用它们。另外,如果你想在字符串中包含字面上的反斜杠或百分号,需要将它们双写(例如,%% 打印 %,或 \\ 打印 \)。

顺便说一下,由于 printf 是 Bash 内置命令,所以它不受正常的参数列表长度限制的影响,因此即使有很多数字,这也会起作用。

英文:

You can use printf directly, and skip the loop entirely:

#!/bin/bash
howmanytimes=$1
text=&quot;This WILL work: %s&quot;
printf &quot;${text}\n&quot; $(seq -w ${howmanytimes})

Note that \n needs to be added to the format string, since printf doesn't add a newline automatically like echo does. If you want additional newlines (like in the example), you can add them as either \n or actual newlines, in either the format variable or where it's used in the printf argument. Also, if you want to include a literal backslash or percent sign in the string, double it (i.e. %% to print %, or \\ to print \).

BTW, since printf is a bash builtin, it's not subject to the normal argument list length limits, so this'll work even with very large numbers of numbers.

答案4

得分: 0

请注意,以下是您提供的内容的翻译部分:

  • "Check your script with shellcheck." 翻译为 "使用 shellcheck 检查您的脚本。"
  • "printf is a simple template language." 翻译为 "printf 是一种简单的模板语言。"
  • "You could use envsubst to replace environment, however in this case printf looks way clearer." 翻译为 "您可以使用 envsubst 来替换环境变量,然而在这种情况下,printf 看起来更清晰。"
  • "Research quoting in shell." 翻译为 "研究在 shell 中引用。"
英文:

Check your script with shellcheck. printf is a simple template language. I could see:

#!/bin/bash
howmanytimes=$1
text=&quot;THIS WOULD WORK: %s&quot;
for this in $(seq -w &quot;${howmanytimes}&quot;); do
   printf &quot;$text&quot; &quot;$this&quot;
done

You could use envsubst to replace environment, however in this case printf looks way clearer. Research quoting in shell.

#!/bin/bash
howmanytimes=$1
text=&#39;THIS WOULD WORK: ${THIS}&#39;
for this in $(seq -w &quot;${howmanytimes}&quot;); do
   THIS=&quot;$this&quot; envsubst &lt;&lt;&lt;&quot;$text&quot;
done

huangapple
  • 本文由 发表于 2023年2月16日 15:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定