How can I write, and use after that, a typical text batch command file?
I'm talking about a file which has the following commands:
sudo -i
sudo apt-get update
sudo apt-get upgrade
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get check
exit
I guess I can write it with Leafpad, but...
Where do I have to save it?
Which format? Which name and extension?
How can I use it, after that, in a LXTerminal session, with only one order (ideally if there would be a way to use it automatically each time the system is starting)?
Thanks a lot for your time!
There are no "batch" files in the unix-like world. We use scripts containing "she-bangs". To make a script like this, open up a text editor (like Leafpad) and start with a line like this:
Where
/path/to/interpreter
is the location of the binary that will be used to interpret your script. In Linux we usually use "bash" (Bourne Again SHell), an "evolution" of the good ol' Bourne Shell. What bash does is basically running binaries with arguments specified in the script from paths included in the PATH environment variable (it usually includes /bin, /sbin, /usr/bin, /usr/sbin and others).But you can also use other interpreters such as Python, Perl, Ruby, sh, csh, zsh, dash, ksh, besides tons of options.
Below the she-bang comes your script. In your specific case, to run the specified commands, we can use bash, in a script like this:
Now save your file. There is no need for an extension, but for symbolic reasons, name it with a .sh extension. We'll run the script as super user, so there's no need to change file permissions.
Open a terminal and cd to the path where you saved your script. Let's say you saved it as script.sh. Run the following command:
chmod
means "change mode", and is used to change who can do what with your script.+x
adds the execute permission for all users, which allows anyone to run the script as though it's any other program. I won't go into a full list of what all the different permissions mean, but this question summarises them fairly well. After that, you can type:Don't forget the ./ part and don't leave any spaces between the ., the / and the s. The
sudo
command will run./script.sh
with root privileges, so your password will be required (you have to be the system admin, of course).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:
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 commandIn 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 orvim
); analogy here would beedit
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 meansread-write-execute
permissions for owner (you)read-execute
for users who are in owner's group (for instance file owner could beadmin
, so if you belong toadmin
group, you can read and execute the file ), andread-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:$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 usingbash
your.profile
configuration file contains the following lines:If you uncomment (remove
#
) in front of last three lines (fromif
tofi
), your personalbin
folder will be included into the$PATH
variable.