網頁

2017年12月22日 星期五

UNIX入門課程1061_ex10

 http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html

作業10: awk programming, associative array
如下例所示, 計算每個人的總和.
$ cat IN-7
# sample input file of exercise 7
                    # this is an blank line.
1234 Alice          # Alice wins 1234
999  Bob            # Bob wins 999
Alice 200           # Alice spends 200
Bob 100 Alice       # Bob loses 100 to Alice
4000 Bob 250 Carol  # Bod wins 4000 and loses 250 to Carol
3000 Carol 300      # Carol wins 3000 and spends 300

$ ./ex10.awk IN-7 Carol has 2950 Bob has 4649 Alice has 1134
$

解答:

#!/usr/bin/awk -f
BEGIN {
}
{
  sub(/#.*/,"") # change #.*  to ""
  for (i=1; i<=NF; i++) {
    if ($i ~ /^[0-9]/) {   #-----$1 is number
      money=$i
      if (i==2 || i==3) {  #-----money is spends or loses
        name=$(i-1)
        array[name]=array[name]-money
      }
    } else {               #-----$1 is name
      name=$i
      money=$(i-1)
      array[name]=array[name]+money
    }
  } #for END
}
 
END {
  for (name in array)
    printf("%s has %s\n", name, array[name])
}

執行結果:



2017年12月15日 星期五

UNIX入門課程1061_ex9

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html
作業九: listing a directory recursively
•建立一個 shell 執行檔, 名為 showdir, 可印出 類似用 tree -F 所得的結果.
若檔案為一目錄, 則在檔名後加 '/'
若檔案為一 symbolic link, 用箭頭顯示. 如上圖所示.
若目錄無法讀取內容, 則略過其以下的內容, 即只顯示目錄名字.
請勿用 find, du 或其他指令先得到一個 tree.


$./showdir /var/tmp/exer9-test-dir
exer9-test-dir/
|---city/
|   |---lapula --> ../sky/temple/utopia
|   `---rome
|---dogs/
`---sky/
    |---birds
    |---mountains
    `---temple/
        |---asking
        `---utopia

$ tree -F /var/tmp/exer9-test-dir
/var/tmp/exer9-test-dir
|-- city/
|   |-- lapula -> ../sky/temple/utopia
|   `-- rome
|-- dogs/
`-- sky/
    |-- birds
    |-- mountains
    `-- temple/
        |-- asking
        `-- utopia
     
4 directories, 6 files
$


解答:
#!/bin/bash
olddir=$PWD
dirdepth=0
function is_last_file {
  local flag
  let flag=${ary[dirdepth]}-1
    if [ $flag -eq 0 ]; then #last file
      ch="\\"  #as \---
    else
      ch="|"   #as |---
    fi
}
function print_file {
  is_last_file #check this file is last?
  case "$1" in
      -D) printf "$ch---$2/\n"
          ;; #file is directory
      -L) printf "$ch---$file --> "
          ls -l $file | awk '{ print $11 }'
          ;; #file is link
      -F) printf "$ch---$file\n" #normal file
          ;; #file is normal file
  esac
  let ary[dirdepth]=ary[dirdepth]-1
}
function check_file_type {
  if [ $dirdepth -gt 0 ]; then  
          for ((i=0; i<=dirdepth-1; i++)); do
            if [ ${ary[i]} -ne 0 ]; then
              printf "|   " #print | & 3 space
            else
              printf "    " #print 4 space
            fi
          done # for END
  fi
  if [ -d "$file" ]; then 
      print_file -D $file #directory  
  else          
      if [ -L $file ]; then
          print_file -L $file #symbolic link
      else 
          print_file -F $file #normal file
      fi
  fi
}
function listfiles {
  cd "$1";
    f_count=`ls -l | wc -l`  #calc total files 
    let f_count=f_count-1
    ary[dirdepth]=$f_count

  if [ $f_count -ne 0 ]; then # if dir is empty then exit
    for file in *; do
      check_file_type $file #call sub
      if [ -d "$file" ]; then
          let dirdepth=$dirdepth+1
          listfiles "$file"
          cd ..;
      fi
    done #for END
  fi
  let dirdepth=$dirdepth-1
}
#-----Main Program 2017/12/06 update-----
#d=$1
#echo "${d##*/}/"
echo "$(basename "$1")/"
listfiles "$1"
 
執行結果:




2017年12月9日 星期六

UNIX入門課程1061_ex8

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html

作業八: hanoi tower
•建立一個 shell 執行檔, 名為 hanoi, 可印出 hanoi tower problem 中 disc 移動的順序

$ ./hanoi 2    # move 2 discs, from A to B by C
from A to C
from A to B
from C to B
$ ./hanoi 3    # move 3 discs, from A to B by C
from A to B
from A to C
from B to C
from A to B
from C to A
from C to B
from A to B
$

解答:

#!/bin/bash
function move_disc {
  if [ $1 -ne 0 ]; then
    move_disc $(($1-1)) $2 $4 $3
      echo from $2 "to" $3
    move_disc $(($1-1)) $4 $3 $2
  fi
}
#-----Main Program-----
disc=$1
if [ $disc -gt 0 ]; then
  move_disc $disc A B C
fi

執行結果:


 

2017年12月2日 星期六

UNIX入門課程1061_ex7

http://erdos.csie.ncnu.edu.tw/~klim/unix-intro/unix-intro-1061.html

作業七: range filter with sh
•建立一個 shell 執行檔, 名為 range, 其使用方法與上題類似, 但參數可能有二個, 可濾出上下限中間的數.
•從 stdin 讀入數字, 一行內的數可能超過一個
$ cat Numbers
6
7
8
9
10
11
12
13
14
129
37
8
119
$ cat Numbers | ./range 10 60
10
11
12
13
14
37
$ cat Numbers | ./range - 60
6
7
8
9
10
11
12
13
14
37
8
$ cat Numbers | ./range 200
$ cat Numbers | ./range 120
129
$ cat Numbers | ./range
6
7
8
9
10
11
12
13
14
129
37
8
119
$

解答:
#!/bin/bash
function Filter_Number() {
  local opt=$1
  local input_num=$2
  case "$opt" in 
    0)echo $input_num
      ;;
    1)if [ $input_num -ge $range_s ];then
        echo $input_num fi
      ;;
    2)if [ $range_s = "-" ]; then
        if [ $num -le $range_e ]; then
          echo $input_num
        fi 
      elif [ $input_num -ge $range_s -a $input_num -le $range_e ];then
        echo $input_num
      fi
      ;;
    esac
}

#-----main program-----
argc=$#
#echo "arg have:$argc"
case "$argc" in
  0);;
  1)range_s=$1
    ;;
  2)range_s=$1 range_e=$2
    ;;
esac
while read data
do
  #set $(echo $data)
  for num in $(echo $data)
    do
      #echo $num
      Filter_Number $argc $num
    done
done

執行結果: