LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-29-2005, 02:53 PM   #1
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 12.04 64-bit
Posts: 230

Rep: Reputation: 30
Linux equivalent of bat files


If have a run.bat file in Windows containg just dir and ran run.bat the directory contents would get listed.

I need the same thing to be done in Linux.
Whats Linux equivalent of .bat ?
And what do I have to do to run it ?

I know that I got to enter ls - though dir also works on most Linux distros.

Thanks
 
Old 12-29-2005, 02:56 PM   #2
dlackovic
Member
 
Registered: Nov 2005
Location: Erie Pa
Distribution: Suse 10.3, Ubuntu 9
Posts: 46

Rep: Reputation: 15
I think you can just make a file in any old text editor with all your commands that you want to run. Then when you open the terminal, type a ".", space, and then the location of the file.

like
> . ~/myfile

This would run a file called myfile located in your home directory.
 
Old 12-29-2005, 03:01 PM   #3
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 12.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
Code:
type a ".", space
This has special meaning in Linux ? Runs linux commands specified in the file ?

Searching lead me here and was thinking of shell - .sh ?
 
Old 12-29-2005, 03:08 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
You don't need a specific file extension to make a file executable, although a lot of people use extensions to tell what type of script it is (.sh shell script, .pl perl library, etc.). To make the equivalent of a .bat file in windows, you usually create a shell script and tell the operating system to make it executable. Basically:

Code:
touch some-script.sh
chmod u+x some-script.sh
Once the file is created, open it in a text editor and put the commands you want to run inside it. You have to tell the operating system what command interpreter you want to run the file, so the first line should contain the name of the command interpreter. For example, if you want your bash shell to run it, your first line will be:

Code:
#!/usr/bin/bash
If you wanted perl to run it, it would be:

Code:
#!/usr/bin/perl
The paths on your system may be different to mine (Slackware 10.2) - I think Fedora uses /bin/bash & /bin/perl for example.

Once this is done, the only thing left is to run the script. Usually you'd put the script in a bin directory that is in your path. Public scripts in /usr/local/bin, private scripts in a bin directory under your home directory, etc. If you just want to run the script in the directory where you're working with it, use:

Code:
./some-script.sh
The reason for this is that the current directory is not usually in your path.
 
Old 12-29-2005, 03:10 PM   #5
dlackovic
Member
 
Registered: Nov 2005
Location: Erie Pa
Distribution: Suse 10.3, Ubuntu 9
Posts: 46

Rep: Reputation: 15
Well, I'm working from memory, but I'm pretty sure that in the terminal a period means "execute the following program." so when you execute your file full of commands, it should run them just like a batch file.

try making a file called Test and saving it in your home directory. in this file, type the following:

pwd
cd ..
pwd
cd ~
pwd

Save that and go into your terminal. At the prompt, type:
. ~\Test

You should see
\username\home
\username
\username\home

Hope that helps.
 
Old 12-29-2005, 03:12 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
The bottom line is that ANY shell script you write is equivalent to a ".bat" file.

A shell script needs to have "x" execute permissions set (e.g. "chmod +rx myscript.sh"); the file suffix is irrelevant (although making up a convention like "*.sh" can be convenient).

A .bat file, on the other hand, must have a ".bat" extension (and, of course, DOS has no concept of "execute permissions". Windows NT and higher does ... but it's not really used except to RESTRICT execute permissions, not to GRANT them).

'Hope that helps .. PSM
 
Old 12-29-2005, 03:12 PM   #7
MidnightCmdr
LQ Newbie
 
Registered: Oct 2004
Location: Austin TX
Distribution: Debian
Posts: 17

Rep: Reputation: 0
What dlackovic is talking about is "sourcing" a shell file. That's different than running it. There are two basic ways to run an executable script. Let's assume your script is called 'script.sh':

1. /bin/bash script.sh
2. chmod +x script.sh; ./script.sh

One last detail; the first line of an executable script should be a "shebang line". This is a line that starts with

#![path to shell goes here]

Here's a small but complete example of an executable script (we'll call it 'test.sh'):

test.sh
-----------------------
#!/bin/bash

cp file1.ext file2.ext
mv file2.ext /tmp
-----------------------

