Sponsored Content
Full Discussion: Error code with if statement
Top Forums UNIX for Beginners Questions & Answers Error code with if statement Post 303026696 by bakunin on Saturday 1st of December 2018 04:20:07 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 03:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy