Does linux have something simalar to batch files?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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:
 

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.
 

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"
 
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.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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.
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.
 

jiml8

Guest
Joined
Jul 2, 2005
Messages
2,634
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.
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.
 

Squashman

Retired Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
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.
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.
 

Squashman

Retired 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.
 

jiml8

Guest
Joined
Jul 2, 2005
Messages
2,634
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.
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.
 
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.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top