1 Star 0 Fork 0

sierxue/scripts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
copy_file.sh 1.14 KB
一键复制 编辑 原始数据 按行查看 历史
sierxue 提交于 2024-01-29 13:09 . new: copy_file
#!/bin/bash
# copy_file.sh
# This script copies a file to a specified directory if the file is newer than the existing one in that directory.
# Usage: ./copy_file.sh source_file target_directory
# Function to copy a file to the specified directory if it is newer
copy_file() {
# Assign arguments to variables for better readability
local source_file=$1
local target_directory=$2
# Check if the source file exists
if [ ! -f "$source_file" ]; then
echo "\"$source_file\" not found!"
notify-send "\"$source_file\" not found!"
return 1
fi
# Copy the file if it is newer than the one in the target directory
if [ "$source_file" -nt "$target_directory/$(basename "$source_file")" ]; then
\cp "$source_file" "$target_directory"
echo "\"$source_file\" has been updated on $(LC_TIME=en_US.UTF-8 date +%Y-%m-%d-%H-%M-%S-%a)!"
fi
}
# Check if the correct number of arguments is provided
if [ $# -ne 2 ]; then
echo "Error: Incorrect number of arguments."
echo "Usage: $0 source_file target_directory"
exit 1
fi
# Call the copy_files function with all the passed arguments
copy_file "$@"
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sierxue/scripts.git
git@gitee.com:sierxue/scripts.git
sierxue
scripts
scripts
master

搜索帮助