1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Does linux have something simalar to batch files?

Discussion in 'Linux and Unix' started by tommo020788, Apr 23, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. tommo020788

    tommo020788 Thread Starter

    Joined:
    Oct 20, 2008
    Messages:
    322
    Hi, I'm just wondering, does linux have some kind of way to make a file that can launch a bunch of lines of code in the terminal, like a batch file does for windows?

    It would save me allot of time if there is something like this I could use to my advantage. :rolleyes:
     
  2. vtel57

    vtel57

    Joined:
    Jul 12, 2004
    Messages:
    805
  3. jiml8

    jiml8 Guest

    Joined:
    Jul 2, 2005
    Messages:
    2,634
    Windows batch files are so spectacularly crippled...

    And Windows users have no idea how limited they are.

    Unix shell scripting is essentially limited only by your imagination and skill. The syntax is arcane, but once you've mastered it, you can do anything.
     
  4. jiml8

    jiml8 Guest

    Joined:
    Jul 2, 2005
    Messages:
    2,634
    A simple script I just have laying around from when I was playing with it a few years back...

    Code:
    #!/bin/bash
    #sample script to start a program, permit it to run for a predefined amount of wallclock time, then kill it.  Specify time in seconds
    #
    progID="gmplayer"
    $progID &
    mypid=`eval ps ax|grep "$progID"|grep -iv "grep"| awk '{print $1}'`
    echo "mypid $mypid"
    maxtime="5"
    mins=0
    secs=0
    killmins=5
    startsecs=`eval date | awk '{print $4}'| awk -F\: '{print $3}'`
    starthours=`eval date |awk '{print $4}'| awk -F\: '{print $1}'`
    startmins=`eval date |awk '{print $4}'| awk -F\: '{print $2}'`
    while [ $secs -lt $killmins ]; do
      cursecs=`eval date | awk '{print $4}'| awk -F\: '{print $3}'`
      curhours=`eval date |awk '{print $4}'| awk -F\: '{print $1}'`
      curmins=`eval date |awk '{print $4}'| awk -F\: '{print $2}'`
      secs=$(( (curhours-starthours)*3600 + (curmins-startmins)*60 - startsecs + cursecs ))
      sleep 1
    done
    kill "$mypid"
     
  5. saikee

    saikee

    Joined:
    Jun 11, 2004
    Messages:
    3,888
    I wouldn't be interested in Linux if it weren't for its terminal commands.

    One can write a simple script of just a few lines to create 130 partitions in a hard disk in seconds.
     
  6. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    I agree with you that Shell scripting in a Unix/Linux/BSD environment is quite awesome but I disagree with you on Windows users being limited.

    As far as scripting goes, Windows has 3 options. Batch, VBscript and now PowerShell.

    I said the same thing when I left the Linux world. I had a hard time figuring out a lot of stuff in Batch but a certain Truck Driver around here opened my mind and imagination to the possibilities of batch scripts. I now started scripting in PowerShell and it is quite awesome.
     
  7. jiml8

    jiml8 Guest

    Joined:
    Jul 2, 2005
    Messages:
    2,634
    I think that VBscript is more like perl or python in the *nix world; a scripting language that is native to the platform.

    The direct corollary to batch files in Windows is shell scripting in *nix. This is the environment that allows the construction of arbitrary "programs" that use arbitrary commands that are available in the system without special preparation. In this regard, shell scripting is light-years more advanced than Windows batch files.

    I have just deployed Win 7 and just discovered PowerShell. Haven't looked very far into it yet, but it does look promising. So far, I don't really know anything about it, except that it is new with Win7.
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    Not new with Windows 7. Just comes pre-installed with Windows 7. Powershell was released when Vista was released but you had to download and install it. It also works on Windows XP.
     
  9. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    One more thing I wanted to add.
    In the world of Unix/Linux/BSD, there are lot more commands available outside of the SHELL's built-in commands then what Windows has and unix shells have more built-in commands then what good old CMD has.

    Grep isn't a built-in command to the shell itself but you will find it installed in most Unix and Linux distributions.

    You can get ports of a lot of the old Unix shell commands for Windows. I use to use many of them when I was still trying to learn how to write things in pure batch. I have weened myself off of most of them but still use them from time to time.
     
  10. jiml8

    jiml8 Guest

    Joined:
    Jul 2, 2005
    Messages:
    2,634
    I actually go the other way, and on those rare occasions that I deploy Windows in a standalone box, I also deploy cygwin in order to have access to a full-up *nix environment. This lets me use things like sshd and ftpd from within Windows.
     
  11. saikee

    saikee

    Joined:
    Jun 11, 2004
    Messages:
    3,888
    Squashman,

    Don't bother much with the terms. What exactly is the Power Shell in Win7? I just use the Command Prompt the same way I did with Dos. It can be called Recovery Console in XP if booted up from a CD but Win7/Vista just call it Command prompt. The version booted from a CD or DVD is generally more powerful than the one embedded in the operating system.

    I think the Unix/Linux style of script files is much more powerful and extensive as a user can do virtually anything whereas in a MS system there is various restrictions and protections.
     
  12. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/918701

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice