Error code with if statement


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Error code with if statement
# 8  
Old 12-01-2018
Quote:
Originally Posted by Ibrahims1
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 ?
Yes: use vi! ;-))

Seriously: look carefully at your code. You see that funny characters at the end:

Code:
           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):

Code:
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.

I hope this helps.

bakunin
# 9  
Old 12-02-2018
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:
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:
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

Moderator's Comments:
Mod Comment edit b bakunin: Please use CODE-tags! Editing them in on your behalf is getting old quickly. Thank you!

Last edited by bakunin; 12-02-2018 at 06:20 AM..
# 10  
Old 12-02-2018
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.

Quote:
Originally Posted by Ibrahims1
i tried and not work
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.

Quote:
Originally Posted by Ibrahims1
any advises as well how to debug such code or how to debug in general using vi editor is highly appreciated
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:

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:

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
~

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:

Code:
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:

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.

I hope this helps.

bakunin
# 11  
Old 12-02-2018
Quote:
Originally Posted by Ibrahims1
... moving forward with the script i get this error :
...
any help please ? any advises as well how to debug such code or how to debug in general using vi editor is highly appreciated
...

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.
This User Gave Thanks to RudiC For This Post:
# 12  
Old 12-02-2018
hello

regarding (Remove ALL ^M characters in it) , my code does not have this in the commands i write so i don't know how to remove them .
# 13  
Old 12-02-2018
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.
# 14  
Old 12-02-2018
Hello Rudic

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Does this statement of code mean.....

Good morning, I am 100% mew to Unix and am trying to troubleshoot why a pgm written 3 years ago, suddenly is not working properly. It is part Perl with some UNIX commands thrown in. I need to verify what the UNIX commands are doing before I can continue with my other troubleshooting. print... (7 Replies)
Discussion started by: jaacmmason
7 Replies

2. Shell Programming and Scripting

Error in if statement

I am working on script for stale nfs. the file consists of cat data01stale.log - - - - /abcd/backup - - - - /abcd/data Script (16 Replies)
Discussion started by: nareshkumar522
16 Replies

3. UNIX for Dummies Questions & Answers

How to use a return code in an if statement?

Hi all, After so many tries and searching online for ideas, I had trouble accomplishing this. Is it possible to do something like this in KSH to run an if statement on a return code? Unfortunately the code below fails... Would anyone know how to fix the below attempt? if "$`{pkginfo... (3 Replies)
Discussion started by: chatguy
3 Replies

4. UNIX for Dummies Questions & Answers

if statement code syntax

Hi, can someone please tell me what is wrong with this code? I just want it to check if the file size is greater than 2000kb. if Thanks! ---------- Post updated at 09:23 PM ---------- Previous update was at 09:21 PM ---------- I should probably post the full code: #!/bin/sh... (9 Replies)
Discussion started by: Bengel
9 Replies

5. UNIX for Dummies Questions & Answers

error in if statement

Hi, This is my script to catch any oracle errors. In this, the $sqlerr returns ORA-01017: invalid username/password; logon denied when i specify wrong username/password the if condition is failing. how can i resolve the issue. the if statement gives error sqloutput=`sqlplus -s -L... (1 Reply)
Discussion started by: Swapna173
1 Replies

6. Shell Programming and Scripting

Error in IF statement

HI i am getting error while executing the given statement for filename in `cat a/file.lst` do if then echo "Exit Code Description :File $filename - is missing in Input Directory" >a.log exit else count1=`awk 'END {print NR}' $filename` echo "$count1">>a.log count2=`awk 'END {print... (4 Replies)
Discussion started by: ravi214u
4 Replies

7. Linux

error in if statement

Hi , I am getting an error when I run the script for checking word "view" in a file . I am using if statement. like this if then VW_VAR=` cat $TN.${ecmdate}.sql1 | grep -i view | awk '{print $3}' | cut -d '.' -f2 ` echo " VW_$VW_VAR " sed -e... (16 Replies)
Discussion started by: capri_drm
16 Replies

8. Shell Programming and Scripting

Snytax error on If Statement--help

year=`date '+%Y'` month=`date '+%m'` day=`date '+%d'` day=`expr $day - 1` case $month in 1 | 3 | 5 | 7 | 8 | 10 | 12);; if($day =7 ); then $day=6 fi 4 | 6 | 9 | 11);; if ; then $day=31 fi 2);; if ; then if ; then (2 Replies)
Discussion started by: dannyd_y
2 Replies

9. Shell Programming and Scripting

Error with if statement..Please help

:b:hi, I have a script as given below: pr_det="1" if then awk ' BEGIN {printf("%23s","session")}' >> report.txt awk ' BEGIN {printf "\n"} ' >> report.txt else awk ' BEGIN {printf("%55s","file_dsc")} ' >> report.txt awk ' BEGIN {printf("%101s","no_recs")} '... (1 Reply)
Discussion started by: jisha
1 Replies

10. Shell Programming and Scripting

Code checking for all values in the same if statement.

I am trying to set up a variable based on the name of the file. function script_name { if then job_name='MONITOR' return job_name; elsif then job_name='VERSION' return job_name fi } for i in `ls *log` do script_name $i done. (4 Replies)
Discussion started by: oracle8
4 Replies
Login or Register to Ask a Question