テスト実行および実行オプションを含むBash機能

テスト実行および実行オプションを含むBash機能

オプションとオプションrsyncで動作する実行用のbash関数を作成しています。いくつかの重要な改善と追加の検査が必要です。--dry-run--exec

--dry-runユーザー仕様の場合、おそらくあまり意味がありません--exec。とにかく --dry-run を使います。

次の設定が開始されました。

次の電話

filetr-archive -n -s bkscan -d temp

コマンドの実行

rsync -av --progress --dry-run --log-file=temp/bkscan.log bkscan temp

これにより、temp/bkscan.logファイルの内容が

2021/07/19 23:55:15 [19947] building file list
2021/07/19 23:55:15 [19947] cd+++++++++ bkscan/
2021/07/19 23:55:15 [19947] sent 273 bytes  received 56 bytes  658.00 bytes/sec
2021/07/19 23:55:15 [19947] total size is 108,837,826  speedup is 330,814.06 (DRY RUN)

ただし、rsync次も作成されますが保存されません。rsync端末に表示される出力は便利ですか?情報を保存することもできます。しかし、どのようにできるか知りたいです。

  bkscan temp
sending incremental file list
bkscan/
bkscan/BkScan.aux
bkscan/BkScan.cp
bkscan/BkScan.fn
bkscan/BkScan.ky
bkscan/BkScan.log
bkscan/BkScan.pdf
bkscan/BkScan.pg
bkscan/BkScan.texi
bkscan/BkScan.texi~
bkscan/BkScan.tp
bkscan/BkScan.vr
bkscan/BookScan.texi~

sent 273 bytes  received 56 bytes  658.00 bytes/sec
total size is 108,837,826  speedup is 330,814.06 (DRY RUN)

filetr-archive ()
{
  # Process command line options
  shortopts="nel:s:d:"
  longopts="dry-run,exec,log:,source:,destin:"
  opts=$(getopt -o "$shortopts" -l "$longopts"  \
        -n "$(basename $0)" -- "$@")
  if [ $? -eq 0 ]; then
    eval "set -- ${opts}"
    while [ $# -gt 0 ]; do
      case "$1" in
      -n|--dry-run)
        shift 1
        local -r dryrun=1
        ;;
      -e|--exec)
        shift 1
        local -r exec=1
        ;;
      -l|--log)
        local logfl=$2
        shift 2
        ;;
      -s|--source)
        local source=$2
        shift 2
        ;;
      -d|--destin)
        local destin=$2
        shift 2
        ;;
      --)
        shift;  break  ;;
      esac
    done
  else
    shorthelp=1 # getopt returned (and reported) an error.
  fi

  if (( filetr_dryrun == 1 )); then 

    echo "rsync -av --progress --log-file=$logfl --dry-run"
    echo "  $source $destin"
    rsync -av --progress --log-file=$logfl --dry-run $source $destin

  elif (( filetr_exec == 1 )); then
      
    # use rsync archive option -a (equivalent to -rlptgoD)
    echo "rsync -av --progress --log-file=$logfl $source $destin"
    # rsync -av --progress --log-file=$logfl $source $destin

  else

    echo "rsync -av --progress --log-file=$logfl $source $destin"
      
  fi

ベストアンサー1

rsync -av --progress --log-file=$logfl --dry-run $source $destin

これ必要二重引用符(例を参照--log-file="$logfl" --dry-run "$source" "$destin"いつ二重引用符が必要ですか?そしてhttp://mywiki.wooledge.org/WordSplitting

local logfl=$2

これはおそらく二重引用符を使用する必要があります。単純な割り当て(または他のものはlogfl=$2ありませんlocal)は必要ない場合の1つですが、などの場合にはlocalそれほどexport明確でreadonlyはありません。問題が発生しないように、そこに引用符を入れてください。

  if (( filetr_dryrun == 1 )); then 
    ...
    rsync -av --progress --log-file=$logfl --dry-run $source $destin
  elif (( filetr_exec == 1 )); then
    ...
    rsync -av --progress --log-file=$logfl $source $destin

-avここにコードをコピーすると、、、、ともに両方--progressのブランチで同じです。これを避けるのが最善であり、Bashを実行しているので、引数を配列として収集できます。以下を参照してください。$source$destin条件付きで引数をスクリプトに渡す

filetr-archive ()

ダッシュは Bash と Zsh の関数名の一部ですが、Ksh や Dash ではそうではありません。したがって、移植性のために下線を使用する方がより合理的である可能性があります。もちろん、とにかくPOSIX以外のアイテムを使用しているので、(( .. ))少なくとも意識的にDashを無視したいと思います。

おすすめ記事