쉘스트립트의 for 문을 이용하여 iptables 적용을 하려합니다.

vlzkcbcb의 이미지

AAA는 IP 정보가 담긴 파일이고 여기에 iptables 적용을 위한 옵션을 포함시켜놨구요.
script.sh iptables 룰 적용을 위한 스크립트파일.
debug.txt 는 왜 적용이안되는지 알아보기위해 디버그파일입니다. (script.sh 에서 생성하게 했어요)

----------------------------------------------------------
debian:/home# cat AAA
-s 1.1.1.1
-s 2.2.2.2
-s 3.3.3.3
-s 100.100.100.1
-s 100.100.100.0/24
-m mac --mac-source AA:AA:AA:AA:AA:AA
-m iprange --src-range 100.100.100.1-100.100.100.5
----------------------------------------------------------
debian:/home# cat script.sh
#!/bin/sh
for AAA in `cat AAA`
do
echo -e $AAA >> debug.txt
/sbin/iptables -A FORWARD $AAA -j ACCEPT
done
----------------------------------------------------------
debian:/home# cat debug.txt
-s
1.1.1.1
-s
2.2.2.2
-s
3.3.3.3
-s
100.100.100.1
-s
100.100.100.0/24
-m
mac
--mac-source
AA:AA:AA:AA:AA:AA
-m
iprange
--src-range
100.100.100.1-100.100.100.5
----------------------------------------------------------

위에서 debug.txt 결과처럼

AAA 파일 내용에서 띄어쓰기 마다 줄이 바뀌는것 같습니다 ㅡㅡ;

어떻게 해결방법이 없을까요?

mrjh76의 이미지

for 문을 쓰는것보다는, 방법을 달리하여, 파일을 줄단위로 처리하는게 좋지 않을까요?

답글 달고나니, 아래 비슷한 답글이 달렸네요!
그런데,, read 를 저렇게 쓸수도 있다는걸 처음 알았네요! 후후...

!/bin/sh
 
LINE=0
 
while [ `wc -l aaa | awk {'printf $1'}` -gt $LINE ]
do
        let LINE=LINE+1
        sed -n "$LINE"p aaa
done
ymir의 이미지

for AAA in `cat AAA` 에서..

AAA 라는 변수에는 cat AAA 결과중에서 white space 로 구분된 토큰이 들어갑니다.

아래처럼 그냥 while 문으로 바꾸시면 될 것 같네요.

#!/bin/sh
while read -r AAA
do
  echo -e $AAA >> debug.txt
  /sbin/iptables -A FORWARD $AAA -j ACCEPT
done < AAA

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

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

댓글 달기

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