2

I need a way to be able to run a script that will automate terminal commands, one after another. As I understand it, this is exactly what bash does. (Am I right, even on this count?)

Where can I learn, basically, how to set them up?

Also, how can I make one run at startup?

PS Are the shell and the terminal/command line the same?

4 Answers 4

4

Where can I learn, basically, how to set them up?

For setting up a script which executes commands, just create a file (e.g. MyCmds.sh) with your editor and write some commands, which you want to execute. E.g.:

#!/bin/bash
# This is a quote, the next line will output "Hello World"
echo "Hello World"

Now you need to make this file executable by setting the x flag (use your terminal for this):

$ chmod +x MyCmds.sh

Now you can run it like this in your terminal (do not forget the leading ./):

$ ./MyCmds.sh

You should see Hello World.

Also, how can I make one run at startup?

To run this script at startup, you have to put the commands, or your script with the absolut path (e.g. /path/to/my/script/MyCmds.sh), into the textfile /etc/rc.local (There is sometimes an exit 0 at the end, your commands have be above that line).

PS Are the shell and the terminal/command line the same?

Yes (and maybe No). I assume that you mean the normal command line where you type in your commands and the with shell the sripts, am I right? It always depends on your setup. If you run a script with #!/bin/bash (the first two signes are called shebang) in the first line, every following line will be passed to the shell-program located at /bin/bash. What you see as your command line is maybe /bin/bash or /bin/shell or ... (They differ by their features). You can check this by looking at the file /etc/passwd. After your linux username there should stand the program for your command line.

2
  • Does the # symbol represent a comment? If so, why is the !/bin/bash commented out?
    – evamvid
    Feb 1, 2014 at 18:52
  • 1
    Yes and No. The first line is an exception. The two signes #! (called shebang) introduce the programm for the interpretation of the script. In my case, it is /bin/bash. You can also run python code like this by writing #!/usr/bin/python in the first line, and then some print('Hello World') into the next line.
    – Tik0
    Feb 1, 2014 at 23:09
4

I would like to recommend: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

It walks you through anything from very basics (goes from hello world example) to more advanced constructions, and it is a part of the The Linux Documentation Project.

3
3

bash is a shell, or commandline interpreter. A shell script is a text file containing a list of commands to be executed by a given shell (comparable to a batch file, if you are familiar with that parlance).

A shell script can use any commandline interpreter (or shell) its author wishes to use by specifying it on the first line. For example, if a given script uses bash, its first line will typically look like this:

#!/bin/bash

This would then be followed by a list of commands (generally speaking) just as you'd type them, in order, at an interactive shell.

With regard to your post scriptum: not quite. The shell (or commandline interpreter) is the program that handles the parsing of command lines, which are the things you enter, either interactively or into a script as described above.

3

There are numerous ways to get going but here is what I recommend you do:

  1. Make a directory to store your scripts:

    mkdir ~/bin
    
  2. Edit your ~/.bashrc ( I assume you know how to handle an editor), and add at the bottom:

    if [ -d ~/bin ] ; then
       export PATH=~/bin:"${PATH}"
    fi
    
  3. Logout and login again (or start a new terminal) and check if ~/bin is in your path:

    echo $PATH
    
  4. Create your first program ~/bin/echo_on_boot and put in

    #! /bin/bash
    echo "Started" > /var/tmp/written_on_startup
    
  5. change the permissions on the program so it will execute:

    chmod 750 ~/bin/echo_on_boot
    
  6. try the program:

    echo_on_boot
    
  7. check if /var/tmp/written_on_startup was created and delete it:

    cat /var/tmp/written_on_startup
    rm -f /var/tmp/written_on_startup
    
  8. edit your crontab with crontab -e, add a line (near the bottom usually), assuming that ~ is /home/vamvid (you cannot use ~ in the crontab file):

     @reboot /home/vamvid/bin/echo_on_boot
    
  9. reboot the system and see if the file is created in /var/tmp.

@reboot will not work if you home directory is e.g. on an encrypted drive as the ~/bin directory is then not available after reboot (only when you login).

Not the answer you're looking for? Browse other questions tagged .