I need some help. I am writing a bash script on RHEL that will use scp to download files from a remote server to the local machine. the way the script works is that you pass it the remote server the source directory and destination directory and the files. It works when I say the files to get is tes* it will grab all the files that start with tes. I can't get it working when I pass regex to it
The code is below
#!/bin/bash
STARTTIME=`/usr/bin/date +%H:%M:%S`
HOME=/home/oracle
export HOME
if [ "$4" = "" ]; then
echo "Usage: $0 [source path] [destination path] [scp server] [file pattern]" >&2
exit 1
fi
###
#sending to tmp log file
echo "----------------------" | tee -a /tmp/GetFile.log.$$
echo "StartTime ${STARTTIME}" | tee -a /tmp/GetFile.$$
echo "Push Files to ${3}" | tee -a /tmp/GetFile.log.$$
echo "Filemask is ${4}" | tee -a /tmp/GetFile.log.$$
echo "Source Path is ${1}" | tee -a /tmp/GetFile.log.$$
echo "Dest Path is ${2}" | tee -a /tmp/GetFile.log.$$
echo "SCP Server is ${3}" | tee -a /tmp/GetFile.log.$$
echo "---------------------" | tee -a /tmp/GetFile.log.$$
#The code below works when I use the filemask as te*
script -q -c "scp oracle@${3}:${1}${4} ${2}" >> /tmp/GetFile.log.$$
FILEMASK=${4}
echo "-----------------------------------" | tee -a /tmp/GetFile.$$
awk -v searchterm="${FILEMASK}" '$0 ~ searchterm {IGNORECASE=1 ; print $1 }' /tmp/GetFile.log.$$ | tee -a /tmp/GetFile.$$
echo "------------------------------------" | tee -a /tmp/GetFile.$$
RETURN_MESSAGE=" - Transfer From server1: "
export RETURN_MESSAGE
echo $RETURN_MESSAGE | tee -a /tmp/GetFile.$$
when I run it this is what I get ./GetFile /home/oracle/ /tmp server1 ten*.*
the following files are retrived tenant.7366 100% 145 7.2KB/s 00:00 tenant.7367 100% 47 2.2KB/s 00:00 tenant.7369 100% 47 2.4KB/s 00:00
Now I have change it a little bit to try and get it working with regex I comment the line below #script -q -c "scp oracle@${3}:${1}${4} ${2}" >> /tmp/GetFile.log.$$
#I use this line instead scp "oracle@${3}:${1}${4}${2}" >> /tmp/GetFile.log.$$
I run it like this ./GetFilev3 /home/oracle/ /tmp/ server1 "[te]*"
But I get the following extra files ef345 et123 tenant.7366 tenant.7367 tenant.7369 tentt1 tentt2
Not sure what I am doing wrong all I want are the files starting with te
What you are performing is a bash glob, not a regular expresison. I believe that you are matching on files that start with either the t
or the e
when you run [te]*
.
What you want to do is likely the following. I supsect you will need to escape the asterisk to prevent the glob from resolving when you invoke it. ./GetFilev3 /home/oracle/ /tmp/ server1 "te\*"