Announcement

Collapse
No announcement yet.

Batch files in Linux!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Computer Chat Batch files in Linux!

    Hello

    In DOS, we used to write batch files *.bat

    I want to write a similar thing in Linux to execute certain jobs on demand.

    The question is what are the batch files in Linux and how can I execute them?

    Regards
    Last edited by Milado; Tue 30 Sep '08, 4:26am.
    Syrian Medical Society - mi la do - My Hacks - vCharset converter - Projects Queue

    I want to breathe the freedom, and to strew the mounts of nostalgia in your eyes.

  • #2
    From the command line:

    nano -w test.sh

    in the top type: #!/bin/bash

    Below it type echo "my script works!"

    Then type exit 0

    and press control + o to save the file, and control +x to exit it.
    In the command line again, type: chmod 755 test.sh
    You can now run it with : ./test.sh

    Example can be found here: http://bash.cyberciti.biz/

    nano is a text editor, like vi, or whatever you prefer to use. I am just using nano as an example, because I feel it's the most comfortable one for me to use.
    The line in the top tells it it's a bash shell script, and where to find bash. Usually that line is right, but otherwise find on your system where bash is located and point to it.
    echo will simply write something to the screen, the stuff in between the double quotes in this case.
    exit 0 is optional, but basically means you are done with wherever you are in the script and want out on level 0
    control + o or control +x are key combinations, to trigger commands, like save current file, or exit this file.
    chmod allows you to change the permissions on a file, in this case we want to set it to executable file with perms 755 on the file test.sh
    the ./ in front of test.sh is current dir, from that root path, this file. if you were in /root and you want to run it , but it's in /etc/scripts/ you type: /etc/scripts/test.sh

    When you're done with the above commands, you can do: cat test.sh
    this will then list the content of the file, which should look like this:
    #!/bin/bash
    echo "my script works!"
    exit 0

    and when you execute the script, you should see this on the page:
    my script works!

    To expand this batch script (shell script), you can do things like
    cd $HOME
    echo "back in home!"
    ls -all
    echo "just listed all the files in $HOME"
    mkdir testingsomething
    echo "just made a new dir called testingsomething"
    cd $HOME/testingsomething
    echo "and now i am in there!"
    etc etc etc
    Last edited by Floris; Tue 30 Sep '08, 4:18am.

    Comment


    • #3
      Thank Floris
      Syrian Medical Society - mi la do - My Hacks - vCharset converter - Projects Queue

      I want to breathe the freedom, and to strew the mounts of nostalgia in your eyes.

      Comment


      • #4
        So test.sh should contain:

        Code:
        #!/bin/bash
        
        Some commands go here!
        
        exit 0
        And must be 755

        Another question: will this work in a directory like /home/username/public_html/ if I execute it in the SSH? ot it must be in the directory: /etc/scripts/
        Syrian Medical Society - mi la do - My Hacks - vCharset converter - Projects Queue

        I want to breathe the freedom, and to strew the mounts of nostalgia in your eyes.

        Comment


        • #5
          It works in every directory.

          Anything specific you're trying to do? Perhaps I can help you get started.

          Comment


          • #6
            I can unzip a new version of vBulletin on a new directory "newvb", execute this file, and then run the upgrade script.

            Code:
            #!/bin/bash
            
            rm /home/username/public_html/newvb/favicon.ico
            rm /home/username/public_html/newvb/LICENSE
            
            mv /home/username/public_html/newvb/*.* /home/username/public_html/forum/
            
            mv /home/username/public_html/newvb/clientscript/*.* /home/username/public_html/forum/clientscript
            mv /home/username/public_html/newvb/clientscript/vbulletin_css/*.* /home/username/public_html/forum/clientscript/vbulletin_css
            mv /home/username/public_html/newvb/clientscript/yui/*.* /home/username/public_html/forum/clientscript/yui
            mv /home/username/public_html/newvb/clientscript/yui/connection/*.* /home/username/public_html/forum/clientscript/yui/connection
            mv /home/username/public_html/newvb/clientscript/yui/yahoo-dom-event/*.* /home/username/public_html/forum/clientscript/yui/yahoo-dom-event
            
            mv /home/username/public_html/newvb/admincp/*.* /home/username/public_html/forum/madmin
            mv /home/username/public_html/newvb/admincp/control_examples/*.* /home/username/public_html/forum/madmin/control_examples
            mv /home/username/public_html/newvb/modcp/*.* /home/username/public_html/forum/mmod
            
            mv /home/username/public_html/newvb/includes/*.* /home/username/public_html/forum/includes
            mv /home/username/public_html/newvb/includes/cron/*.* /home/username/public_html/forum/includes/cron
            mv /home/username/public_html/newvb/includes/datastore/*.* /home/username/public_html/forum/includes/datastore
            mv /home/username/public_html/newvb/includes/paymentapi/*.* /home/username/public_html/forum/includes/paymentapi
            mv /home/username/public_html/newvb/includes/xml/*.* /home/username/public_html/forum/includes/xml
            
            mv /home/username/public_html/newvb/archive/*.* /home/username/public_html/forum/archive
            
            mkdir /home/username/public_html/forum/install
            mv /home/username/public_html/newvb/install/*.* /home/username/public_html/forum/install
            
            exit 0
            So there is no need to upload every single file. I can upload the zip compressed file.

            Regards
            Syrian Medical Society - mi la do - My Hacks - vCharset converter - Projects Queue

            I want to breathe the freedom, and to strew the mounts of nostalgia in your eyes.

            Comment


            • #7
              Why not unpack, move content of upload/ to the new location, and let mv take care of the overwrite, and recursive?
              And then just delete the unpacked dir.

              Or unpack the dir on top of the existing dir, overwriting if exist.

              mv supports -f to force, without prompt, overwriting what's there, including sub dirs.

              Personally .. I cp -rf the public_html to a backup_public_html/ so I can quicly revert.
              Then unpack the .zip inside this public_html, overwriting what's there.
              since config.php.new is in the zip you can use the old config.php, so this isn't an issue either.

              Comment


              • #8
                I have many files that belong to other mods, so I can't simply discard the forum directory.

                I don't know that "mv" command can move sub-directories and contained files within them to the target directory.

                So the commad will be like this?
                Code:
                mv /home/username/public_html/newvb/* /home/username/public_html/forum/
                I may give it a try.

                Regards
                Syrian Medical Society - mi la do - My Hacks - vCharset converter - Projects Queue

                I want to breathe the freedom, and to strew the mounts of nostalgia in your eyes.

                Comment


                • #9
                  Originally posted by Floris View Post
                  Personally .. I cp -rf the public_html to a backup_public_html/ so I can quicly revert.
                  Then unpack the .zip inside this public_html, overwriting what's there.
                  since config.php.new is in the zip you can use the old config.php, so this isn't an issue either.
                  I remove a few other files before overwriting- the ie prompt image, rss feed image, database error image and favicon.ico etc.
                  ~~~~~

                  Comment


                  • #10
                    You can pretty much do this in any language, but Bash is probably the most commonly used.

                    I would recommend looking through: http://www.gnu.org/software/bash/manual/bashref.html


                    You also might want to take a look at Perl and Python if you want to be a little more expansive. For this, though, it shouldn't be too much more than regular expressions and a bit of Bash.

                    Comment

                    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                    Working...
                    X