Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the uses of the exec command in shell scripts? [closed]

Tags:

shell

unix

exec

Can anyone explain what are the uses of the exec command in shell scripting with simple examples?

like image 542
user2400564 Avatar asked Aug 21 '13 07:08

user2400564


People also ask

What is the use of exec command in Linux?

exec command in Linux is used to execute a command from the bash itself. This command does not create a new process it just replaces the bash with the command to be executed. If the exec command is successful, it does not return to the calling process.

How do I close a shell script?

To end a shell script and set its exit status, use the exit command. Give exit the exit status that your script should have. If it has no explicit status, it will exit with the status of the last command run.

What happens when you execute a command in the shell?

Running the CommandThe shell makes a copy of itself, a process called forking. This copy of the shell replaces itself with the command, with all of the arguments that were processed earlier. This is known as an "exec," and the combined process is known as "fork-and-exec."

What does exec $@ mean?

exec "$@" is typically used to make the entrypoint a pass through that then runs the docker command. It will replace the current running shell with the command that "$@" is pointing to. By default, that variable points to the command line arguments.


1 Answers

The exec built-in command mirrors functions in the kernel, there are a family of them based on execve, which is usually called from C.

exec replaces the current program in the current process, without forking a new process. It is not something you would use in every script you write, but it comes in handy on occasion. Here are some scenarios I have used it;

  1. We want the user to run a specific application program without access to the shell. We could change the sign-in program in /etc/passwd, but maybe we want environment setting to be used from start-up files. So, in (say) .profile, the last statement says something like:

     exec appln-program 

    so now there is no shell to go back to. Even if appln-program crashes, the end-user cannot get to a shell, because it is not there - the exec replaced it.

  2. We want to use a different shell to the one in /etc/passwd. Stupid as it may seem, some sites do not allow users to alter their sign-in shell. One site I know had everyone start with csh, and everyone just put into their .login (csh start-up file) a call to ksh. While that worked, it left a stray csh process running, and the logout was two stage which could get confusing. So we changed it to exec ksh which just replaced the c-shell program with the korn shell, and made everything simpler (there are other issues with this, such as the fact that the ksh is not a login-shell).

  3. Just to save processes. If we call prog1 -> prog2 -> prog3 -> prog4 etc. and never go back, then make each call an exec. It saves resources (not much, admittedly, unless repeated) and makes shutdown simplier.

You have obviously seen exec used somewhere, perhaps if you showed the code that's bugging you we could justify its use.

Edit: I realised that my answer above is incomplete. There are two uses of exec in shells like ksh and bash - used for opening file descriptors. Here are some examples:

exec 3< thisfile          # open "thisfile" for reading on file descriptor 3 exec 4> thatfile          # open "thatfile" for writing on file descriptor 4 exec 8<> tother           # open "tother" for reading and writing on fd 8 exec 6>> other            # open "other" for appending on file descriptor 6 exec 5<&0                 # copy read file descriptor 0 onto file descriptor 5 exec 7>&4                 # copy write file descriptor 4 onto 7 exec 3<&-                 # close the read file descriptor 3 exec 6>&-                 # close the write file descriptor 6 

Note that spacing is very important here. If you place a space between the fd number and the redirection symbol then exec reverts to the original meaning:

  exec 3 < thisfile       # oops, overwrite the current program with command "3" 

There are several ways you can use these, on ksh use read -u or print -u, on bash, for example:

read <&3 echo stuff >&4 
like image 108
cdarke Avatar answered Oct 16 '22 16:10

cdarke