LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-15-2008, 05:59 AM   #1
rajiv.birari
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Rep: Reputation: 0
Can I write batch file


Hello
Can I write script file(batch) same as window batch file on linux

regards
RKB
 
Old 10-15-2008, 06:01 AM   #2
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,631

Rep: Reputation: Disabled
Yes you can write the script that will do what you want but with linux commands ofcourse!!!
If you want some thing to do oftenly, you may want to look at cron.
 
Old 10-15-2008, 06:05 AM   #3
rajiv.birari
LQ Newbie
 
Registered: Oct 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by linuxlover.chaitanya View Post
Yes you can write the script that will do what you want but with linux commands ofcourse!!!
If you want some thing to do oftenly, you may want to look at cron.
How !
I am new for Linux
Can u give me some examples
 
Old 10-15-2008, 06:18 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The "Advanced Bash Scripting Guide" book in the www.tldp.org website has hundreds of examples. The entire book consists of well commented examples making it easier to learn than from a reference guide. Don't let the word Advanced scare you off. Thorough would be a better description.
 
Old 10-15-2008, 06:23 AM   #5
mobinskariya
Member
 
Registered: Sep 2007
Location: Kerala, India
Distribution: ubuntu 10.04
Posts: 367

Rep: Reputation: 55
Thumbs up

assuming that you do know basic bash commands.
try this
open a text editor.enter the following
Code:
#!/bin/bash
pwd
ls -l
assuming that you have saved it as test.sh
then make it an executable by command
Code:
chmod 777 test.sh

then type in bash
Code:
./test.sh

then you will understand everything
 
Old 10-15-2008, 10:48 AM   #6
cygnal
LQ Newbie
 
Registered: May 2007
Distribution: Slackware/Debian
Posts: 26

Rep: Reputation: 15
Linux shell scripts are the equivalent of windows batch scripts, they are really no more than a series of commands in a text file. In particular, they have a call to the shell they are supposed to use (linux has a number of different shells available, with different functionalities and syntaxes), which will look like

#!/bin/bash

The #! tells the system that what follows is the path to the shell that should run the commands contained in the script. /bin/bash is the location of the bash shell, the linux standard. Take for example the series of commmands:

ls
pwd
whoami

The first command, ls, gives a listing of files in the current directory. The pwd command prints the path of the directory you are currently located in. The whoami command returns your username. A shell script to execute those commands would look like this:

Code:
#!/bin/bash
ls
pwd
whoami
So far it's just a text file; it needs to have execute permission added to it. The command

Code:
chmod +x shellfile
Where 'shellfile' is the name of the script, the file containing the series of commands. The script can now be executed by passing the command
Code:
./shellfile
from the directory where the script resides. You can also pass the absolute path, for example /home/bob/shellfile. The results and output of the commands in the script will be displayed on your screen.

Now, I kind of lied when I said that it needs execute permission to be run; an alternative to adding execute permission to a file can be to use the source command, as in

source shellfile

or

. shellfile

(the . is short for 'source'). You may also forego the inclusion of the #!/bin/bash call on the first line and invoke your script with

bash shellfile

Keep in mind too that ANYONE, including root, attempting to execute a file that does not have execute permission set (either by ./shellfile or /home/bob/shellfile) will get a "permission denied" error. Many shell script files will also be named with a .sh extension, or one that corresponds to the shell that handles them, such as .ksh, .bash, .zsh, etc, although this is not strictly necessary.
 
Old 10-15-2008, 01:56 PM   #7
evaluatinglinux
Member
 
Registered: Oct 2008
Posts: 45

Rep: Reputation: 15
Google for Unix Shell Scripting. It totally rocks. Makes Windows Batch Scripting look like a cheap gimmick!


Debian Kernel

Last edited by evaluatinglinux; 10-25-2008 at 02:51 AM.
 
Old 10-15-2008, 02:11 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by jschiwal View Post
The "Advanced Bash Scripting Guide" book in the www.tldp.org website has hundreds of examples. The entire book consists of well commented examples making it easier to learn than from a reference guide. Don't let the word Advanced scare you off. Thorough would be a better description.
This book is a must read if you are serious about learning shell scripting. Despite of it's title, it's suitable for beginners as well as advanced users. However, note that linux shells (any of them) are far far more feature rich than command.com, also, linux (and unix like OSes in general) have a very big set of tools that have been programmed in ways that they can be often connected to operate with the output of each other tools. That adds a lot of possibilities, the set of available tools is much bigger than those available under DOS or Windows command line as well.

So, be prepared to learn.

Last edited by i92guboj; 10-15-2008 at 02:12 PM.
 
Old 10-15-2008, 02:30 PM   #9
john test
Member
 
Registered: Jul 2008
Distribution: ubuntu 9.10
Posts: 527
Blog Entries: 1

Rep: Reputation: 35
Here you go - just what you need
http://tldp.org/LDP/abs/html/dosbatch.html#VIEWDAT
has conversion tables for dos batch commands to Baxh shell commands
and an example of converting viewdata.bat to viewdata.sh
Good luck
 
Old 10-15-2008, 07:54 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You may also find this useful: http://rute.2038bug.com/index.html.gz
 
Old 10-16-2008, 06:05 AM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by cygnal View Post
Now, I kind of lied when I said that it needs execute permission to be run; an alternative to adding execute permission to a file can be to use the source command, as in

source shellfile

or

. shellfile
Note that sourcing a script and running it are not the same thing. I don't want to disrupt the thread so I will not enumerate the differences here, but this has many implications that can misguide the OP. Enough to say that the environment will be different, and that the script can change your working environment, producing lots of funky effects once the script is terminated.

Neither of these ways is more or less correct. Just be aware that they are not the same, and they might be better or worse depending on the concrete case. I just wanted to say: don't assume that they all are equivalent ways to launch a script. They aren't.

Last edited by i92guboj; 10-16-2008 at 06:06 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
batch file for accessing a shared file in ubuntu samba pdelcast Linux - Networking 5 04-24-2008 12:21 AM
batch file to convert every file in a directory with mencoder. toben Linux - Software 1 01-31-2008 05:28 PM
how to write a batch file to make a program run during boot up in windows??? b0nd Programming 7 09-04-2006 06:16 AM
How do I write a script (batch file)?? cvzyl Linux - Software 4 07-30-2006 09:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration