my first script for linux and how i did it
Results 1 to 7 of 7

Thread: my first script for linux and how i did it

  1. #1
    Blast From the Past
    Join Date
    Jan 2003
    Posts
    729

    Talking my first script for linux and how i did it

    hey all!!

    well i love playing the game neverball... and neverputt... opensource game for linux that is addictive beyond all reason.

    http://icculus.org/neverball/

    well.. when you compile this program it didnt really have an install.. it just built in the src directory.

    well i was getting tired of

    $cd neverball*
    $./neverball

    SOOO i decided to get dirty and write my first script!!!


    i did some google searching and found this link

    http://freeos.com/guides/lsst/ch01sec09.html

    this was entirely responsible for me learning linux scripting... although from looking at them before i kinda knew what had to be where... so i knew what it should look like etc etc

    and after some trial and error it worked!

    its too simple but fantastic for a first throw at programming/scripting i think

    it asks you to choose between neverball and neverputt
    then it reads the keyboard output as $choice

    then if choice equals 1 it launches neverball... if it equals 2 it launches neverputt...

    if it equals 3 or more it says to choose 1 or 2 and exits.


    simple... before i knew what code to use at all i was able to picture what the script would need to do and how i might be able to do it... i think that helped me being it gave me an idea of what i need to look for


    in order for the program to work the neverball src dir needs to be in your home folder or ~...
    i might expand on this and make it search for it...


    SO comments, questions anyone?
    work it harder, make it better, do it faster, makes us stronger

  2. #2
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    For a first time, that is definitely a good job. One thing I'd recommend is that you stick to the basic shells versus the more specialized one as it will have more portability. In most cases you can, especially since you used bash here, use /bin/sh for greater effect since more are likely to have this versus bash, though bash is certainly more common than it used to be...

    Another way you could have approached it, rather than ask for input, is to use command line arguments (i hate being asked for input from scripts )...pet peeve of mine I guess...It also goes to show you with shell scripting, there is almost always more than one way to skin a cat....

    Code:
    #!/bin/sh
    
    if [ "$1" == "neverball" ]; then
            if [ -f ~/neverball/neverball ]; then
                    cd ~/neverball && ./neverball
            fi
    elif [ "$1" == "neverputt" ]; then
            if [ -f ~/neverball/neverputt ]; then
                    cd ~/neverball && ./neverputt
            fi
    elif [ "$1" == "" ]; then
            echo "load.sh <program>"
            exit 1
    else
            echo "I don't know how to load $1"
            exit 1
    fi
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  3. #3
    Blast From the Past
    Join Date
    Jan 2003
    Posts
    729
    that is interesting, but can you explain that code to me aliitle more?

    where do you input the data for $1, im presuming thats the command line option?

    and i was wondering about the command line option but this made sense to me
    work it harder, make it better, do it faster, makes us stronger

  4. #4
    Senior Member
    Join Date
    Oct 2002
    Posts
    1,130
    where do you input the data for $1, im presuming thats the command line option?
    Yep.

    Anytime $1 is used in a script it will be replaced with the first argument given on the command line. $2 is the second, $3 is the third, and so on. $0 is actually the name of the script, so if you change the name, you can always refer to it as $0.

    I'd look at the Advanced Bash Scripting Guide to teach you some more. It progresses very well from the basics to the more advanced operations, and taught me almost everything I know about bash scripting.

    Good job! Was certainly better than my first script!
    Government is like fire - a handy servant, but a dangerous master - George Washington
    Government is not reason, it is not eloquence - it is force. - George Washington.

    Join the UnError community!

  5. #5
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    Gratulations - I have to second Striek: my first script was
    much more ...simpler

    He also provided an excellent link - I like that advanced bash scripting
    guide, and I use it from time to time.


    Quick Ideas:
    - extend on the possible ways of input. check whether some command arguments
    have been given, if not switch to interactive mode
    - do not exit when an invalid or unknown argument has been provided. loop.
    and add an exit option.
    - use variables for neverball's/neverputt's directories
    - does neverball/neverputt comes with an executable or do you first have to
    compile/link it? if so, then you could add a check whether ./neverball resp. ./neverputt
    actually exists, and if not, compile/link it automatically...

    May the scripting force be with you

    Cheers
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  6. #6
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Excellent points from everyone...one other thing that I started thinking about when looking back at what I had done compared to your code...I would suggest surrounding your variables during a test with "" (that is what the [ ] in the if statement is doing). The reason for this is that if for some reason the variable is ever null, the test will fail b/c the operator you are using expects two arguments and will give you an error as such. If you surround it with "", the " will still allow for the variable value to be substituted (versus ' which does not, or ` which can be used to execute a command and substitute in the output) and you won't have to worry about a null substitution for the variable...

    Hope that made sense...
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  7. #7
    Blast From the Past
    Join Date
    Jan 2003
    Posts
    729
    yes that made perfect sense nebulus, thanks for your input

    as for all of you thanks for your comments and ideas, il rework my code in afew hours when i get off work and repost later to see if i got it right

    thanks all!
    work it harder, make it better, do it faster, makes us stronger

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides