0

This works from the command line:

cp -r some/dir/!(.git) some/other/dir/

What I'm trying to accomplish is doing a recursive copy but ignore the .git sub-directory.

I want to put this in a shell script but the parentheses are causing me problems.

Placing that command in a shell script (bash) it gives me a syntax error.

If I escape the parentheses (with back-slashes), then I get the following error:

/usr/bin/cp: cannot stat 'some/dir/!(.git)': No such file or directory

How should the command be formed inside a bash shell script?

UPDATED 15-apr-2020:

Here is the code from my simplified shell script

#!/bin/bash

shopt -s extglob

TEST_DIR=./copy-to-here/

DIRS="
 some-dir/
"

for dir in $DIRS; do

   echo "${dir}!(.git}"
   # /usr/bin/cp -rp "$dir!(.git)" $copytodir
   ls -l "${dir}!(.git)"

done

Here is a recording of my attempt

Lance Perry
  • 1,276
  • 2
  • 18
  • 28
  • To be clear -- the error message is different from that given in the linked duplicate, but the cause and fix are the same. – Charles Duffy Apr 14 '20 at 17:43
  • gotcha, but that post did not help me, I did exactly what that post said and I still get the following error: cannot access some/dir/!(.git): no such file or directory ..... when i know the directory exists, i ran the command from the command line – Lance Perry Apr 14 '20 at 18:01
  • If you want to assert that you can make the failure happen after a `shopt -s extglob`, give us a log that shows us a `shopt -s extglob` being run and the failure happening, and that *also* shows us that the file really does exist. We need the details sufficient to distinguish the question from the existing ones (showing, not telling, that it isn't the same issue) to be *included in that question itself*. – Charles Duffy Apr 14 '20 at 18:10
  • Also, *how* your script is being run matters; we often have people who *think* their scripts are being run with bash, when they're actually executed with `/bin/sh`, or zsh, or another interpreter. The duplicate has comments and answers that say as much too -- but again, [edit] to show us *within the question itself*. – Charles Duffy Apr 14 '20 at 18:12

0 Answers0