我试图通过运行带有标志/参数的脚本来复制文件(或重命名文件)以提供源文件名和目标文件名:
#!/bin/bash/
while getopts s:d flag
do
case "${flag}" in
s) copy_source=${OPTARG};;
d) copy_dest=${OPTARG};;
esac
done
echo "Copy a file input with argument to another file input with argument"
cp $copy_source $copy_dest
Run Code Online (Sandbox Code Playgroud)
输出是一个错误:
sh test_cp.sh -s file1.txt -d file2.txt
Copy a file input with argument to another file input with argument
cp: missing destination file operand after ‘file1.txt’
Try 'cp --help' for more information.
Run Code Online (Sandbox Code Playgroud)
是否cp(和mv)不接受参数化的目的地是哪里?我究竟做错了什么?