Error code with if statement

Windows text editors and UNIX text editors disagree on what a line is.

In windows, a line is a bunch of characters followed by two nonprinting characters, \r \n.
In UNIX, a line is a bunch of characters followed by one nonprinting character, \n.

All these \r's are basically garbage which Windows insists on adding to the end of every line.

Hi Corona , so how can i get rid of what windows is adding (as in my code i dont see this ^M ) ...

Windows didn't add the <carriage-return> characters (i.e. control-M, frequently displayed by some utilities when using some options as ^M ); you added those characters by choosing to use a WYSIWYG editor while it was configured to produce DOS style text files.

In post #8 in this thread, bakunin told you exactly what needs to be done and gave you three ways to do it. You replied "i tried and not work" which tells us absolutely nothing. What, EXACTLY, did you try? In what way, EXACTLY, did it not work? If one of the commands you tried produced a diagnostic message, show us EXACTLY what command you tried (in CODE tags) and show us EXACTLY the diagnostic messages produced by the command you tried (in CODE tags).

Hi Don

i saw post 8 and i tried, in post 9 i mentioned that i don't know how to use the command to solve the problem that was suggested in post 8 , i mentioned as well my file location so i wish if you can help how to use the solution suggested in post 8 as i am new and dont know how to use it,...thanks for your support.

Sigh....

Told you what was wrong with your script file. You mentioned that you wrote your script (see post #1) and then did:

So you have a file named "hello" which contains your script, no? Now, in this file there are erroneous end-of-line markers, because you posted this (#7)

$ cat -A hello
#!/bin/bash^M$
echo -e "enter the name of the file : \c"^M$
read file_name^M$
if [ -e $file_name ]^M$
then^M$
echo "$file_name found"^M$
else^M$
echo "$file_name not found"^M$
end^M$
^M$

ibrahims@N-5CG613336K ~

It is now perhaps a 1-million-dollar, who-wants-to-be-a-millionaire, extremely hard to find out type of guess which file i meant to process when i told (#8) you to use:

sed 's/^M$//' /path/to/dosstyle.file > /path/to/unixstyle.file

(hint, because otherwise it would be unsolvable by mere humans: its the only one we talked about the whole time. Try three times and i'll tell you the answer.)

And,after this Nobel-prize-worthy puzzle, guess what i meant by the file name /path/to/dosstyle.file and, respectively, by the file name /path/to/unixstyle.file . Have you even tried to understand what i wrote about or did you just look at the command, didn't understand it immediately and said "didn't work"? What exactly "didn't work"? Or is that just an euphemism for "i couldn't be bothered to even try"?

bakunin

/PS: reminds me of an english proverb: you can lead a horse to water but you can't make it drink.

2 Likes

Thanks every one for your support , i will try to follow all what you said, it may need more reading before i become able to understand every recommendation, i will do my best. thanks again

1 Like

This works . I have seen this error before . That time it was because I was missing a semi colon. I suspect its the same in your case

echo "enter file name " ; read input ; if [ -e $input ]; then echo "$input fo
und"; else echo "$input not found"; fi
enter file name
ntsuser.ini
ntsuser.ini not found


 echo "enter file name " ; read input ; if [ -e $input ]; then echo "$input fo
und"; else echo "$input not found"; fi
enter file name
ntuser.ini
ntuser.ini found