shell 명령어 실행. 직접실행과 파일로 읽어서 실행의 차이

eternityova의 이미지

test.txt파일에는
다음과 같은 명령어가 있습니다
tgjob -s suite deliverychecksuite/RBSNC_MSRBS_lrat_DC.xml ica-0.0.103 rcmg2-0.5.139 rcm_options " -discleansftp " custom_gte CXC2010601_1-R9A198 cio custom_bundle CXC2010602_1-R9A198 ctx_ver CXC1738272_1-R10A02 rcm_g2_stnd gwl lrat mje testng rcm_g2_stnd gwl passthrough " -Dlog4j.configuration=log4j.properties -Dg2nodeci.track=main -Dg2nodeci.test_level=bdc -Dmsran.jcat.listener+=com.ericsson.msran.g2.listeners.EsiListener -Xms700m -Xmx1024m " maxtime 150 p9 force_esi_check

#!/bin/ksh

readfile=$(<"test.txt")
test2=$($readfile)

echo $test2

test=$(tgjob -s suite deliverychecksuite/RBSNC_MSRBS_lrat_DC.xml ica-0.0.103 rcmg2-0.5.139 rcm_options " -discleansftp " custom_gte CXC2010601_1-R9A198 cio custom_bundle CXC2010602_1-R9A198 ctx_ver CXC1738272_1-R10A02 rcm_g2_stnd gwl lrat mje testng rcm_g2_stnd gwl passthrough " -Dlog4j.configuration=log4j.properties -Dg2nodeci.track=main -Dg2nodeci.test_level=bdc -Dmsran.jcat.listener+=com.ericsson.msran.g2.listeners.EsiListener -Xms700m -Xmx1024m " maxtime 150 p9 force_esi_check)

echo $test

exit 0

test에 명령어를 직접입력하여 실행하면 실행이 잘 됩니다
그러나 파일로 읽어온 readfile을 test2로 실행하면 실행이 되지 않는데
이유를 모르겠습니다
Error: unknown argument -discleansftp

처음에는 " 때문인줄알았는데 이것도 아닌거 같고
고수님들의 의견 부탁드립니다.

swish95의 이미지

에러에 답이 있네요
echo $readfile 해보세요

------------------------------------------------------------
ProgrammingHolic

eternityova의 이미지

echo $readfile 은 잘 됩니다
본문 코드에 test2와 test에는 같은값이 들어가는거 같은데
왜 test2만 ERROR가 나는지 모르겠습니다

echo $readfile 결과값
tgjob -s suite deliverychecksuite/RBSNC_MSRBS_lrat_DC.xml ica-0.0.103 rcmg2-0.5.139 rcm_options " -discleansftp " custom_gte CXC2010601_1-R9A198 cio custom_bundle CXC2010602_1-R9A198 ctx_ver CXC1738272_1-R10A02 rcm_g2_stnd gwl lrat mje testng jhook postinstall=RBSNC/configuration/RBSNC_MSRBS_ConfigSectorCheck.xml rcm_g2_stnd gwl passthrough " -Dlog4j.configuration=log4j.properties -Dg2nodeci.track=main -Dg2nodeci.test_level=bdc -Dmsran.jcat.listener+=com.ericsson.msran.g2.listeners.EsiListener -Xms700m -Xmx1024m " maxtime 150 p9 force_esi_check

ymir의 이미지

test.txt 에 rcm_options " -discleansftp " 와 같이 중간에 따옴표가 있는데..
그걸 그냥 읽어 들이면 따옴표도 escaping 되어 개별 문자로 취급됩니다.
게다가 따옴표 안에 공백도 있기 때문에..
실행했을 때 공백 기준으로 모두 개별 argument 로 분리 전달되어 문제가 생긴거죠.

이런 경우에는 test2=$($readfile) 대신 test2=$(eval $readfile) 과 같이..
eval 을 한 번 써주면 됩니다.

$ cat run.sh
#!/bin/bash
 
until [ -z "$1" ] ; do
        echo "> $1"
        shift
done
 
$ bash run.sh foo " bar"
> foo
>  bar
$ bash run.sh foo \" bar\"
> foo
> "
> bar"
$ eval bash run.sh foo \" bar\"
> foo
>  bar

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

swish95의 이미지

좋은거 배우고 갑니다. ^^

------------------------------------------------------------
ProgrammingHolic

eternityova의 이미지

답변 감사합니다
eval에 대해서 공부를 해봐야겠네요

댓글 달기

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