Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

closed as too broad by jasonwryan, Anthon, Bernhard, Chris Down, manatwork Jan 31 at 9:24

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

add comment

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer
    
Does the # symbol represent a comment? If so, why is the !/bin/bash commented out? –  evamvid Feb 1 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 at 23:09
add comment

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.

share|improve this answer
    
This is the best resource for getting started with bash! –  mahatmanich Jan 31 at 9:12
    
yeah - only used the TLDP for bash almost :-) –  Adionditsak Jan 31 at 9:18
1  
Actually this is much more thorough for a 'starter', same site but different entry :-) tldp.org/LDP/Bash-Beginners-Guide/html –  mahatmanich Jan 31 at 11:10
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

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