-1

Am writing a bash function to run rsync, with possibility of using --dry-run option and --exec option. Need some criticism improvements and additional checks.

Might not make much sense for the user to specify both --dry-run and --exec. I would simply use --dry-run anyway.

Have started with the following setup.

Calling the following

filetr-archive -n -s bkscan -d temp

runs the command

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

which makes the file temp/bkscan.log whose contents are

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)

But, rsync also produces the following, which never gets saved. Is the output from rsync displayed on the terminal useful? I could also store the information. But I wonder how I could do it.

  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

Pietru
  • 389
  • 1
  • 17
  • 4
    I don't see a question here – Panki Jul 19 '21 at 10:39
  • 4
    I’m voting to close this question because this is asking for [codereview.se]. – muru Jul 19 '21 at 11:01
  • What's wrong with code review? – Pietru Jul 19 '21 at 11:10
  • From the help file: "You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site and push other questions off the front page." Also to avoid: "there is no actual problem to be solved". If you have no problem, don't ask a question. – berndbausch Jul 19 '21 at 11:30
  • 1
    If you have a specific issue with your script, then you could ask, but we'd need far more detail and context that a simple dump of the code and a request for improvements. You probably want to ask on [codereview.se] instead, as muru suggested. – terdon Jul 19 '21 at 11:35
  • So I would do that. Did not know about it. – Pietru Jul 19 '21 at 11:46

1 Answers1

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

This needs double-quotes, i.e. --log-file="$logfl" --dry-run "$source" "$destin". See When is double-quoting necessary? and http://mywiki.wooledge.org/WordSplitting

local logfl=$2

This probably should also use double-quotes. A plain assignment (logfl=$2 without the local or anything else) is one of the cases that doesn't require them, but with local, export, readonly etc. it's not that clear cut. Just put the quotes there to prevent any issues.

  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

You're duplicating code here, all those -av, --progress, $source, $destin are the same in both branches. It's best to avoid that, and since you're running Bash, you can collect the arguments to an array, see: Conditionally pass params to a script

filetr-archive ()

The dash works as part of a function name in Bash and in Zsh, but not in Ksh or Dash, so it might make sense to use an underline instead for portability. Of course, you're using non-POSIX stuff like (( .. )) anyway, so you might want to consciously ignore at least Dash anyway.

ilkkachu
  • 138,973