0

I'm developing a shell script to sync the particular folder from target to destination path, I need to execute the script like below:

sh deploy.sh deploy staging particular_folder_name

Below is the script which i developed:

function deploy() { 
    staging
}

function staging() {
    ENTITY_NAME="$1"
    echo "deployment here"

    ENTITY_PATH=`ls -ld /u01/home/${USER}/testing_vela/subject-areas/entites/staging-modules/${ENTITY_NAME}`
    echo  $ENTITY_PATH

    DEPLOYMENT_FOLDER=`echo $ENTITY_PATH|rev|awk -v FS='/' '{print $1}'|rev`
    echo $DEPLOYMENT_FOLDER
 
    SOURCE_PATH='/u01/home/${USER}/testing_vela/deploy/subject-areas/modules'
    DESTINATION_PATH='/u02/home/${USER}/testing_vela/subject-areas/entites/staging-modules'

    if [ "${ENTITY_NAME}" == "${DEPLOYMENT_FOLDER}" ]
    then 
        echo "folder is exist"
        rsync -avzh ${SOURCE_PATH}/${ENTITY_NAME} ${DESTINATION_PATH}/${ENTITY_NAME}
    else
        echo "folder does not exist"
    fi
}

case $1 in
    deploy) "$@";
    staging) "$@";
esac

Can someone help me to correct, if I am doing anything wrong in the above script? I am facing the below error whenever I execute the script.

ERROR:

sh deploy.sh deploy staging contract
deploy.sh: line 87: syntax error near unexpected token `)'
deploy.sh: line 87: `staging) "$@";'
10
  • Don't use the .sh suffix on your shell script: talisman.org/~erlkonig/documents/… Commented Jun 7, 2022 at 13:56
  • "folder does not exist" should probably be considered an error message. As such, you should write it to stderr: echo >&2 'folder does not exist'. But it seems very strange to emit that error in response to a fragile parsing of ls. If you are going to confirm the existence of a directory, use test -d. Commented Jun 7, 2022 at 14:04
  • You can use basename rather than rev|awk|rev. Also, in awk you can get the last field with $NF, so you don't need the rev. Commented Jun 7, 2022 at 14:45
  • You can also use ${ENTITY_PATH##*/} to get the last component of the path. Commented Jun 7, 2022 at 14:46
  • 1
    Variables don't expand in single-quotes, so things like SOURCE_PATH='/u01/home/${USER}/...' won't work; use double-quotes instead. shellcheck.net will spot this and a number of other problems in the script. Commented Jun 7, 2022 at 18:55

2 Answers 2

1

The syntax for a case statement requires 2 semi-colons to terminate the commands of a case:

case $1 in
deploy) "$@";;
staging) "$@";;
esac
Sign up to request clarification or add additional context in comments.

3 Comments

Now am not getting error ,but rsync is doing for all the folders in target and destination ,but my requirement is should be done only for one folder
@rakeshricky Parsing ls is extremely fragile. You need to fix the logic around building the rsync commands, and verify that they are as you expect and that they are correct.
Actually ,when i ran the same rsync command on shell not as script,it able to run for particular folder
0

If it still throws error , put the 2 semi-colons on next line like below :

    case $1 in
          deploy) "$@"
    ;;
           staging) "$@"
    ;;
    esac

1 Comment

now am not getting error but the rsync command not able to take folder_name(contract) even am passing while executing the script like below sh -x deploy.sh deploy staging contract

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.