小编flo*_*lad的帖子

在 bash 脚本中使用命令行参数作为 cp 和 mv 的目标

我试图通过运行带有标志/参数的脚本来复制文件(或重命名文件)以提供源文件名和目标文件名:

#!/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)不接受参数化的目的地是哪里?我究竟做错了什么?

bash shell-script getopts

3
推荐指数
1
解决办法
380
查看次数

标签 统计

bash ×1

getopts ×1

shell-script ×1