basic bash programming question

This is a discussion on basic bash programming question within the Linux Programming forums, part of the Platform Specific Boards category; I keep getting this error "line 36: syntax error: unexpected end of file", ive googled it and the only solutions ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    basic bash programming question

    I keep getting this error "line 36: syntax error: unexpected end of file", ive googled it and the only solutions i could find were not working. im still not entirely sure im running the program correctly. im using this command in my linux terminal: "bash Homework5Bash.bash"

    Code:
    #!/bin/bash
    name=$"Joe Shmoe"
    echo "$name"
    var=0
    while [$var -lt 101]
    do
    	i=var%2;  
    	if [$i = 0];then
    		echo "$var";
    	fi
    	j=var%17;
    	if [$j = 0];then
    		if [$i = 0];then
    			echo " banana,";
    		else
    			echo "$var banana";
    		fi
    	fi
    	k=var%15;
    	if [$k = 0];then
    		if [[$i = 0] || [$j = 0]]; then
    			echo " pear";
    		else
    			echo "$var pear,";
    		fi
    	fi
    	l=var%6;
    	if [$l = 0];then
    		echo "orange";
    	fi
    	var=$[$var+1];
    	echo "\n";
    done
    Im really just looking for any way to solve that end of file error, just so I can have the program at least run.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > I keep getting this error "line 36: syntax error: unexpected end of file",
    Well your posted code has only 33 lines, so now what?

    What I do get is
    $ bash foo.sh
    Joe Shmoe
    foo.sh: line 5: [0: command not found
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    yes because when i posted i deleted 2 lines worth of comments at the top containing my real name and student id

    am i supposed to be naming my bash file as a ".sh" or a ".bash "?

    ive only been taught 30 minutes worth of bash programming so im very new to this

    EDIT: This is weird, im getting that error in my original code, yet when i copied the same exact code from what i posted im getting that same error you mentioned. and im looking at it and all that is changed is my real name to Joe Schmoe and i removed 2 comments... thats it.
    Last edited by Jimb0; 05-05-2011 at 10:33 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Well this prints 100 lines of \n
    Code:
    #!/bin/bash
    name=$"Joe Shmoe"
    echo "$name"
    var=0
    while [ $var -lt 101 ]
    do
        i=var%2;
        if [ $i = 0 ]; then
            echo "$var";
        fi
        j=var%17;
        if [ $j = 0 ]; then
            if [ $i = 0 ]; then
                echo " banana,";
            else
                echo "$var banana";
            fi
        fi
        k=var%15;
        if [ $k = 0 ]; then
            if [ [ $i = 0 ] || [ $j = 0 ] ]; then
                echo " pear";
            else
                echo "$var pear,";
            fi
        fi
        l=var%6;
        if [ $l = 0 ]; then
            echo "orange";
        fi
        var=$[ $var+1 ];
        echo "\n";
    done
    Note that shell scripts are a bit more sensitive to white space (compared to a C program).
    Look at all the if statements, and then look at your while statement. Use -lt type operators.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    ahh i see thank you so much i almost have this working:
    Code:
    #!/bin/bash
    name=$"Joe Shmoe"
    echo "$name"
    var=0
    while [ $var -lt 101 ]
    do
        i=$[ $var%2];
        if [ $i -eq 0 ]; then
            echo "$var";
        fi
        j=$[ $var%17];
        if [ $j -eq 0 ]; then
            if [ $i -eq 0 ]; then
                echo " banana,";
            else
                echo "$var banana";
            fi
        fi
        k=$[ $var%15 ];
        if [ $k -eq 0 ]; then
            if [ [ [ $i -eq 0 ] || [ $j -eq 0 ] ] ]; then
                echo " pear";
            else
                echo "$var pear";
            fi
        fi
        l=$[ $var%6 ];
        if [ $l -eq 0 ]; then
            echo " orange";
        fi
        var=$[ $var+1 ];
        echo "\n";
    done
    everything is printing out correctly except im just getting some weird errors with the multiple conditional statement on line 21.

    EDIT: nm i got it completely working just discovered the "-o"

    thank you so much...again.
    Last edited by Jimb0; 05-05-2011 at 10:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bash question
    By lehe in forum Linux Programming
    Replies: 9
    Last Post: 07-29-2009, 12:43 AM
  2. ssh/bash script question
    By Overworked_PhD in forum Tech Board
    Replies: 2
    Last Post: 03-30-2009, 07:48 PM
  3. #!/bin/bash question
    By elsheepo in forum C Programming
    Replies: 10
    Last Post: 03-08-2009, 11:42 PM
  4. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  5. Just a basic question about game programming
    By need4speed in forum Game Programming
    Replies: 5
    Last Post: 01-20-2008, 02:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21