I have to get some files from a device and store them in a directory with a particular name. I'm writing a script that does this, but one of the first things it does is it checks if the directory with the wanted name already exists.

The device version is what determines the name of the directory, but the formatting is off. From my device I get the following number: NUM=10-2-2 I have to reformat it to 10.2.2, so I use NUM=$(echo $NUM | tr "-" ".") This is the name of a directory. The full path, then, would be /var/www/myServer/10.2.2.

However, the following code:

if [ -d $Filepath ]; then
   echo the directory exists
    echo the directory does not exist

always tells me the directory does not exist. But if I declare the variable Filepath directly (i.e. Filepath=/var/www/myServer/10.2.2) I get the desired result. Why does this happen?

The output of the tr command is as follows:

~$ <command-that-gets-version-number> | tr "-" "."

Why does this happen? Any ideas at all would be helpful.

I am running this on Ubuntu 11.x

  • Per how you explained it it makes no sense, and it should work, so the problem is likely elsewhere. Please post the full script. – kos Jul 9 '15 at 22:25

Found my problem. The result from the <command-that-gets-version-number> | tr "-" "." line was leaving a carriage return character at the end. I noticed this because when I executed: ~$ printf %s "$NUM" | od -t x1 the output showed the last character was 0d, which is carriage return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.