exec command help

Hi,

I have the following lines in a script :

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

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

1 Like

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 :

#!/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:

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

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:

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

But in ksh:

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

and in dash:

$ 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:

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:

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

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

1 Like

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

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:

And also:

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