Bash:if if case

时间:2017-01-09 20:53:13

标签: bash

下面我使用if语句和case语句来安排下面我的参数的顺序,以减轻我对rsync的重复输入。对于case以下if块,#!/bin/bash rsync="rsync -vrtzhP --delete" localmus=" /cygdrive/c/Users/user/Music/Zune/" remotemus=" 10.252.252.254::Zune/" localcal=" /cygdrive/c/Users/user/calibre/" remotecal=" 10.252.252.254::calibre/" dry=" -n" if [ $1 == "zune" ] && [ $2 == "tohere" ] then toex=$rsync$remotemus$localmus fi if [ $1 == "zune" ] && [ $2 == "tothere" ] then toex=$rsync$localmus$remotemus fi if [ $1 == "calibre" ] && [ $2 == "tohere" ] then toex=$rsync$remotecal$localcal fi if [ $1 == "calibre" ] && [ $2 == "tothere" ] then toex=$rsync$localcal$remotecal fi if [[ $3 == "dry" ]] then toex=$toex$dry fi echo echo $toex echo echo "Execute? y/n: " read answer case $answer in y) eval $toex ;; n) echo NO! ;; esac 声明会更明智吗?如果是这样,怎么样?

$ python -m flask run

3 个答案:

答案 0 :(得分:3)

您可以连接$ 1和$ 2来启用它。

case "$1 $2" in
"zune toHere")
  toex=$rsync$localmus$remotemus
  ;;
"calibre toHere")
  toex=$rsync$remotecal$localcal
  ;;
*)
  echo "Unknown command $1 $2"
  exit 2
  ;;
esac

答案 1 :(得分:1)

auto语句将在此处生成更具可读性和更紧凑的代码:

case

答案 2 :(得分:0)

没有理由一起处理这两个,绝对没有理由使用eval

remote=10.252.252.254::
local=/cygdrive/c/Users/user/

do_rsync () {
  rsync -vrtzhP --delete "$1" "$2"
}

case $1 in
  zune)
    remote+=Zune/
    local+=/Music/Zune
    ;;
  calibre)
    remote+=calibre/
    local+=/calibre
    ;;
  *) echo "Unknown transfer type: $1" >&2
     return 1
     ;;
esac

case $2 in
  tohere)
    do_rsync "$remote" "$local" ;;
  tothere)
    do_rsync "$local" "$remote" ;;
  *) echo "Unknown transfer direction: $2" >&2
     return 1
esac
相关问题