If else condition

Hi,

I am writing a script and using if condition as per below example.

echo $abspath
echo if [[ -f "$abspath" ]] 
then 
echo "file exist" 
fi

but this never satisfy the condition even if i am giving correct path to it. and this works on command prompt correctly.

Please help me out in finding the root cause of it.

Thanks,
Vipin Kumar Rai

---------- Post updated at 02:30 AM ---------- Previous update was at 02:28 AM ----------

pasting the my code:

echo $abspath
if [[ -f "$abspath" ]] 
then 
echo "file exist" 
fi

You seem to have found the error in your first script - does the problem persist or is it solved? Please provide an indication of the status of the thread.

Hi,

It is not solved yet.

Thanks,
Vipin Kumar Rai

Too bad! Please describe your problem in detail. On two installations (linux & FreeBSD) your script yields

abspath=/mnt/9/playground/leapfunction 
if [[ -f "$abspath" ]] 
then 
echo "file exist" 
fi
file exist

seems to work flawlessly.

You could give us the error message you are getting...
I suspect you are using a true sh shell that doesnt recognise [[ syntax ...
Try

echo $abspath
if [ -f "$abspath" ] 
then 
   echo "file exist" 
fi

If this works, I hope you see why we insist on knowing the shell you are using and your OS...

Demonstration:

onas:/home/vbe $ cat test002
abspath=./test003 
if [ -f "$abspath" ]
then 
   echo "$abspath file exist " 
fi
onas:/home/vbe $ sh test002
./test003 file exist 
onas:/home/vbe $ ksh test002
./test003 file exist 
onas:/home/vbe $ cat test003
abspath=./test003 
if [[ -f "$abspath" ]]
then 
   echo "$abspath file exist " 
fi
onas:/home/vbe $ ksh test003
./test003 file exist 
onas:/home/vbe $ sh test003
test003: 5: [[: not found
onas:/home/vbe $ 

HI,

Thanks for you suggestion.
The problem was "^M" getting appended in the variable.
can you please let me know if i want to check the value if it has ^M, how to display then.

Thanks,
Vipin Kumar Rai

---------- Post updated at 08:30 AM ---------- Previous update was at 08:28 AM ----------

for eg.

abc="i am vipin^M"
cat $abc

it is not showing "^M" character but i want to show it.
what is the way?

Q: Have you tested the snippet you just submitted?
So your conclusion?

cat prints input files (including stdin) to stdout. Use options like -A, -E, -T, -v to show control characters.
echo or (preferrably) printf print text and/or variables to stdout; pipe this through e.g. cat -A to see if a ^M is there.

This said, we are off topic considering the thread's title and so I will close the thread.
Up the the user to open a new threadregarding his new request...

Yes - if the system uses GNU- cat (or some other non-standard-conforming variant). POSIX-cat has only one valid option: "-u" for unbuffered operation.

Three ways to avoid and/or manage "^M" characters:

First, DO NOT WRITE YOUR SCRIPTS IN WINDOWS AND THEN FTP THEM TO UNIX! This is how the overwhelming majority of misplaced ^M-characters come to pass.

Second, open the file in questsion in vi and then enter: ":set list". This will display non-printable characters: "^I" is a tab, "^M" is a (Windows-style) line feed, "$" is a newline character, etc.. use the command ":set nolist" to switch back to normal display.

Third, control your variable definitions:

var='foo
bar'

will place a "^M" character between "foo" and "bar".

I hope this helps.

bakunin

Assuming standard TTY settings, the command:

var='foo
bar'

will place a <newline> (i.e., control-J or the C escape sequence '\n' ) between "foo" and "bar"; not a <carriage-return> (i.e., control-M or '\r' ).

Try:

var='foo
bar'
printf '%s\n' "$var" | od -bc

and you'll get:

printf '%s\n' "$v"|od -bc
0000000   146 157 157 012 142 141 162 012                                
           f   o   o  \n   b   a   r  \n

not:

0000000   146 157 157 015 142 141 162 012                                
           f   o   o  \r   b   a   r  \n