tanakahdaのプログラマ手帳

プログラミングとかソフトウェア開発とかの備忘録

シェルスクリプトの事始め@bash

#!/bin/bash

# 作業ディレクトリ
readonly OBJECT_DIR="/Users/tanakahda/Desktop/test/*"

echo "
╭━━━┳━━━┳━━━┳━━━┳━╮╭━┳━━━╮ ╭╮╱╭╮╭╮╱╭╮
┃╭━╮┃╭━━┫╭━╮┣╮╭╮┃┃╰╯┃┃╭━━╯ ┃┃╱┃┣╯╰╮┃┃
┃╰━╯┃╰━━┫┃╱┃┃┃┃┃┃╭╮╭╮┃╰━━╮ ┃┃╱┃┣╮╭╋┫┃╭━━╮
┃╭╮╭┫╭━━┫╰━╯┃┃┃┃┃┃┃┃┃┃╭━━╯ ┃┃╱┃┃┃┃┣┫┃┃━━┫
┃┃┃╰┫╰━━┫╭━╮┣╯╰╯┃┃┃┃┃┃╰━━╮ ┃╰━╯┃┃╰┫┃╰╋━━┃
╰╯╰━┻━━━┻╯╱╰┻━━━┻╯╰╯╰┻━━━╯ ╰━━━╯╰━┻┻━┻━━╯
"

echo "
Please select a command no.
[?] Output the README of all students to one file: 1
[?] Do echo students who have not submitted a README: 2
"

###################################################
# 全生徒のREADMEをマージしてファイルに出力します
###################################################
exec_merge_file () {
  output=ALL_README-`date "+%Y%m%d%H%M%S"`.log
  
  for file in $(find . -type f -name 'README'); do
    echo "-----------------------------" >> $output
    ls -ltr $file >> $output
    cat "$file" >> $output
  done
}

##################################################
# 各生徒のREADMEを存在チェックし、無い人をechoします
##################################################
exec_exists_file () {
  
  directories=()
  for object in $OBJECT_DIR; do
    if [ -d $object ] ; then
      directories+=("$object")
    fi
  done
  for home_dir in ${directories[@]}; do
    if [ ! -f "${home_dir}/README" ]; then
      echo $home_dir
    fi
  done

}


##################################################
# main routine
##################################################
read command_no

case $command_no in
  "1")
    exec_merge_file
    ;;
  "2")
    exec_exists_file
    ;;
  *)
    echo "Please select a exist number.";;
esac