exec command help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exec command help
# 1  
Old 03-25-2012
exec command help

Hi,

I have the following lines in a script :

Code:
.
.
exec < some_file
.
.
.

I have very little idea about exec command. I would like to know what this does and what will happen if the file some_file does not exist. Specifically, I would like to know whether the lines following this line will be interpreted if some_file does not exist.
# 2  
Old 03-25-2012
There are broadly speaking two distinct uses of exec:
  1. with a command. This exits the script and executes the command. The lines following the exec statement will not be executed.
  2. with a redirection and without a command. In this case stdin takes it input from "some_file". The lines following exec will be executed while stdin remains redirected.
This is irrespective of whether "some_file" exists or not.. *not entirely correct see post below*
So the answer is: use nr. 2. is the case here and the statements below exec will get executed whether or not the file exists...

Last edited by Scrutinizer; 03-25-2012 at 10:06 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-25-2012
Quote:
Originally Posted by Scrutinizer
There are broadly speaking two distinct uses of exec:
  1. with a command. This exits the script and executes the command. The lines following the exec statement will not be executed.
  2. with a redirection and without a command. In this case stdin takes it input from "some_file". The lines following will be executed while stdin remains redirected.
This is irrespective of whether "some_file" exists or not..
So the answer is option 2. is used and the statements below exec will get executed whether or not the file exists...
Thanks Scrutinizer for the info on exec...

But, I still have a doubt...

To test it, I made a script file named "test2" having the following lines :

Code:
#!/usr/bin/ksh
exec < test123
echo Hi

As per your explanation, "Hi" should be echoed irrespective of whether test123 exists or not. But, when I run "test2" ( with no file named "test123" in the current directory), I get the following output:

Code:
A file or directory in the path name does not exist.
test2[2]: test123: 0403-016 Cannot find or open the file.

"Hi" is not at all echoed........
# 4  
Old 03-25-2012
You are right, If the file does not exist in use nr. 2, then POSIX compliant shells should exit the script, so that the lines following exec will be executed.

I had only quickly tested it with bash:

Code:
bash ./test2
./test2: line 2: ./test123: No such file or directory
Hi

But in ksh:
Code:
ksh ./test2
./test2[2]: ./test123: cannot open [No such file or directory]

and in dash:
Code:
$ dash ./test2
./test2: 2: ./test1231c: cannot open ./test123: No such file

According to POSIX "exec" is a so-called "special built-in utility", and there is a redirection error, therefore the script should exit ( Shell Command Language: Consequences of Shell Errors )
Special Built-in utilities are:
Code:
break
: (colon)
continue
. (dot)
eval
exec
exit
export
readonly
return
set
shift
times
trap
unset

The reason that bash worked, is that is not fully POSIX compliant by default:
Code:
bash ./test2
./test2: line 2: ./test123: No such file or directory
Hi

Code:
$ bash --posix ./test2
./test2: line 2: ./test123: No such file or directory

See: Bash POSIX Mode, item #18. Bash Reference Manual

Last edited by Scrutinizer; 03-25-2012 at 10:35 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-25-2012
Even with --posix, Bash is not fully POSIX complaint and makes not claim to be. All that flag does is cause Bash "to conform more closely to the POSIX standard."
# 6  
Old 03-25-2012
A good point, but IMO it goes a bit further than that, and the non-compliance is limited and specific: the same page also says:
Quote:
There is other posix behavior that Bash does not implement by default even when in posix mode. Specifically:

The fc builtin checks $EDITOR as a program to edit history entries if FCEDIT is unset, rather than defaulting directly to ed. fc uses ed if EDITOR is unset.
As noted above, Bash requires the xpg_echo option to be enabled for the echo builtin to be fully conformant.
And also:
Code:
Bash can be configured to be posix-conformant by default, by specifying the --enable-strict-posix-default to configure when building (see Optional Features).

And from my manage..
Quote:
Bash is intended to be a conformant implementation of the Shell and Utilities portion of the IEEE POSIX specification (IEEE Standard 1003.1). Bash can be configured to be POSIX-conformant by default.

Last edited by Scrutinizer; 03-25-2012 at 11:17 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Exec command - what is it doing here?

Hi all, Forgive me for asking for help with my first post, but I am struggling here. I've been asked to translate a bash script into a Windows script (probably batch or powershell, not sure yet), so the first step is obviously understand what the bash script is doing. But I have no experience in... (5 Replies)
Discussion started by: el_foz
5 Replies

3. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

4. UNIX for Dummies Questions & Answers

Exec command

Hi can some one explain the following command , It would really help if some can really elloborate on what is happening out here export PATH | exec /bin/sh ./auto_approve :q P.S: This is the first time i am using exec ,so an elloboration what does it do and what is the use of the :q will be... (1 Reply)
Discussion started by: Sri3001
1 Replies

5. Shell Programming and Scripting

exec command

How can I use the exec command to log my korn shell session to the screen and the log file? Currently I have this command: $exec 1> ${LOG} 2>&1 This logs the output to the log file only. I want it to go to the screen also. Is this possible with this command? thanks. (10 Replies)
Discussion started by: djehresmann
10 Replies

6. Shell Programming and Scripting

exec command help

All, I am using below shell script to output the content to outputfile.txt. What I am looking for is in addition to outputfile.txt, I want the output to be on standard output too. exec > outputfile.txt echo "Starting " echo "ending" (5 Replies)
Discussion started by: basic_shell
5 Replies

7. Shell Programming and Scripting

exec command

can any one pls explain the meaning of exec 1<&5 ?? its urgent (2 Replies)
Discussion started by: santosh1234
2 Replies

8. UNIX for Dummies Questions & Answers

Need help with -exec cp command.

I have a ksh script that contains the following: find /dir1/dir2 -type f -name "FILE.*" -newer /dir1/dir2/afterme.txt -exec cp /dir1/dir2/dir3 {} \; When I run it from the cli, it runs fine. When I run it from the ksh script I get find: missing argument to `-exec' I also tried -exec cp... (40 Replies)
Discussion started by: bbbngowc
40 Replies

9. Shell Programming and Scripting

exec command

hai i want know the difference between two shell scripts those are 1) a=2004 echo $a #output------2004 exec < inputfile while read line do echo $a #output-------2004 a=2005 echo $line echo $a ... (1 Reply)
Discussion started by: g_s_r_c
1 Replies

10. UNIX for Dummies Questions & Answers

using the -exec command

linux redhat 8.0 I am getting accustomed to using the -exec command to get around my databse.. and use it to edit and update files..! is this more apllicable than jumping from one directory to the other.. I have set up the databse so that the inode #'s are accessable and can get me from one... (0 Replies)
Discussion started by: moxxx68
0 Replies
Login or Register to Ask a Question