函数在linux中均匀地隔开注释
您好,我正在linux中编写一个自定义函数,该函数设计为在注释指示符后放置四个空格。我不熟悉在linux中编写代码,我更擅长阅读而不是写出代码。注释用符号表示;性格到目前为止,这是我在草稿中的内容。这是使用putty作为bash仿真器的基本linux函数在linux中均匀地隔开注释,linux,Linux,您好,我正在linux中编写一个自定义函数,该函数设计为在注释指示符后放置四个空格。我不熟悉在linux中编写代码,我更擅长阅读而不是写出代码。注释用符号表示;性格到目前为止,这是我在草稿中的内容。这是使用putty作为bash仿真器的基本linux commentPlacer() { typeset -i count1=0 #iterator for first loop typeset -i count2=0 #iterator for secod loop len= ${#$1}
commentPlacer()
{
typeset -i count1=0 #iterator for first loop
typeset -i count2=0 #iterator for secod loop
len= ${#$1} #length of argumnent
comment=";"
space= " "
comIndex=${$1#/;/} #index of the comment
commentSpace= "; " #the comment indicator with the proper spacing
for(( count1; count1 <= len; count1++ )) #loop to check if there is a comment on the line
if [[ $1[count] == comment ]]
for (( count2; count2 < $1[count1]; count2++ ))
if [[ $1[count2] != commentSpace ]] #if the line doesn't have enough spacing in the comment use commentSpace variable
then echo ${{$1:0:comIndex - 1} + commentSpace + {$1:commentSpace + 1: -1}} #cut off line before comment indicator and replace the line with the proper spacing.
fi
done
fi
done
}
将其重新格式化为如下所示:
x="example" ;This line is a comment.
假设这是在bash shell脚本中
两个for循环都缺少do,第一个if语句也缺少。此外,bash没有将字符串与+连接起来,一些变量扩展似乎缺少$,并且大多数扩展都是不带引号的。并且不能将$varname[index]索引为字符串
sed也可以做同样的事情:
如果您真的想将其作为shell函数编写,我强烈建议您遵循适当的开发方法,并在每次更改后测试代码。您还可以使用该网站检查代码的语法。您不知道问题中的不完整代码是用哪种语言编写的。我猜它是或是其他POSIX sh 然后仔细阅读下面的内容。您至少应该编写代码
for (( count1; count1 <= len; count1++ )) ; do
# more code here
done
但也请参见;您可以使用[$1[count]-eq注释]
当然,应该正确调用CommentPlacerShell函数,可能使用with
评论哪种语言?你可以使用一些压头,比如或。顺便说一句,你没有显示任何。因此,请编辑您的问题以改进它。另外,大多数的,比如,可以缩进代码我已经按照你的要求做了谢谢你的时间。即使是编辑,你也没有给出任何,也没有真正解释你想要打印的语言的语法。请原谅,这是我在linux上的第一个函数。我真的不知道我在做什么抱歉让你失望了。这个要求在StackOverflow上是强制性的,与Linux无关。无论您在这里提出什么问题,您都需要向读者提供足够的信息和上下文,以便在他们的机器上重现您的问题再次向您提问这是我第一次涉足linux。我宁愿学习如何正确地书写,也不愿使用“sed”。@BrianMoore我认为使用正确的工具来完成工作就是正确地解决问题。这是真的@Kusalanda,如果你不知道如何使用该工具或它是如何工作的,你如何知道使用哪种工具以及何时使用?@BrianMoore好吧,你问了一个问题,这是很好的第一步,如果你愿意学习,你可以尝试给你的建议。没有必要把事情复杂化。你是对的,因为现在我很高兴我理解了if语句和for循环的语法,我有更多的问题,但现在这对我来说是巨大的,谢谢你的时间和耐心。
sed 's/;[[:blank:]]*/; /' file
for (( count1; count1 <= len; count1++ )) ; do
# more code here
done
if [[ $1[count] == comment ]]; then
# more code here
fi
commentPlacer 'x="example"' ';This line is a comment'.