Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1063147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:44:26+00:00 2026-05-16T18:44:26+00:00

I have a part of my script that does this: Removes everything in directory

  • 0

I have a part of my script that does this:

  1. Removes everything in directory
  2. Force syncs from perforce that directory
  3. copies files from another directory to said directory, of which there are some conflicts that the source control prevent from being overwritten (which is expectable and what I want)

Before I would have just this:

...
cp <source path> <dest path>
echo done copying
...
echo done

Output:

...
Permission Denied:file1
Permission Denied:file2
done copying
...
done

So, it would do the stuff, and reach done.
Then, I went and made a sort of check to make sure the directory exits like so:

if[ -d sourcepath ]
      then 
       if [ -d destpath ]
           then
              cp <source path> <dest path>
           else
              echo problem with dest
              exit 1
        fi
    else
        problem with source
       exit 1
fi

But now the script just exits after the last of the Permission Denies, not hitting anything after, so the output is like this:

Output:

...
Permission Denied:file1
Permission Denied:file2

I’m not too savvy in the bash rules, so I just thought I’d post this question here since I couldn’t find it. It seems that in the if, though, the fact that there are permission problems cause it to exit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:44:27+00:00Added an answer on May 16, 2026 at 6:44 pm

    Other than syntax errors presumably introduced during cut’n’paste here, there’s nothing wrong with that code, as shown by the following experiment:

    #!/bin/bash
    
    rm -rf sourcepath destpath
    mkdir sourcepath
    mkdir destpath
    touch sourcepath/xyz
    touch destpath/xyz
    chmod 000 destpath/xyz
    
    if [ -d sourcepath ]
          then
           if [ -d destpath ]
               then
                  cp sourcepath/xyz destpath/xyz
               else
                  echo problem with dest
                  exit 1
            fi
        else
           echo problem with source
           exit 1
    fi
    echo Woohoo!
    

    When run, this outputs:

    cp: cannot create regular file `destpath/xyz': Permission denied
    Woohoo!
    

    so you can see it’s carrying on after the failure.

    One piece of invaluable advice: when you’re debugging bash scripts, put the line:

    set -x
    

    right up the top (after #!/bin/bash if it’s there). That will cause it to output each command before executing it.

    In fact, I always right my scripts starting with:

    #!/bin/bash
    #set -x
    

    so I can just uncomment the second line for debugging purposes.

    As an aside, I would code that as:

    if [[ ! -d sourcepath ]] ; then
        problem with source
        exit 1
    fi
    if [[ ! -d destpath ]] ; then
        problem with dest
        exit 1
    fi
    cp <source path> <dest path>
    

    but that’s just because I don’t like complicated if-then-else constructs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a build script and as part of that script it copies a
As part of my integration strategy, I have a few SQL scripts that run
I have configured Tomcat 6 to serve Perl CGI scripts. That part is well
I'm currently experimenting with build script, and since I have an ASP.net Web Part
Is there someway to have a part of the page that renders like a
In my (PHP) web app, I have a part of my site that keeps
I have a two part question Best-Practice I have an algorithm that performs some
In Toyota manufacturing lines they always know what path a part have traveled. Just
I have a web part with links to e.g. Manage Users i SharePoint (2003)
I'm using the content query web part and have exported it to a webpart

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.