LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Total Newb question.. (https://www.linuxquestions.org/questions/linux-general-1/total-newb-question-64994/)

srgusa 06-11-2003 12:12 PM

Total Newb question..
 
Hi,

Sorry for the complete newbie question but I'm having trouble finding a coherent answer to this. I really just need a simple answer to this if that's possible.

Is there an equivalent in Unix/Linux to a DOS or Windows batch file (.bat, .cmd)?

I'm just trying to find out what type of file I need to create with maybe a few commands in it, that I can run from the command line without using Perl or SH or something.

In essence, I just want to create a file that will automate a series of command lines.

Thanks much!

acid_kewpie 06-11-2003 12:14 PM

linux LIVES on script files, which can do SO SO SO SO SOmuch more than a pitiful batch file ever could. have a look at the bash scripting howto at tldp.org or skim though the rute guide in my signature.

I'm sure you'll see how funny that question seems to someone who knows about scripting! ;)

roesingape 06-11-2003 12:16 PM

that's what SH is for...just make a shell script... download a simple one and customize it, or, write the commands all on one line in bash then whenever you want to do them just push arrow up until it appears. 9 times out of ten there's already something written to do what you want...hope this helps...I'm a newbie too..

srgusa 06-11-2003 12:25 PM

Yep I was able to figure out that if i put the lines i want into a text file, name it whatever with no extension, the do SH <filename> at the prompt that it runs with no problem.

The problem I have is, How do I run a file like that without typing SH first? I just want to RUN the file...ie. type the filename and thats it, no SH or anything. Is that possible?

MArgRes 06-11-2003 12:53 PM

Yes, you can. First you (obviously) have to have the execute bit set on the file. (Do an ls -l to find out.) If it isn't, you'll have to do a chmod to set it. If it is set, you have to put the file in your 'path' for you to be able to execute it directly. To find out your path, type 'echo $PATH'. It will show you a list of directories where you could put it to have the shell look there first. You can add a directory to your path by editing your .bash_profile file and putting something like:

PATH=PATH:$HOME/<your-dir>
export PATH

and then putting the file in the <your-dir> directory. As a side note, instead of typeing 'sh filename' to execute it, you can also type '. filename' (just a dot and then the file), and it does the same thing (as long as you're in the same dir as the file). :)

acid_kewpie 06-11-2003 01:01 PM

Quote:

PATH=PATH:$HOME/<your-dir>
export PATH
this is not really advisable. your standard path should not be changed unless this is unaviodable.

if you have a script file not on your path then you need to prefix it with the relative or absolute path. e.g ./script in your current directory.

the file does *not* need to be in your path to execute it at all, all that is required is that it is actaully set executable, by running "chmod +x scriptname"

srgusa 06-11-2003 01:19 PM

Wow you all are great. Thanks so much for the quick and thorough responses.

Unfortunately, I still see nothing that tells me how to RUN the file without typing anything but the file name.

ie. the script is called JUNK

I want to type:

JUNK

and have it run. Not ./JUNK or SH JUNK or .JUNK but just plain ol' JUNK.

Can I do that?

acid_kewpie 06-11-2003 01:23 PM

it is possible by changing the path, yes. but you shouldn't not *WANT* to do that. it is not the way things work. executable files are kept in /bin directories on your path somewhere. It may seem strange to suggest this, but you should do things properly, and call it in a way that may seem annoying to you, as that is the way to do it. the script file, when ready and completed would typically be put in a suitabel location.

chakkerz 06-12-2003 06:53 AM

you need to change the permissions on the file to make it executable, so change directory to where JUNK is located, then run
chmod a+x JUNK

to then subsequently run the file do a:
./JUNK when you are in the directory where JUNK is located

as for disliking ./JUNK
type ./J and hit TAB and it will complete the filename ... four keystorkes, just like typing JUNK


All times are GMT -5. The time now is 03:00 PM.