3

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...

  1. Where do I have to save it?

  2. Which format? Which name and extension?

  3. 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!

  • 2
    linuxcommand.org . You do not use sudo in the script, so no sudo -i or sudo. You write the script, save it ion ~/bin and call it with sudo script_name – Panther Nov 9 '15 at 0:06
  • Specifically: linuxcommand.org/lc3_wss0010.php The name and extension of the file are not important. You can save it anywhere that you can execute it. You can run it with: ./myscript. – tweej Nov 9 '15 at 0:10
  • 1
    This is Linux. We don't use "batch" files, we use "scripts". An 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. – muru Nov 9 '15 at 0:16
  • Thank you so much for all your data!!! You have given me a lot of new knowledge. Thank you so much! – Juan Nov 9 '15 at 3:10
  • Please accept an answer if one of them has answered your question @Juan – Gavin Morton Nov 28 '19 at 2:03
2

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.

  • Thank you so much!!! I'll try to follow all your helping tips and I'll comment if I get my target. Thank you so much!!! Blessings!!! – Juan Nov 9 '15 at 22:06
10

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:

#!/path/to/interpreter

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:

#!/bin/bash

# the line above sets bash as the interpreter

# note that "sudo" is not required here because we will run the script as super user later.

apt-get update
# updates APT repositories

apt-get upgrade
# check for software upgrades and upgrade them

apt-get autoremove
# remove obsolete packages

apt-get autoclean
# remove stored .deb files

apt-get check
# update package cache and check for broken dependencies

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 +x script.sh

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:

sudo ./script.sh

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).

  • Thank you so much for all your data!!! You have given me a lot of new knowledge. Thank you so much! – Juan Nov 9 '15 at 3:08
  • I wrote it, with Leafpad, and I saved it into the Documents folder. Then I opened an LXTerminal and used sudo, but the system tells me: "Order not found." What's wrong here? – Juan Nov 9 '15 at 3:27
  • 1
    You have to make the script execuable – A.B. Nov 9 '15 at 6:25
  • "We'll run the script as super user, so there's no need to change file permissions." is wrong, the script must be executable by someone (owner, owner group or others, depending on the ownership of the file, unless you're calling the interpreter directly, e.g. sudo bash script.sh). – kos Nov 9 '15 at 12:38
  • @Juan Probably your terminal doesn't know where you saved your script. Let's say it's stored in /home/juan/Documents/script.sh. When we write ./script.sh, we're saying "the file script.sh in .", where . is the "working directory" of the terminal. You can check the value of . by running pwd. If it doesn't match the location of your script, you can change it with the cd command, eg. cd /home/juan/Documents. Then running ./script.sh (or whatever you called it) should work. You could also have written the full path, eg. /home/juan/Documents/script.sh instead of ./script.sh. – Warbo Nov 9 '15 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.