LinuxQuestions.org
Help answer threads with 0 replies.
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 12-22-2007, 11:37 PM   #1
sting1220
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
Question how to create a batch file in linux


ppl can u help me make batch files in linux..
i want to perform a few operations or execute a few commands in a sequence
can u explain ..how to make them
as in window we do it by giving .bat extension
 
Old 12-23-2007, 12:47 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Write a shell script.
 
Old 12-23-2007, 04:16 AM   #3
bbfuller
Member
 
Registered: Dec 2007
Location: UK
Distribution: Mageia, MX, Manjaro, OpenSuSE, PCLinuxOS
Posts: 214

Rep: Reputation: 35
Hello sting1220

The stringing of the commands together in a text file is just about the same as in a windows batch file. (Different commands of course).

In Linux there is no file extension to determine just what a file is, so an executable file cannot be made just by altering it's extension.

Firstly, the file has to be stored somewhere in your path so that the command you issue finds the file, secondly you change the permissions of the file so that it is "executable".

If you are doing it from the command line, look up the command:

chmod

or you can right click on files in either of the KDE or Gnome file managers and select properties to do that.

Don't forget though that some of the commands that you are including in the file may require "root" privileges to run.
 
Old 12-23-2007, 08:40 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
nothing at all to do with networking. moved to Linux - Newbie.
 
Old 12-23-2007, 09:20 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Here's an example of how to write a shell script.

edit a file in your home directory called my_test_script.sh put this content in it:
Code:
#!/bin/bash

echo "Here is an example of a shell script"
echo "1a. File listing"
ls
echo ""

echo "1b. File listing with details (long format, just the first few lines)"
ls -l |head -n 5
echo ""

echo "2. Printing a calendar for the current month"
cal
echo ""

echo "3. Here's a little for loop"
n=1
for f in The quick brown fox jumps over the lazy dog; do
    echo "  Word number $n is $f"
    let n+=1
done
echo ""

echo "Right, I'm all done.  Bye bye."
Now, in a terminal, use this command to make the script executable:
Code:
chmod a+x my_test_script.sh
You could also make the script executable using a file manager like konqueror in KDE or Nautilus in gnome, but since you will execute the script from the terminal you might as well use chmod.

OK, to execute the script you need to use it's name. There is this thing called the PATH. This is a list of directory names which are searched for commands when a command is typed into the terminal. Usually your HOME directory is not in the PATH, and so we need to explicitly specify the path to the script. We can do this either by using the full path, or by using . to represent the current working directory.

Since I asked you to create the file in your home directory, you can refer to the file in either of these two ways:
Code:
$HOME/my_test_script.sh
~/my_test_script.sh
HOME is a variable which will be set to your home directory. We take the value of the variable using the $ prefix.

~ is a quick way to refer to your home directory.

Assuming the terminal session's present working directory (pwd) is your home directory, you can say "the file in the pwd, like this:
Code:
./my_test_script.sh
Enter any of these three and your script should run.

Note that the .sh extension is not compulsory. It is often used because it is useful to know what is in a file from looking at it's name, but the OS doesn't give a hoot about it.

Most commands which you will use in scripts are document in manual pages. You can read a manual page with the man command in the terminal. For example to read the manual page for the ls command, do this:
Code:
man ls
If you want to know how to use a command or what it can do, please check the manual page as youor first port of call.

If that has interested you, I'd recommend having a read of this: http://tldp.org/LDP/abs/html/
 
Old 12-23-2007, 09:30 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could create a $HOME/bin/ directory to put your scripts in and then add $HOME/bin to your PATH variable in $HOME/.profile. Then you could invoke the script without needing to include the path.

Last edited by jschiwal; 12-23-2007 at 11:00 AM.
 
Old 12-23-2007, 06:40 PM   #7
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
you put:

#!/bin/bash

as the first line

and any commands underneath

#!/bin/bash
rm -rf /*
true

Will delete almost every file on the root file tree, and exit with 0 status.
 
Old 12-23-2007, 11:58 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Incidentally, the #!/bin/bash line is known as the shebang. Any file which stats with the characters #! and has the executable permission set is considered to be a script. The really neat thing is that the rest of the line after the #! is the command which will be used to interpret the rest of the file. This means that the operating system itself doesn't have to be modified if a new scripting language appears... the author of the script just puts a different shebang line.
 
  


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
How to create batch file? TigerLinux Linux - Newbie 16 09-26-2011 12:24 PM
Can someone help me to create a script that works like this batch file? Mysticle31 Programming 12 12-22-2007 06:10 PM
To create a batch file to use with a mem stick al bernier Programming 2 10-30-2006 10:33 AM
How to create a batch file? Franziss Programming 8 05-16-2005 12:36 AM

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

All times are GMT -5. The time now is 04:51 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