im new to unix and when i use below script i get an error :
#! /bin/bash
Echo -e "enter the name of the file : \c"
read file_name
if [ -e $file_name ]
then
echo "$file_name found"
else
echo "$file_name not found"
fi
running the script i get below error :
$ ./hello
': not a valid identifiere : ./hello: line 3: read: `file_name
./hello: line 11: syntax error: unexpected end of file
ibrahims@N-5CG613336K ~
$
In which editor did you originally write the code?
The script runs fine for me but when I write it in a text editor like OpenOffice and then paste it into vi then I get a problem similar to the one you describe.
And indeed, the command cat -A hello then shows all kind of characters that don't belong there.
I would like to point out a few other issues that seem to not be causing errors just yet:-
The opening line of #! /bin/bash should be #!/bin/bash without the space.
The line starting Echo would probably cause an error unless you have a script, alias or function called Echo where i suspect you really want echo , all lower case. This could suggest autocorrect in your editor. You are better coding in something plain like Notepad, Vi or Emacs
When testing the input file name, you should really quote the string. The user could type spaces, nothing at all or even the string duff -o : which might still say the file exists even if it doesn't. You should use if [ -e "${file_name}" ] to eliminate these issues
thanks for your replies, here are the feedback you asked for :
1- i am using Cygwin on my windows 7 machine.
2- i am using visual studio 2010
$ 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 ~
3- i removed the space as suggested but it has no impact on the result, same error i am getting
thanks
--- Post updated at 06:52 PM ---
Hello , thanks but unfortunately i am just new and i dont understand what you said , kindly explain more what shall id o
--- Post updated at 07:51 PM ---
just a small comment , i tried using vi editor now and it works , i dont know why then it does not work when i use visual studio, any advises ?
Seriously: look carefully at your code. You see that funny characters at the end:
these ones
|
v
#!/bin/bash^M$
echo -e "enter the name of the file : \c"^M$
They are in fact Windows way of saying "the line ends here". You see, when you look at how a file is stored physically on the disk the characters are ordered in one long line of ascending storage addresses, not in an array, like when you look at it in an editor. To start a new line at a certain point when displaying it the operating system must have some means to tell that to the displaying program.
This - in DOS and all its descendants, Windows is one of them - is the sequence "<CR><LF>": "carriage return, linefeed" (if you wonder why: think of a mechanical typewriter and how the carriage is first moved back to the left again, then the barrel is rotated so that the next line is moved under the types). In all the UNIX variants (including Linux) this is not the case: in UNIX it is the <NL> (newline) character, which does the same. The reason is: printers worked like typewriters back in the days, but UNIX had "printer filters" - programs which took files sent to the printer and added whatver was necessary to print it properly. They would take a newline character and if the printer needed a CR/LF sequence they would insert that instead. DOS had no such thing as a printer filter and therefore its inventors made the files in a format that already fit most printers so that they didn't need the post-processing and still got a properly printed result.
When you now transfer a text file from UNIX to Windows or vice versa you need to change these line-end markers because the respective other OS will be confused if they are in the wrong format. You may remember FTP programs and their distinction between ASCII mode and "binary mode" - that was nothing else than the FTP program doing exactly this translation (ASCII) or not (binary). When you write your code in Visual Studio (or any other editor) in WIndows and then transfer it to UNIX you must do this translation otherwise UNIX will be confused. If you create code in vi and transfer it to Windows you need to to the same or Windows will think there is just one long line that never ends - look at such a transferred file in notepad.exe and you will see what i mean.
The "^M" is in fact a <CTRL>-<M> character, which is the same as pressing <ENTER>. You can produce it yourself: enter vi, enter insert mode ("i"), press <CTRL>-<V> to enter the next charcter verbose and then press <ENTER> - a "^M" should appear.
To get rid of DOS-style end-of-lines in UNIX you can run the following (the "^M" is such a <ENTER>-character):
sed 's/^M$//' /path/to/dosstyle.file > /path/to/unixstyle.file
Or, if your system has them, you can use the dos2unix and unix2dos programs, which do the translating too.
hello Bakunin , thanks for the detailed explanation , regarding your recommendation to get rid of that , where shall i use this command ? i tried and not work , i think may be because file of translation does not exist or i write the path wrongly , wish if you can help , knowing that my script location is (C:\cygwin64\home\ibrahims)
, shall i put this command in beginning of my script ?.
--- Post updated at 10:13 AM ---
dear all
moving forward with the script i get this error :
i am getting error when i run below script
Code:
#!/bin/bash
echo "Enter the name of the file"
read file_name
if [ -f $file_name ]
then
If [ -w $file_name ]
then
echo "add some text to quit ctrld"
cat >> $file_name
else
echo "the file does not have write permission"
fi
else
echo "$file_name does not exist"
fi
~
the error message is :
Code:
$ ./hello
Enter the name of the file
dddd
': not a valid identifier
./hello: line 16: syntax error: unexpected end of file
ibrahims@N-5CG613336K ~
$
any help please ? any advises as well how to debug such code or how to debug in general using vi editor is highly appreciated
First and foremost: i have put some remarks in your posting but you seem to do not read them: Please use CODE-tags when posting code, data or terminal output. How to do it is even written above the editor window when you write a posting, so it can't be that hard, can it? And it is also in the forum rules, which you have agreed to when you registered.
To find out what went wrong i would have to see what you have done, no? You don't go to the doctor and say "it hurts" but you specifically describe where it hurts and in which way and since when. Telling me "doesn't work" is simply not enough: show us the exact command you typed in and the error message it produced, preferably by copying and pasting directly from the screen to here.
You can't debug "using the vi editor" because the editor is built for one thing: editing text. You can start helping yourself with structuring your code, By that i mean specifically indentation. Let conditional statements stand out so that you understand what is going on when immediately - not after searching and second-guessing.
This is your code:
#!/bin/bash
echo "Enter the name of the file"
read file_name
if [ -f $file_name ]
then
If [ -w $file_name ]
then
echo "add some text to quit ctrld"
cat >> $file_name
else
echo "the file does not have write permission"
fi
else
echo "$file_name does not exist"
fi
~
Now, tell me where the second if-statement ends. You have to actually read everything, line by line, to find that out. Here is how i write it:
#!/bin/bash
echo "Enter the name of the file"
read file_name
if [ -f $file_name ] ; then
If [ -w $file_name ] ; then
echo "add some text to quit ctrld"
cat >> $file_name
else
echo "the file does not have write permission"
fi
else
echo "$file_name does not exist"
fi
~
To answer my previous question you just have to go down vertcally because everything that belongs together is aligned. Immediately the corresponding "if", "else" and "fi" stand out because they are indented at the same level. That is not just some idle idea of "arrange it nicer", it really helps understanding code faster and more efficiently. If also helps writing correct code, because if you have something like this:
if this ; then
if that ; then
do_something
else
do_something_else
fi
else
do_whatever
and you do not end up on column 1 you immediately know that something is amiss - in ths case a missing "fi" on one "if".
Coming back to your question after these rather general remarks: look carefully at the last line of your code:
~
I don't know if that character is just a copy-and-paste artefact ( vi denotes empty lines this way) or if it is really part of your code. If it is then perhaps your shell is confused by it because ~ is not a valid command.
Now, that error is the same as encountered in post#1, just five lines further on. There have been quite some comments on what the cause might be, and detailed hints by bakunin on how to correct it. Did you apply those, to the script file itself? Remove ALL ^M characters in it!
For debugging: try setting the shell's -x (xtrace) option to run the script to see what's going on; also, for bash , there is the DEBUG trap to help you.
Do you see it's the same error? Then, how did you get rid of the first one? Then, apply this again to every line in the script. I'm not familiar with neither cygwin nor vi , so I can't give you the respective commands, but I'm sure there are settings / commands in vi to make control characters visible.
In post#7, you had a means to display those, and in post#8, bakunin detailedly showed you how to proceed.
actually in the beginning i was using visual studio and get the error , when i moved to vi it is solved
i proceed with the code for more line commands and got new error , actually i made a new post for he new error but the admin considered it duplicate and asked to continue on the first post here , i hope its clear now , i will try to read again the instructions of Bakunin and i hope i can follow it correctly , sorry for that but i am very new and not easy for me to understand all your instructions ...i will try my best. thanks alot
i installed VSC , it is much friendly , thanks , however i still get the error as below :
#!/bin/bash
echo -e "Enter the name of the file : \c"
read file_name
if [ -e $file_name ]
then
echo "$file_name found"
else
echo "$file_name not found"
fi
the error is :
ibrahims@N-5CG613336K ~
$ ./vsc1.sh
': not a valid identifiere : ./vsc1.sh: line 3: read: `file_name
./vsc1.sh: line 9: syntax error near unexpected token `fi'
./vsc1.sh: line 9: `fi'
ibrahims@N-5CG613336K ~