Un script en bash es un archivo de texto que contiene una sucesión de comandos que serán interpretados y ejecutados por el shell (Bash).
La morfología de un script en bash está dada por las siguientes partes:
#!/bin/bash# sctipt que deja la consola en fondo azul#!/bin/bash # sctipt que deja la consola en fondo azul setterm -background blue echo It is a blue day exit 1
Para correr el script se dos posibilidades:
./colorme which means: run colorme from the current directory
Variables are created when you assign a value to them ( eg: COLOR=blue )
To use the variable, put a $ before the variable name. ( eg: echo $COLOR )
Modify the colorme script to use the color variable as follows:
#! /bin/bash COLOR=blue setterm -background $COLOR echo It is a $COLOR day
A script can get input from the user while it is running. Use the echo command to display a prompt on the screen and the read command to get the input.
#! /bin/bash echo -n "Pick a screen color (blue, yellow, red ): " read -e COLOR setterm -background $COLOR echo It is a $COLOR day
You can also pass parameters to the script on the command line. Bash will accept up to 9 parameters separated by spaces. The first parameter is $1, the second parameter is $2, etc. The colorme script using input parameters is shown below.
#! /bin/bash setterm -background $1 echo It is a $1 day
To run the script, use the command: colorme red
In this case, $1 will be given the value “red”.
Run the script again using the command: colorme blue
This time, $1 will have the value “blue”.
Exercises:
The script should:
Test your script using your own username. Then, login on another console as floopy, switch back to your own console and make sure that your script displays the correct data about floopy.
To test your script, you should create some subdirectories, some files that are zero length using touch and some files that are not zero length using vi, redirection or cp.