Skip to main content
Explain how to force the correct shell
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

You are missing the mandatory : after the d in your while getopts line if the -d is to accept a parameter. Therefore your copy_dest is empty, and hence cp complains about the "missing operand". If you add "debug" lines such as

echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"

after your loop, you will see the problem. To solve, simply add the ::

while getopts s:d: flag
do
   ...
done

AlsoAlso, please note that in particular when dealing with filenames, you should always quote shell variables, as in

cp "$copy_source" "$copy_dest"

In additionIn addition, be aware that running a script as

sh test_cp.sh

will override the shebang-line #!/bin/bash and you cannot be sure that it is run under bash! If you want to ensure the correct shell is being used, you could either explicitly state


bash test_cp.sh arguments

or make the script file executable and run it as


./test_cp.sh arguments

You are missing the mandatory : after the d in your while getopts line if the -d is to accept a parameter. Therefore your copy_dest is empty, and hence cp complains about the "missing operand". If you add "debug" lines such as

echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"

after your loop, you will see the problem. To solve, simply add the ::

while getopts s:d: flag
do
   ...
done

Also, please note that in particular when dealing with filenames, you should always quote shell variables, as in

cp "$copy_source" "$copy_dest"

In addition, be aware that running a script as

sh test_cp.sh

will override the shebang-line #!/bin/bash and you cannot be sure that it is run under bash!

You are missing the mandatory : after the d in your while getopts line if the -d is to accept a parameter. Therefore your copy_dest is empty, and hence cp complains about the "missing operand". If you add "debug" lines such as

echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"

after your loop, you will see the problem. To solve, simply add the ::

while getopts s:d: flag
do
   ...
done

Also, please note that in particular when dealing with filenames, you should always quote shell variables, as in

cp "$copy_source" "$copy_dest"

In addition, be aware that running a script as

sh test_cp.sh

will override the shebang-line #!/bin/bash and you cannot be sure that it is run under bash! If you want to ensure the correct shell is being used, you could either explicitly state


bash test_cp.sh arguments

or make the script file executable and run it as


./test_cp.sh arguments
Add caveat
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

You are missing the mandatory : after the d in your while getopts line if the -d is to accept a parameter. Therefore your copy_dest is empty, and hence cp complains about the "missing operand". If you add "debug" lines such as

echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"
echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"

after your loop, you will see the problem. To solve, simply add the ::

while getopts s:d: flag
do
   ...
done

Also, please note that in particular when dealing with filenames, you should always quote shell variables, as in

cp "$copy_source" "$copy_dest"

In addition, be aware that running a script as

while getopts s:s: flag
do
  sh ..test_cp.
donesh

will override the shebang-line #!/bin/bash and you cannot be sure that it is run under bash!

You are missing the mandatory : after the d in your while getopts line if the -d is to accept a parameter. Therefore your copy_dest is empty, and hence cp complains about the "missing operand". If you add "debug" lines such as

echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"

after your loop, you will see the problem. To solve, simply add the ::

while getopts s:s: flag
do
   ...
done

You are missing the mandatory : after the d in your while getopts line if the -d is to accept a parameter. Therefore your copy_dest is empty, and hence cp complains about the "missing operand". If you add "debug" lines such as

echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"

after your loop, you will see the problem. To solve, simply add the ::

while getopts s:d: flag
do
   ...
done

Also, please note that in particular when dealing with filenames, you should always quote shell variables, as in

cp "$copy_source" "$copy_dest"

In addition, be aware that running a script as

sh test_cp.sh

will override the shebang-line #!/bin/bash and you cannot be sure that it is run under bash!

Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

You are missing the mandatory : after the d in your while getopts line if the -d is to accept a parameter. Therefore your copy_dest is empty, and hence cp complains about the "missing operand". If you add "debug" lines such as

echo "Source parameter: $copy_source"
echo "Destination parameter: $copy_dest"

after your loop, you will see the problem. To solve, simply add the ::

while getopts s:s: flag
do
   ...
done