LoginSignup
11
10

More than 5 years have passed since last update.

getoptsとset -xのいい関係

Posted at

getoptsで引数にvのときに
set -xとしておくと良いかんじに
出力の詳細がわらわらでて、-v感を
お手軽に実現できて大変よろしい

getopts_x.sh
#!/bin/sh

usage(){
    cat << EOF 
$(basename $0) is useful script!
this is usage.
EOF
exit 2
}

while getopts hv OPT 
do
   case $OPT in
        h)  usage
            ;;  
        v)  set -x
            ;;  
        \?) usage
            ;;  
    esac
done

shift $((OPTIND - 1)) 

getopts周りの良い記事は
bash によるオプション解析

usage書くのも良い習慣になるので
使いやすいシェルスクリプトを書く

usageは習慣として man コマンドで出力される形式を真似ている
スクリプト見るのも使うのもLinux上だと思うので

11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10