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:
with a command. This exits the script and executes the command. The lines following the exec statement will not be executed.
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...
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.
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
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).