Consider this scp command to send all files and directories in the current directory except the shell scripts.
scp -r ./!(*.sh) [email protected]:~/
When this command is ran from the terminal, it works fine. I am trying to run the same command in a bash script, and for that I had to change it this way
scp -r ./!\(*.sh\) [email protected]:~/
but it returns this error
./!(*.sh): No such file or directory
EDIT 1 Using the full quote as follow
scp -r './!(*.sh)' [email protected]:~/
return the same error
EDIT 2 The full script looks like this
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
cd "${DIR}" || exit
echo -e "Sending all scripts to $1."
shopt -s extglob
scp -r -P $1 ./!\(*.sh\) [email protected]:$2
- Making scp as a separate function fixed the issue with Regex as follow:
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
cd "${DIR}" || exit
run_scp() {
scp -r -P $PORT $1 [email protected]:$TARGET_DIR
}
PORT=$1
TARGET_DIR=$2
echo -e "Sending all scripts to $PORT."
shopt -s extglob
run_scp ./!\(*.sh\)
shopt -s extglobto enable the extended globbing patterns?extglob onshopt -s extglobin the script did not solve this issue!(...)to be interpreted as an extended glob pattern (assuming that that really is a bash script, and you haveshopt -s extglobin a different line and not in the same function).shopt -s extglobto the script, did you still quote the globbing pattern? (you shouldn't)