[질문] awk 스크립트에서 함수 sub에 변수값이 제대로 들어가지 않습니다.

김일영의 이미지

아래와 같은 스크립트를 작성했는데, 의도한대로 동작하질 않네요... T.T

#!/usr/bin/sh
 
# target에는 "/myfs1/" 같은 문자열이 들어가게 된다.
target=`dirname $1``basename $1`'/'
 
find $target -type f -print |
while read; do
    awk '{ fulldir=$0; <span>sub(target, "/", fulldir);</span> print fulldir }'
# 위 awk 문의 의도는 아래 awk 문과 같은 동작을 하는 것이다. 아래 awk문은 원하는대로 동작한다.
#   awk '{ fulldir=$0; sub("/myfs1/", "/", fulldir); print fulldir }'
done

위의 스크립트의 의도는 이런 겁니다.

/myfs1 이 이렇다고 할 때

ls /myfs1
aaa bbb ccc

스크립트 파일명이 a.sh라고 하면

./a.sh /myfs1
/aaa
/bbb
/ccc

이렇게 /myfs1/aaa, /myfs1/bbb, /myfs1/ccc 에서 "/myfs1/" 부분을 "/"으로 치환해서 출력하고자 합니다.

더 좋은 방법이 분명 있겠지만 제가 셸 스크립트를 잘 몰라서 awk를 이용해서 저렇게 짰는데
sub라는 awk 함수가 마음먹은대로 작동을 안해주네요...
지금 a.sh를 둘려보면 저렇게 나오는게 아니라 이렇게 나옵니다:

./a.sh /myfs1
//myfs1/aaa
//myfs1/bbb
//myfs1/ccc

고수님들 부디 가르침을 주십시오...

pung96의 이미지

http://kldp.org/node/87708#comment-415887
이걸 말씀하시는 것이 맞는지 모르겠군요.
awk는 -v 옵션으로 변수를 미리 지정할 수도 있는 것 같던데...

김일영의 이미지

pung96님 조언 감사합니다만 잘 되지 않는군여...

awk의 { }은 셸이 아니라서 그런지 $0, $1,... 이런 것 외에는 $ 자체를 못쓰나 봅니다.
그리고 sub 함수의 실행 결과를 awk 문 외부에서 쓰는 것도 안되더군요.
셸은 아니지만 outer 셸의 변수에 값을 돌려줄 수 없는 것과 같은... 뭐 그런건가 봅니다.

말하자면 이런건 되지만

pulldir='txt'
awk '{ sub("tx", "aa", pulldir); print pulldir }'

이런건 안되는군요.

pulldir='txt'
awk '{ sub("tx", "aa", pulldir) }'
echo $pulldir

기가 막힌건 차라리 진짜 셸에서 outer 셸 변수값을 바꿨을때처럼
변수의 원래 값이 유지되기나 하면 이해라도 해줄텐데
이건 inner 셸 변수가 outer 셸에서 참조되는 것처럼 값이 unset되어버리네요.

그럼 뭐야? awk문 안에서 export가 되는 것도 아니고...
몇 안되는 awk문 함수만으로 나머지 로직을 다 짜라는거야?

awk ... 좋은 줄 알았더니... 배신입니다... 알고 보니 역시 오크스럽네요...

역시 외모 지상주의에 다 이유가 있다니깐여~ 역시 생긴게 이뻐야 성격도 좋다는;;;
오크!!! 너랑 안 놀아~~!!!

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.