#!/bin/bash
# 检查参数数量
if [ $# -ne 5 ]; then
echo "Usage: $0 <directory> <file1> <file2> <file3> <file4>"
exit 1
fi
# 第一个参数是目录
directory=$1
# 检查目录是否存在并且是目录文件
if [ ! -d "$directory" ]; then
echo "Error: $directory is not a directory."
exit 1
fi
# 从第二个参数开始,检查并拷贝文件
shift # 移除第一个参数(目录),这样我们就可以从$1开始遍历后面的文件
while [ "$#" -gt 0 ]; do
file=$1
# 检查文件是否存在并且是普通文件
if [ -f "$file" ]; then
# 拷贝文件到目录
cp "$file" "$directory"
if [ $? -ne 0 ]; then
echo "Error: Failed to copy $file to $directory."
exit 1
fi
else
echo "Warning: $file is not a regular file. Skipping."
fi
shift # 移除当前参数,继续下一个
done
echo "All files have been checked and copied as needed."
# 检查参数数量
if [ $# -ne 5 ]; then
echo "Usage: $0 <directory> <file1> <file2> <file3> <file4>"
exit 1
fi
# 第一个参数是目录
directory=$1
# 检查目录是否存在并且是目录文件
if [ ! -d "$directory" ]; then
echo "Error: $directory is not a directory."
exit 1
fi
# 从第二个参数开始,检查并拷贝文件
shift # 移除第一个参数(目录),这样我们就可以从$1开始遍历后面的文件
while [ "$#" -gt 0 ]; do
file=$1
# 检查文件是否存在并且是普通文件
if [ -f "$file" ]; then
# 拷贝文件到目录
cp "$file" "$directory"
if [ $? -ne 0 ]; then
echo "Error: Failed to copy $file to $directory."
exit 1
fi
else
echo "Warning: $file is not a regular file. Skipping."
fi
shift # 移除当前参数,继续下一个
done
echo "All files have been checked and copied as needed."