-3

I am having a bit of a problem with a script I am trying to create.

This script should take two arguments which are the source directory and the destination directory and if the user enters less than 2 arguments it should print out an error message and exit. In addition this script should check if the source directory exists or not, if not it should print out an error message and exit. Also the script should check if the destination directory exists or not, if not it should create that directory. Finally the script should copy the files from the source directory to the destination directory.

This is my attempt so far:

if (( $# < 2 ));
   echo "Error: Too few arguments supplied"
   exit 1
if [ -d "src_dir" ]
then
    echo "Directory src_dir exists."
else
    echo "Error: Directory src_dir does not exist."
fi

if [ -d "dst_dir" ]
then
    echo "Directory dst_dir exists."
else
    mkdir dst_dir
    cp -r src_dir/* dst_dir
fi

Any help would be really appreciated. Thanks in advance!

shellter
  • 36,525
  • 7
  • 83
  • 90
arry617
  • 19
  • 1
  • 1
  • 8
  • What problem are you having? What does happen, and what were you expecting to happen? – Joel C Apr 24 '16 at 03:36
  • paste your code into http://shellcheck.net (add a "she-bang" line at the top, ie `#!/bin/bash`.) Good luck. – shellter Apr 24 '16 at 03:36

1 Answers1

1

For checking the correct number of arguments: Check number of arguments passed to a Bash script

if [ "$#" -ne 2 ]; then
    echo "Error: Too few arguments supplied"
    exit 1
fi

For checking if the directory exists or not: Check if a directory exists in a shell script

if [ ! -d "$1" ]; then
    echo "Error: Directory $1 does not exist."
    exit 1
fi

For making the dir on second argument: How to use Bash to create a folder if it doesn't already exist?

mkdir -p $2

Finally, just copy them all:

cp -r $1/* $2/
Community
  • 1
  • 1
Glrs
  • 1,060
  • 15
  • 26