My variable cannot be passed through into my path

Hi,
Can you please help. I am scripting in sh and I am trying to simply copy one directory to another but for some reason my variables are not recognised?

echo "The latest version of the program is being found......."
cd $SOFTWARE/src/$progname
version=`ls $SOFTWARE/src/$progname | grep 'v00*'  | tail -1`
echo "Latest version for $progname is $version"
 
#Copy over the latest version of the program
cp -r $SOFTWARE/src/$progname/$version $SOFTWARE/devl/$progname

The problem is to do with the $version variable it seems because when I subsitute $version with a directory name that is hard coded it works BUT when you simply echo $version it also looks correct. Do I need to convert it to a different format or something?

CF

hint:

grep 'v00*' ???

Assuming u meant the extra space, just deleted this but still the $version is not recognised since the cp command does nothing?

I think cabrao meant that you didn't need the . Besides, with your grep in single quotes the shell would not expand it in any way so it would look for filenames literally including v00

At the very least in a regular expression you would need .* (without single quotes)

A * in a regular expression matches 0 or more of the last expression (in your case the character 0), whereas . means match any character and .* matches 0 or more of any character.

You should also change your ls to "ls -1".

version=$(ls -1 $SOFTWARE/src/$progname | grep v00  | tail -1)

I don't know the names of the files in your $program directory, but:

version=$(ls -1 $SOFTWARE/src/$progname/*v00* | tail -1)

should also do the same thing.

Don't confuse the * in how the shell expands filenames with * in a regular expression - they're not the same thing.

okay, these alternatives still work but i am facec with the same problem when trying to do the copy, it seems to find the latest directory or version e.g. v003 but it does not copy the directory to the target directory?

if i hard code v003 in the scrpt instead it works though???

post the error lines if you could

don't get any I'm affraid, i only realise it hasn't copied the directory when looking into the target one and its empty. to confirm the source and target are correct I have echoed these pathnames and they appear correct in the terminal and when I do the same command cp -r source target on the command line it works? by the way I'm on the linux too

Try some thing like this :

cp -r "$SOFTWARE/src/$progname/$version"  "$SOFTWARE/devl/$progname"

tried this too but still nothing, its strange, I have even echoed each variable to a file just to check for spaces but nothing!! One thing I have noticed though it if I manually created a folder in the target directory when I run this script it deletes this folder almost suggesting the $version variable is in fact empty but when I echo this it shows the correct value!?!?!!?

I suspect with the $version value your getting , try to print it's length and see..

sorry not sure how to do this?

do something like this :

echo "$version" | awk '{ print length }' 

See if it is having any trailing spaces ( i mean check the length )

cheers, okay I have tried this and it has length 4 of which is correct! I am really baffled now, any more ideas??

The same code working fine for me for an example which i tried ...have to investigate but don't have access to your system to see what is really happening .. :expressionless: