2. Very simple Scripts

This HOW-TO will try to give you some hints about shell script programming strongly based on examples.

In this section you'll find some little scripts which will hopefully help you to understand some techniques.

2.1 Traditional hello world script

     #!/bin/bash     
     echo Hello World    
   

This script has only two lines. The first indicates the system which program to use to run the file.

The second line is the only action performed by this script, which prints 'Hello World' on the terminal.

If you get something like ./hello.sh: Command not found. Probably the first line '#!/bin/bash' is wrong, issue whereis bash or see 'finding bash' to see how sould you write this line.

2.2 A very simple backup script (tar ball)

   #!/bin/bash     
   tar -cZf /var/my-backup.tgz /home/me/
   

In this script, instead of printing a message on the terminal, we create a tar-ball of a user's home directory. This is NOT intended to be used, a more useful backup script is presented later in this document.

2.3 Use SCP to transfer files

 
    scp -r -i ~/key.pem $1 ubuntu@52.87.7.192:/home/ubuntu/$2

In this snippet, you use SCP to transfer a file ($1) to a remove server and remote directory ($2) It also uses a key.pem file for certificate authentication

 
    scp -r -i * ubuntu@52.87.7.192:/home/ubuntu/...

In this snippet, you use SCP to transfer all files [recursively by -r] to a remove server and remote directory, where ... is your folder name, if any