Skip to main content
Changed title (had minor typo, also tried to make main problem more obvious)
Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

using flags Using command-line parameters as destination atribute for cp and mv in bash script

Became Hot Network Question
Minor formatting (use inline code for verbatim command names) and re-tag; issue not related to cp or mv, but getopts
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

I am attempting to do copy a file (or rename a file) by running the script with flags/parameters to give both the source and the destination file name:

#!/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

The output is an error:

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.

Does cpcp (and mvmv) not accept parametrized destination  ? What am I doing wrong  ?

I am attempting to do copy a file (or rename a file) by running the script with flags/parameters to give both the source and the destination file name:

#!/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

The output is an error:

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.

Does cp (and mv) not accept parametrized destination  ? What am I doing wrong  ?

I am attempting to do copy a file (or rename a file) by running the script with flags/parameters to give both the source and the destination file name:

#!/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

The output is an error:

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.

Does cp (and mv) not accept parametrized destination? What am I doing wrong?

Source Link

using flags as destination atribute for cp and mv

I am attempting to do copy a file (or rename a file) by running the script with flags/parameters to give both the source and the destination file name:

#!/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

The output is an error:

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.

Does cp (and mv) not accept parametrized destination ? What am I doing wrong ?