Cshell에서 sed 치환 시, $를 escaping하는 방법

stock9343의 이미지

우선 cshell에서
sed치환을 사용하여 특정문자열을
새로운 파일에 넣는 중인데
illegal error가 발생되어 확인 부탁드립니다.

sed -e "s:#ends:\$test:g" ./aaa

aaa라는 파일에서 #ends라는 문자열이 보이면
이 문자열을 $test 문자그대로 변환하는 구문입니다.
"" 내부에서 $test를 변수치환으로 인식하지 않기
위해서 $앞에 \를 넣어 escaping시켰음에도
불구하고
illegal error가 발생되는데
원인을 찾지 못하겠네요.
조언 좀 부탁드립니다

ymir의 이미지

sed 구문에서는 별 문제 없어 보였는데..
csh 에서는 변수 escaping 과 따옴표가 조금 다르게 동작하네요.

% cat a
-map 1 -map 2 -map 3
#ends
% set test=foo
% echo $test
foo
% echo '$test'
$test
% echo "$test"
foo
% echo \$test
$test
% echo "\$test"
\foo
% echo '\$test'
\$test
% sed -e "s:#ends:\$test:g" ./a
Bad : modifier in $ ("").
% sed -e s:#ends:\$test:g ./a
-map 1 -map 2 -map 3
$test
% sed -e s:#ends:$test:g ./a
Unknown variable modifier.
% sed -e "s:#ends:$test:g" ./a
Unknown variable modifier.
% sed -e s:#ends:"$test":g ./a
-map 1 -map 2 -map 3
foo

csh 거의 20년 전 까지 쓰다가, 이후에 sh 과 bash 로 갈아타서 기억이 가물가물 하네요.
특별한 이유가 있는게 아니라면, sh 나 bash 에 익숙해 지는게 좀 더 활용도가 높을 것 같습니다.
구닥다리 unix 시스템이라도 bourne shell 없는 시스템은 없으니 범용성도 높고, 이걸 익혀두면 바로 bash 에도 적응 가능합니다.
csh 을 썼을 때에도 스크립트 자체는 계속 bourne shell 로 만들다 보니, 굳이 csh 을 쓸 이유가 없어지더군요.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

stock9343의 이미지

덕분에 쉘스크립트 지식 및 노하우를
많이 공부 및 배우는것 같습니다.
제가 근무하는 영역이 반도체 개발쪽이고, 리눅스 환경에서
자동화시킬때, 대부분 cshell로 해서 많이 짜셔서,
저도 가급적 cshell기반으로 스크립트를
짤려고 노력하고 있습니다.
bash 쉘도 가끔씩 하긴 하는데,
우선 업무적으로는 cshell을 많이 이용해야 되는 상황이라서 ㅎㅎㅎ
암튼 감사합니다

chanik의 이미지

     After the input line is aliased and parsed, and before each command is
     executed, variable substitution is performed, keyed by ‘$’ characters.
     This expansion can be prevented by preceding the ‘$’ with a ‘\’ except
     within double quotes (`"'), where it always occurs, and within single
     quotes (`''), where it never occurs.

csh 매뉴얼에서 발췌한 내용입니다. \$var 식으로 \를 $ 앞에 붙여서 변수치환을 막을 수 있기는 한데, 예외적으로 " ", ' ' 안에서는 할 필요 없는 모양입니다. " " 안에서는 변수치환이 무조건 일어나고 ' ' 안에서는 절대 일어나지 않기 때문이라는 말 같네요.

뭔가 융통성이 없는 느낌이긴 한데, 목적을 이루려면 아래와 같이 ' '로 감싸면 되겠습니다.

sed -e 's:#ends:$test:g' test.txt
stock9343의 이미지

""안에서는 무조건 변수치환이
일어나고, 반대로 ''안에서는
변수치환이 일어나지 않지만
변수치환을 일어나게 하고 싶을때에는
또 동작이 안되고 이러네요 ㅎㅎ
csh이 뭔가 깔끔하게 안되나 보네요.

set list = "-map 1 -map 2 -map 3"
이 있을때

sed -e 's:#end:#end $test '$list':g'
즉 $test는 변수치환없이 뽑고,
반대로 $list는 변수치환을 시켜서 뽑아야 되는데
이럴때에는
s unterminated error가 뜨고 하네요.
암튼 감사합니다
csh의 한계인가보네요 ㅎㅎ

ymir의 이미지

위에 제가 테스트 한 부분을 잘 살펴 보세요.
형태는 달라도 동일한 결과를 얻을 수 있다는 걸 보실 수 있을 겁니다.
space 를 escape 시키면, 굳이 따옴표로 묶지 않아도 됩니다.

% cat a
foo
#end
% set list = "-map 1 -map 2 -map 3"
% sed -e s:#end:#end\ \$test\ "$list":g ./a
foo
#end $test -map 1 -map 2 -map 3

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

ymir의 이미지

추가로.. 좀 지저분하긴 하지만, 단계별로 끊어서 처리하는 것도 하나의 방법이 될 수 있을 것 같습니다.

% cat a
foo
#end
% sed -e 's:#end:#end $test:g' -e "s:\(#end.*\):\1 ${list}:g" a
foo
#end $test -map 1 -map 2 -map 3

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

stock9343의 이미지

이제 csh환경에서 sed 치환할때
확실히 알겠네요.
""든 ''든 묶지 않은 상태에서
변수치환을 시켜야 되는 부분은
""로 묶고, 그대로 뽑을 때에는
//로 escape시키네요.
추가적으로 sed를 두번하여
분리시켜서 적용하는 방법까지.

저도 테스트해보니 잘됩니다.
감사합니다

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.