Scripts en Bash

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

Script básico

La morfología de un script en bash está dada por las siguientes partes:

  • Intérprete: #!/bin/bash
  • Comentario básico de funcionamiento # sctipt que deja la consola en fondo azul
  • Comandos a ejecutar
  • Código de salida (es opcional, caso contrario el script sale con el código del último comando ejecutado)
#!/bin/bash
# sctipt que deja la consola en fondo azul
setterm -background blue
echo It is a blue day
exit 1

Ejecución del script

Para correr el script se dos posibilidades:

  • Method 1:
    • Make the script executable: chmod 700 colorme
    • Try to run the script by typing the command: colorme
    • You will get the error message: command not found
    • Remember, a unix system will only only look for commands or scripts in the directories in your search path. So the system looks for the “colorme” command in the directories /usr/bin and /bin, doesn't find it and returns the error message.
    • Run the script with the command: ./colorme which means: run colorme from the current directory
  • Method 2:
    • If you are getting error messages when you run the script, you can trace the lines as they execute using the command: bash -v colorme
    • As the script executes, each line is displayed on the screen so that you know exactly what your script is doing.
  1. Using variables in a script

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
  1. Getting user input

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
  1. Passing Parameters on the command line:

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:

  1. Write a script called checking that displays information about a specified user.

The script should:

  • display a prompt asking for the username
  • read the user input
  • finger the user
  • display any results from the who command about this user only
  • display any results from the ps command about this user only

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.

  1. Code a script called dirchk that displays data about the current directory. The script should:
    • display a count of the number of subdirectories of this directory.
    • display a count of the number of files in the directory.
    • list all of the files in the directory that are zero length (use an option of the find command to do this)
    • use du to display the amount of storage space used by this directory

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.

 
documentos/bash/bash_script.txt · Última modificación: 2008/12/21 19:43 (editor externo)
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki