Bash 스크립트에서 주석 달기
스크립트에서 다음 줄의 각 줄에 어떻게 주석을 달 수 있습니까?
cat ${MYSQLDUMP} | \
sed '1d' | \
tr ",;" "\n" | \
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' | \
tr "\n" "," | \
sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | \
sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
다음과 같은 주석을 추가하려고하면 :
cat ${MYSQLDUMP} | \ # Output MYSQLDUMP File
나는 얻다:
#: not found
여기에 의견을 제시 할 수 있습니까?
여기에는 약간의 오버 헤드가 있지만 기술적으로 귀하의 질문에 대답합니다.
echo abc `#Put your comment here` \
def `#Another chance for a comment` \
xyz, etc.
파이프 라인의 경우 오버 헤드가없는 깨끗한 솔루션이 있습니다.
echo abc | # Normal comment OK here
tr a-z A-Z | # Another normal comment OK here
sort | # The pipelines are automatically continued
uniq # Final comment
스택 오버플로 질문 여러 줄 명령에 줄 설명을 넣는 방법을 참조하십시오 .
후행 백 슬래시는 연속 명령으로 해석 되려면 줄의 마지막 문자 여야합니다. 그 후에는 주석이나 공백이 허용되지 않습니다.
명령 사이에 주석 줄을 넣을 수 있어야합니다
# output MYSQLDUMP file
cat ${MYSQLDUMP} | \
# simplify the line
sed '/created_at/d' | \
# create some newlines
tr ",;" "\n" | \
# use some sed magic
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' | \
# more magic
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' | \
# even more magic
sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' | \
tr "\n" "," | \
# I hate phone numbers in my output
sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | \
# one more sed call and then send it to the CSV file
sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
DigitalRoss가 지적했듯이, 선 woud가로 끝날 때 후행 백 슬래시는 필요하지 않습니다 |
. 다음 줄에 주석을 달 수 있습니다 |
.
cat ${MYSQLDUMP} | # Output MYSQLDUMP file
sed '1d' | # skip the top line
tr ",;" "\n" |
sed -e 's/[asbi]:[0-9]*[:]*//g' -e '/^[{}]/d' -e 's/""//g' -e '/^"{/d' |
sed -n -e '/^"/p' -e '/^print_value$/,/^option_id$/p' |
sed -e '/^option_id/d' -e '/^print_value/d' -e 's/^"\(.*\)"$/\1/' |
tr "\n" "," |
sed -e 's/,\([0-9]*-[0-9]*-[0-9]*\)/\n\1/g' -e 's/,$//' | # hate phone numbers
sed -e 's/^/"/g' -e 's/$/"/g' -e 's/,/","/g' >> ${CSV}
The backslash escapes the #, interpreting it as its literal character instead of a comment character.
$IFS
comment hacks
This hack uses parameter expansion on $IFS
, which is used to separate words in commands:
$ echo foo${IFS}bar
foo bar
Similarly:
$ echo foo${IFS#comment}bar
foo bar
Using this, you can put a comment on a command line with contination:
$ echo foo${IFS# Comment here} \
> bar
foo bar
but the comment will need to be before the \
continuation.
Note that parameter expansion is performed inside the comment:
$ ls file
ls: cannot access 'file': No such file or directory
$ echo foo${IFS# This command will create file: $(touch file)}bar
foo bar
$ ls file
file
Rare exception
The only rare case this fails is if $IFS
previously started with the exact text which is removed via the expansion (ie, after the #
character):
$ IFS=x
$ echo foo${IFS#y}bar
foo bar
$ echo foo${IFS#x}bar
foobar
Note the final foobar
has no space, illustrating the issue.
Since $IFS
contains only whitespace by default, it's extremely unlikely you'll run into this problem.
Credit to @pjh's comment which sparked off this answer.
In addition to the examples by DigitalRoss, here's another form that you can use if you prefer $()
instead of backticks `
echo abc $(: comment) \
def $(: comment) \
xyz
Of course, you can use the colon syntax with backticks as well:
echo abc `: comment` \
def `: comment` \
xyz
Additional Notes
The reason $(#comment)
doesn't work is because once it sees the #
, it treats the rest of the line as comments, including the closing parentheses: comment)
. So the parentheses is never closed.
Backticks parse differently and will detect the closing backtick even after a #
.
Here is a bash script that combines the ideas and idioms of several previous comments to provide, with examples, inline comments having the general form ${__+ <comment text>}
.
In particular
<comment text>
can be multi-line<comment text>
is not parameter-expanded- no subprocesses are spawned (so comments are efficient)
There is one restriction on the <comment text>
, namely, unbalanced braces '}'
and parentheses ')'
must be protected (i.e., '\}'
and '\)'
).
There is one requirement on the local bash environment:
- the parameter name
__
must be unset
Any other syntactically valid bash parameter-name will serve in place of __
, provided that the name has no set value.
An example script follows
# provide bash inline comments having the form
# <code> ${__+ <comment>} <code>
# <code> ${__+ <multiline
# comment>} <code>
# utility routines that obviate "useless use of cat"
function bashcat { printf '%s\n' "$(</dev/stdin)"; }
function scat { 1>&2 bashcat; exit 1; }
# ensure that '__' is unset && remains unset
[[ -z ${__+x} ]] && # if '__' is unset
declare -r __ || # then ensure that '__' remains unset
scat <<EOF # else exit with an error
Error: the parameter __='${__}' is set, hence the
comment-idiom '\${__+ <comment text>}' will fail
EOF
${__+ (example of inline comments)
------------------------------------------------
the following inline comment-idiom is supported
<code> ${__+ <comment>} <code>
<code> ${__+ <multiline
comment>} <code>
(advisory) the parameter '__' must NOT be set;
even the null declaration __='' will fail
(advisory) protect unbalanced delimiters \} and \)
(advisory) NO parameter-expansion of <comment>
(advisory) NO subprocesses are spawned
(advisory) a functionally equivalent idiom is
<code> `# <comment>` <code>
<code> `# <multiline
comment>` <code>
however each comment spawns a bash subprocess
that inelegantly requires ~1ms of computation
------------------------------------------------}
참고URL : https://stackoverflow.com/questions/1455988/commenting-in-a-bash-script
'IT story' 카테고리의 다른 글
로컬에서 실행되는 웹 페이지에서 로컬 파일에 대한 링크를 만들려면 어떻게해야합니까? (0) | 2020.06.14 |
---|---|
이 코드는 왜 버퍼 오버 플로우 공격에 취약합니까? (0) | 2020.06.13 |
입력 유형 =“파일”에서 기본 텍스트를 변경 하시겠습니까? (0) | 2020.06.13 |
C ++에서 문자열 앞의 'L'은 무엇을 의미합니까? (0) | 2020.06.13 |
백 스택에 추가 될 때 어떻게 프래그먼트 상태를 유지할 수 있습니까? (0) | 2020.06.13 |