Now make it executable...

chmod +x test.sh

and run it!

./test.sh

It will now execute the commands in the script.

Last edited by MidnightCmdr; 12-29-2005 at 03:14 PM.
 
Old 12-29-2005, 03:25 PM   #8
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 12.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
Im looking at /usr/bin and there are many files in this folder.
There is a perl file, php file - but no bash file - though there is a bashbug file.
Where could the bash file be ?

Last edited by anjanesh; 12-29-2005 at 03:27 PM.
 
Old 12-29-2005, 03:27 PM   #9
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by anjanesh
Im looking at /usr/bin and there are man files in this folder.
There is a perl file, php file - but no bash file - though there is a bashbug file.
Where could the bash file be ?
In /bin. On my system, /usr/bin/bash is a symlink to /bin/bash.
 
Old 12-29-2005, 03:29 PM   #10
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 124Reputation: 124
/bin/sh or /bin/bash
 
Old 12-29-2005, 03:29 PM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
No. Anjanesh's original question had nothing to do with the need to SOURCE ENVIRONMENT VARIABLES.

Using "." is a useful technique, and the fact that *nix shell scripts don't necessarily propagate environment properties (e.g. "set" or "cwd" commands) to either parent or child processes is a significant difference from DOS/Windows .bat files (where you can unwittingly mess up child or parent 9 ways from Sunday... ;-))

But I didn't want Anjanesh to jump to the (mistaken!) conclusion that one must use ".". The exact opposite, of course, is true. You *shouldn't* use the "." ("source") syntax unless you explicitly need to.
 
Old 12-29-2005, 03:56 PM   #12
anjanesh
Member
 
Registered: Sep 2004
Location: Navi Mumbai
Distribution: Ubuntu 12.04 64-bit
Posts: 230

Original Poster
Rep: Reputation: 30
Ok. bash is ther in /bin.
So my test.sh contains
Code:
#!/bin/bash

ls
Say I upload test.sh to /
chmod +x test.sh
./ test.sh

should list the directories+files of /, right ?

Let me try getting this uploaded and then I'll post back. So far no luck in getting this uploaded in WinSCP because it gives permission denied. Tried to copy to so many folders.
If I use putty I can su - and switch as root but no gui and figuiring out how to copy from my PC.
 
Old 12-29-2005, 04:17 PM   #13
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

Rep: Reputation: 164Reputation: 164
ls just lists the files/directories of the directory it is run from, unless you provide it with another location to start from:

Code:
#!/bin/bash

ls /
Either scp or sftp should let you upload a file to your Linux box. Do you have a user account (not root) on the Linux box?
 
Old 12-29-2005, 06:27 PM   #14
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by anjanesh
Ok. bash is ther in /bin.
So my test.sh contains
Code:
#!/bin/bash

ls
Say I upload test.sh to /
chmod +x test.sh
./ test.sh

should list the directories+files of /, right ?
No. You use the ./program-to-call (without space character in between!!) to run a program that can be found in the current directory (that's what the dot means here). Else you have to put the folder where the program is in your PATH variable or call it with full path (in your case "/test.sh" -- it's the same that you have to do with every MS-DOS batch file
 
Old 12-29-2005, 08:57 PM   #15
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 271Reputation: 271Reputation: 271
Another thing you can do if you just want to modify the existing commands is to set an alias in .bashrc. In your home directory, if you type ls -a you should see a file called .bashrc. Inside this file you can add aliases to commands. For example, if you wanted to use the command dir to list the contents of a directory, you could add alias dir='ls'

I think there's also a global .bashrc somewhere...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Is there an equivalent of autoexec.bat in debian? Akhran Debian 5 09-13-2005 06:58 PM
What is the autoexec.bat and config.sys equivalent of linux lemuel Linux - Newbie 4 12-07-2004 10:34 PM
Linux Startup - AutoExec.bat equivalent? perry Linux - Software 6 05-04-2004 12:13 AM
.bat file equivalent in linux? Gnute Linux - General 5 08-17-2003 10:21 AM
Autoexec.bat Equivalent dwpondscum Linux - Distributions 5 06-16-2003 10:51 AM


All times are GMT -5. The time now is 05:57 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration