Shell, executing a command based on input.

Discussion in 'Mac Programming' started by TheChubbyBunny, Nov 25, 2009.

  1. TheChubbyBunny macrumors newbie

    Joined:
    Nov 25, 2009
    #1
    So I'm creating a shell script right, can any of you look through and tell me what's wrong with it?:
    Code:
    #!/bin/sh
    
    PATH=""
    
    printf "Please specifiy your server's path:"
    
    read PATH
    
    cd $PATH
    
    echo "Your server's path is $PATH"
    
    printf "now please specify an action: compile, run, or both: "
    
    read ACTION
    
    if ( "x$ACTION" == "compile" ) ; then
    	
    	javac *.java
    	
    	echo "compiling server..."
    
    else if ( "x$ACTION" == "run" ) ; then
    	
    	java -Xmx256m server
    	
    	echo "running server..."
    
    else if ( "x$ACTION" == "both" ) ; then
    	
    	javac *.java
    	java -Xmx256m server
    	
    	echo "compiling and running server..."
    	
    else
    	
    	echo "OH SHI-"
    	
    endif
    
    in terminal I have this:

    Code:
    Please specifiy your server's path:/Server
    Your server's path is /Server
    now please specify an action: compile, run, or both: compile  
    ./Compiler.sh: line 56: syntax error: unexpected end of file
    
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    The script you have supplied doesn't have 56 lines, so another script is must have the error. What is on line 56 there?

    Also, it's been a while since I've done scripting, but you have variable ACTION but are testing for x$ACTION, and with quotes to boot. Doesn't placing the x in your test void the test?

    Edit: Here is a test script. It looks to me like you should be using a different shell for your coding (with tweaks) or adapt as per the sample.

    #!/bin/sh

    Code:
    ACT='gg'
    echo $ACT
    
    if [ $ACT == "ggg" ] ; then
    echo PP
    elif [ $ACT == "gg" ] ; then
    echo FOUND gg
    else
    echo FAILED
    fi
    Your 'else if' needs to close each 'if' for example and that closing token is fi, not endif. Also the brackets should be squared not rounded. This is different from the shell I usually use.
     
  3. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #3
    the line 56 is just some extra space, i'll delete it, but what's up there is my whole script. I'll take out x and see what happens.
     
  4. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #4
    now i get
    Code:
    Please specifiy your server's path:/server
    Your server's path is /server
    now please specify an action: compile, run, or both: compile
    ./compiler.sh: line 43: syntax error: unexpected end of file
    with the script:
    Code:
    #!/bin/sh
    
    PATH=""
    
    printf "Please specifiy your server's path:"
    
    read PATH
    
    cd $PATH
    
    echo "Your server's path is $PATH"
    
    printf "now please specify an action: compile, run, or both: "
    
    read ACTION
    
    if ( "$ACTION" == "compile" ) ; then
    	
    	javac *.java
    	
    	echo "compiling server..."
    
    else if ( "$ACTION" == "run" ) ; then
    	
    	java -Xmx256m server
    	
    	echo "running server..."
    
    else if ( "$ACTION" == "both" ) ; then
    	
    	javac *.java
    	java -Xmx256m server
    	
    	echo "compiling and running server..."
    
    endif
     
  5. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #5
    okay, so I got it working , I was using a c shell if-then statement instead of a shell one.
    The problem is is that it's not navigating to the path specified.
    can anybody help me with that?
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    Have you removed the 'x' before each "x$ACTION"? Or are you saying that the 'cd' isn't working?

    P.S. See my added notes, which you seem to have figured out.
     
  7. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #7
    yeah, I also changed the syntax of my if-then statement.
    But the problem is in the first part. You can see it asks the user to input the path they want, it will read the path fine, but it doesn't execute the cd command right. It's almost like it just doesn't execute it at all, which sucks, because you would have to open up the .sh file , and manually change the folder path.
     
  8. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #8
    The first part of your code worked for me fine. Remember that when the script exits, it will not be in that directory unless you were already there when you executed the script. That cd applies to the script, which is a separate execution from where you are.

    Place the following line immediately after the cd to see where the script thinks it is.

    Code:
    echo `pwd`
    You can also do the following to be extra sure.

    Code:
    echo `/bin/ls`
     
  9. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #9
    Code:
    #!/bin/sh
    
    # Compiler.sh
    # 
    #
    
    printf "Please specify your server's path:"
    
    read PATH
    
    echo "your server is located in $PATH" #just basically a test to see if it's reading the input
    
    cd $PATH
    
    echo `pwd`
    
    printf "Please specify an action: 'compile', 'run', or 'both': "
    
    read ACTION
    
    if [ "$ACTION" = "compile" ] ; then
    	
    	javac *.java
    	
    	echo "compiling server..."
    
    elif [ "$ACTION" = "run" ] ; then
    	
    	java -Xmx256m server
    	
    	echo "running server..."
    
    elif [ "$ACTION" == "both" ] ; then
    	
    	javac *.java
    	java -Xmx256m server
    	
    	echo "compiling and running server..."
    
    fi
    Here's my updated script, which is not navigating to the correct folder via the cd command.
    now here's what happens when the script is executed:

    Code:
    Please specify your server's path:/server
    your server is located in /server
    /server
    Please specify an action: 'compile', 'run', or 'both': compile
    ./compiler.sh: line 23: javac: command not found
    compiling server...
    
    Apparently it's navigating to the correct folder, but still says that the command "javac" is not found, which usually means that your not executing the command in the right folder. Also say's it's compiling server when it's not. Kind of annoying but I don't know to get the script to realize there's an error.
     
  10. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #10
    Change your variable named PATH to something else like MYPATH.
     
  11. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #11
    thanks xStep, works now but I get this wierd error:
    Code:
    ./compiler.sh: line 34: cd: /bin:/sbin:/usr/bin:/usr/sbin: No such file or directory
    What's even weirder is the commands seem to execute fine, and the whole thing works as intended. Anyway to get rid of that seemingly useless error?
     
  12. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #12
    I don't know. Double check all of your uses of your PATH variable. Perhaps you missed changing one.

    Place a -v at the end of the first line and see if it gives you any hints.

    Code:
    #!/bin/sh -v
     
  13. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
  14. TheChubbyBunny thread starter macrumors newbie

    Joined:
    Nov 25, 2009
    #14
    is there anyway that the script can tell what it's containing folder is ?
    for example, instead of this:
    Code:
    printf "Please specify your server's path:"
    
    read MYPATH
    
    echo "$MYPATH" > path #just basically a test to see if it's reading the input
    
    cd $MYPATH
    
    I could have something like:
    Code:
    cd $CONATINING_FOLDER
    
    So that you don't have to input the folder path everytime?
     
  15. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.

Share This Page