쉘프로그래밍으로 기본적인 프로그램을 하는데 오류가 생깁니다.

region의 이미지

명령어중 du -a라는 명령어가 있습니다.
현재 있는 diskfile의 크기를 나타내주는 명령어입니다.
이걸 받아서 diskfile의 토탈값을 출력해줘야하는 sh프로그램을 작성해야합니다.
현재 제가 작성한 파일은
set $(du -a)
count=1;
sum=0;
max=$(($#-1))
while [ "$count" -lt "$max" ]
do
eval temp='$'$count
if [ "$(($count % 2))" = "1" ]
then
sum=$(($sum + $temp))
fi
count=$(($count+1))
done
echo "현재 diskfile의 크기는 $sum"

expr 계산식이 아는 $((계산식))을 사용했습니다.
혹시 $(())로 안되고 expr로만 해야하나요? (expr로도 해봤습니다)
이렇게 프로그램을 짯는데 안됩니다.
왜 안되는지 도무지 모르겠습니다.
set $(du -a)로 du -a내에 출력되는 값?들을 받아왔고
max에는 $#를 이용해서 전체 매개변수의 갯수를 가져오고 -1을 한 이유는 마지막 매개변수에는 총합이 있는데 이를 제거해야하기때문에 -1을 했습니다.

그리고 while문에서는 카운트의 값과 맥스의 값을 비교해줬구요.
eval을 써서 $count(숫자가 계속 증가함)가 직접적으로 가리키는 값을 temp에 저장하도록 했습니다.
eval을 썻기 때문에 $count가 직접적으로 가리키는 값은 증가된 카운트값인 1,2,3,4 이런식이 아니라 $1, $2, $3가 가지고 있는 실질적인 값이라고 생각하고 있는데 이게 맞는지 잘 모르겠습니다.

그리고 if문에서는 du -a명령어를 보면 홀수번매개변수에는 숫자가 저장되어 있고 짝수변 매개변수에는 문자가 저장되어있기때문에 숫자만 저장해서 합치기 위하여 count%2가 1일 경우에는 실행토록 했습니다.
sum은 당연히 합치는 값이구요.

이게 도대체 왜 안돌아가는지 궁금합니다. 제가 생각하기에는 맞는데말이죠..ㅠㅠ

neocoin의 이미지

du -s target_fila_path | awk '{print $1}'

bushi의 이미지

bash 2.0 이상.
tcsh 의 것을 많이 많이.

((..)) 는 산술연산에 사용하고
$(...) 는 명령(함수말고) 실행에 사용합니다.
간접참조는 foo=${!bar} 처럼.

((count++))
let "count++"
count=$(expr $count + 1)

if [ $count -lt 1 ]; then
fi
 
if ((count < 1)); then
fi

count=1;
ref=count;
eval echo \$$ref
echo ${!ref}

아직 배열에 대한 간접참조는 되지 않습니다. 원본 배열의 맨 첫번째 요소만 가져오더군요.

OTL

댓글 달기

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