首页 > 解决方案 > Linux mv 命令然后关机

问题描述

我有这个在 Ubuntu 16.04 上运行的小 bash 脚本(简化):

tar zxvf fileNameHere.tgz  <-- Untar tgz file in $SRC_DIR
files=$(ls $SRC_DIR)
echo "Extracting $files"  >> $APP_LOG_DIR/update.log
mv $SRC_DIR/* $OUTPUT_DIR
shutdown -r now

我注意到,重新启动后,只有有时文件没有移动到目标,我想知道该关闭命令是否可能是一个问题。关机前是否需要调用“同步”?

标签: linuxbashrsyncshutdown

解决方案


修复了带有注释的脚本:

#!/usr/bin/env bash

# Test if SRC_DIR and OUTPUT_DIR are actual directories
if [ -d "$SRC_DIR" ] && [ -d "$OUTPUT_DIR" ]; then

  # Populates the arguments array with the content
  # of SRC_DIR rather than parsing the output of ls
  set -- "$SRC_DIR/"*

  # Prints joined file entries of the arguments array
  # while stripping their leading directory path
  printf 'Extracting %s\n' "${*#*/}" >> "$APP_LOG_DIR/update.log"

  # Moves all the arguments array's entries (the actual 
  # content of the SRC_DIR) into OUTPUT_DIR
  mv -- "$@" "$OUTPUT_DIR/"
  shutdown -r now
fi

推荐阅读