What is called in the Windows world a "batch" file, is called a "script" in *NIX world. For the most part, scripts are a file with multiple commands or commands arranged in specific ways. Here's couple of things you should know:
Basic structure
Typically you will see something like this:
#!/bin/sh
# Comments
printf "Hello world"
First line specifies an interpreter ( commonly refereed to as shebang
line ), second is a comment ( anything after #
is not interpreted, just plain text ), third one is actual command
In terminal you can call a file without the shebang line. That will cause the script to be read and interpreted by your current shell ( aka command interpreter ). For instance, I am running mksh
shell. I specify the #!/bin/sh
it will be ran by Ubuntu's default shell - dash
. Every shell has some specifics about its' syntax of commands, hence you have to tailor your script accordingly.
Writing script
This has to be done in text editor, but you could also write it in LibreOffice Writer and then save it in plain text. Personally , I just use command line text editors (nano
for the most part or vim
); analogy here would be edit
in pre-Windows 7 versions of Windows.
File names and extensions don't matter on *NIX systems , as the system reads the file's first several bytes to determine its type. I got into habbit adding .sh
extension, but really - it's unnecessary.
Make script executable
That is made using chmod 755 scriptName.sh
command. It means read-write-execute
permissions for owner (you) read-execute
for users who are in owner's group (for instance file owner could be admin
, so if you belong to admin
group, you can read and execute the file ), and read-execute
for anybody else. Never give write permissions to others ! If you have super user privileges that could result into some bad juju ( aka you could get hacked, and malicious user will change your scripts so that it stills your personal info ).
Running scripts
You could always run a script by navigating to the script location in terminal and typing ./scriptName
.
To run any command anywhere, including scripts, they must be stored somewhere on the system that is included into the $PATH
variable. Here's what I mean:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/xieerqi/bin
$PATH
variable tells me that if a script or executable binary file is located in /usr/local/sbin
or /usr/local/bin
or any other of those directories, I can run that command just by typing it into the terminal.
Now, for your own scripts, you should make bin
folder, and store the scripts there. If you are using bash
your .profile
configuration file contains the following lines:
# set PATH so it includes user's private bin if it exists
#if [ -d "$HOME/bin" ] ; then
# PATH="$HOME/bin:$PATH"
#fi
If you uncomment (remove #
) in front of last three lines (from if
to fi
), your personal bin
folder will be included into the $PATH
variable.
exit
as the last command is unnecessary, the script isn't going to hang around waiting for you, it will exit when the last command exits.