bash – ¿Qué hace un comando "ejecutivo"?

Pregunta:

No entiendo el comando bash exec . Lo he visto usado dentro de scripts para redirigir toda la salida a un archivo (como se ve en esto ). Pero no entiendo cómo funciona ni qué hace en general. He leído las páginas del manual pero no las entiendo.

Respuesta:

man bash dice:

exec [-cl] [-a name] [command [arguments]]
      If command is specified, it replaces the shell.  No new  process
      is  created.  The arguments become the arguments to command.  If
      the -l option is supplied,  the  shell  places  a  dash  at  the
      beginning  of  the  zeroth  argument passed to command.  This is
      what login(1) does.  The -c option causes command to be executed
      with  an empty environment.  If -a is supplied, the shell passes
      name as the zeroth argument to the executed command.  If command
      cannot  be  executed  for  some  reason, a non-interactive shell
      exits, unless the execfail shell option  is  enabled.   In  that
      case,  it returns failure.  An interactive shell returns failure
      if the file cannot be executed.  If command  is  not  specified,
      any  redirections  take  effect  in  the  current shell, and the
      return status is 0.  If there is a redirection error, the return
      status is 1.

Las dos últimas líneas son lo importante: si ejecuta exec por sí solo, sin un comando, simplemente hará que las redirecciones se apliquen al shell actual. Probablemente sepa que cuando ejecuta command > file , la salida del command se escribe en el file lugar de en su terminal (esto se llama redirección ). Si ejecuta exec > file lugar, entonces la redirección se aplica a todo el shell: cualquier salida producida por el shell se escribe en el file lugar de en su terminal. Por ejemplo aquí

bash-3.2$ bash
bash-3.2$ exec > file
bash-3.2$ date
bash-3.2$ exit
bash-3.2$ cat file
Thu 18 Sep 2014 23:56:25 CEST

Primero inicio un nuevo shell bash . Luego, en este nuevo shell ejecuto exec > file , de modo que toda la salida se redirija al file . De hecho, después de eso ejecuto la date pero no obtengo ningún resultado, porque el resultado se redirige al file . Luego salgo de mi shell (para que la redirección ya no se aplique) y veo que ese file contiene la salida del comando de date que ejecuté antes.

Leave a Comment

Your email address will not be published. Required fields are marked *

web tasarım