Thread: basic bash programming question

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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, 04:18 PM
  5. Just a basic question about game programming
    By need4speed in forum Game Programming
    Replies: 5
    Last Post: 01-20-2008, 03:24 AM