Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to test if file "file1.c" is present in the current working directory, what am I doing wrong with my test command? I thought I understood this command, am I doing something wrong for the Bourne shell that I do not know about?

#! /bin/sh
NAME=$1
if((test -e "$NAME"));then
echo File $NAME present
else
echo File $NAME not present
fi
share|improve this question
up vote 2 down vote accepted

You don't need the enclosing parentheses, test itself would suffice:

if test -e "$NAME"; then

The (()) is for arithmetic comparison operations.

test is synonymous to [ command, so you can use:

if [ -e "$NAME" ]; then

too.

Also some shell has the [[ keyword:

if [[ -e "$NAME" ]]; then
share|improve this answer
    
Thank you. That works for me. Is this only for test? Because I am able to do something like if(($# != 2));then....and the parentheses is required for me (or atleast it doesn't work without it). – Andrew M Mar 30 at 3:27
    
@AndrewM Check my edits.. – heemayl Mar 30 at 3:32
    
Thank you for the explanation. My book does not cover this well at all. – Andrew M Mar 30 at 3:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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