Search code examples
bashif-statementwindow

Simple if statement in bash doesn't work


I am learning a little bit of bash in Linux and I just can't understand why this doesn't work. It is a simple IF statement and a read command to keep the window opened. What happens is that when I execute the .sh file the terminal's window opens for a second and closes back. I can't see any message or check whether there's any error or why it doesn't work. If I remove the IF block then I can see the message and the window remains opened. This is the code inside my file

count=99
if [ $count -eq 100 ]; then
    echo "Count is 100"
else
    echo "Count is not 100"
fi
read -p "Press enter to continue" nothing

I tried many other ways of using the IF structure but seems like none works


Solution

  • Use the dos2unix utility to convert the text file created on Windows to the correct format for Linux. See this wikipedia page for more details.

    Install if necessary:

    $ sudo apt-get install dos2unix
    <snip>
    Setting up dos2unix (5.3.1-1) ...
    $
    

    Run it on your script:

    $ dos2unix if.sh
    dos2unix: converting file if.sh to Unix format ...
    $