Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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).
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':
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 ?
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.
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.
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.
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,646
Rep:
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
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.