1

I have problems with an if statement in my script. I have stored a path in a variable and want to use the if...else statement to check if the file (file1) in the path of the variable exists or not. This is my code:

variable=$/home/username/file1

if [ -f "${variable}" ]
then
    echo "$variable exists"
    exit
else
    echo "file not found"
    exit
fi

I have also tried [ -f "$variable" ] but in all cases, both statements are executed. I receive as output:

/home/username/file1 exists
file not found

I guess the main problem is, that the command itself is not working. But why does it not quit after the first exit command? Where is the big bug that I do not see?

closed as off-topic by jasonwryan, Kusalananda, ilkkachu, Jeff Schaller, schily Jul 3 '18 at 10:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, Kusalananda, ilkkachu
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Run the script with debugging on and you'll see what's going on behind the scenes. – Tomasz Jul 3 '18 at 8:14
  • Which shell are you using? – andcoz Jul 3 '18 at 8:14
  • How are you running the script? There is no way that the script shown here can produce both outputs. It should say file not found if the pathname $/home/username/file1 is not a regular file. – Kusalananda Jul 3 '18 at 8:40
  • I have found the problem. The path was stored in a file and copied from it with the grep command. However, there was a duplicate of the path in the file which messed it up. Now I have removed the duplicate and - magically - the if statement works now. – Buggy Jul 3 '18 at 8:40
  • 1
    That in itself doesn't explain how you'd manage to get a shell to run both branches of an if. But if the code you used wasn't exactly the one you posted, then... – ilkkachu Jul 3 '18 at 8:58
1

The issue in your script is in the first line.

To assign a path to a variable, simply consider it as a string:

variable="/home/username/file1"

or even this:

variable=/home/username/file1

then it works.


The $ sign is needed to assgn the output of a command to a variable:

OUTPUT="$(ls -1)"
  • This does not describe the cause of the issue nor the resolution to it. variable=$/home/username/file1 is a perfectly fine variable assignment and unrelated to the double output, and to command substitutions. – Kusalananda Jul 3 '18 at 8:41
  • Thanks for the info Kevin, I guess that was a typo. I actually got the variable out of a grep command ;) but I shortened my version here for clarity – Buggy Jul 3 '18 at 8:45
  • You're right. This doesn't explain the double output. But assigning the variable like in the question doesn't work for me in bash. Also, the used shell is not specified. – Kevin Lemaire Jul 3 '18 at 8:46

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