Bash Script Function Return True-FalseUnderstanding “IFS= read -r line”BASH return to main...
Why are the books in the Game of Thrones citadel library shelved spine inwards?
How to deal with an incendiary email that was recalled
A title for a history book
Can a person refuse a presidential pardon?
In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?
Cat is tipping over bed-side lamps during the night
Porting Linux to another platform requirements
Non-Cancer terminal illness that can affect young (age 10-13) girls?
Making him into a bully (how to show mild violence)
If I delete my router's history can my ISP still provide it to my parents?
What are "industrial chops"?
What is the most fuel efficient way out of the Solar System?
Cookies - Should the toggles be on?
Why publish a research paper when a blog post or a lecture slide can have more citation count than a journal paper?
How can my powered armor quickly replace its ceramic plates?
Does a phylactery of a lich have to be a box?
A starship is travelling at 0.9c and collides with a small rock. Will it leave a clean hole through, or will more happen?
SET NOCOUNT Error in handling SQL call after upgrade
How can I get my players to come to the game session after agreeing to a date?
How much mayhem could I cause as a sentient fish?
What is a good reason for every spaceship to carry a weapon on board?
Dilemma of explaining to interviewer that he is the reason for declining second interview
What incentives do banks have to gather up loans into pools (backed by Ginnie Mae)and selling them?
Do authors have to be politically correct in article-writing?
Bash Script Function Return True-False
Understanding “IFS= read -r line”BASH return to main functionPrevent SIGINT from interrupting function call and child process(es) withinWhy to set errno to zero at the time of initialization of the program and can't test it before error occurred?A script to take data from csv file and perform related operations in a databaseShell script exits early for unclear reasonsDoes “rm -rf $dir” ever return false?Variable comparison inside if in BashHow to get return value from MATLAB functionShell script to find and replace value from csv fileHow can a bash function return multiple values?
I want to query the function by returning the value.
My codes are as follows;
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
true
break
else
false
fi
done < $file
}
printf "Please enter the value : "
read keyboard
if check $keyboard;
then
echo "yes";
else
echo "no";
fi
It does not work, what am I doing wrong?
shell-script function return-status
New contributor
add a comment |
I want to query the function by returning the value.
My codes are as follows;
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
true
break
else
false
fi
done < $file
}
printf "Please enter the value : "
read keyboard
if check $keyboard;
then
echo "yes";
else
echo "no";
fi
It does not work, what am I doing wrong?
shell-script function return-status
New contributor
2
Is there a question in here?
– ilkkachu
2 hours ago
The only problems I found with your code that it prints an error message if the file does not exist and that it printsyes
if the file is empty.
– Bodo
1 hour ago
add a comment |
I want to query the function by returning the value.
My codes are as follows;
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
true
break
else
false
fi
done < $file
}
printf "Please enter the value : "
read keyboard
if check $keyboard;
then
echo "yes";
else
echo "no";
fi
It does not work, what am I doing wrong?
shell-script function return-status
New contributor
I want to query the function by returning the value.
My codes are as follows;
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
true
break
else
false
fi
done < $file
}
printf "Please enter the value : "
read keyboard
if check $keyboard;
then
echo "yes";
else
echo "no";
fi
It does not work, what am I doing wrong?
shell-script function return-status
shell-script function return-status
New contributor
New contributor
edited 1 hour ago
ctrl-alt-delor
11.8k42159
11.8k42159
New contributor
asked 2 hours ago
OğuzOğuz
191
191
New contributor
New contributor
2
Is there a question in here?
– ilkkachu
2 hours ago
The only problems I found with your code that it prints an error message if the file does not exist and that it printsyes
if the file is empty.
– Bodo
1 hour ago
add a comment |
2
Is there a question in here?
– ilkkachu
2 hours ago
The only problems I found with your code that it prints an error message if the file does not exist and that it printsyes
if the file is empty.
– Bodo
1 hour ago
2
2
Is there a question in here?
– ilkkachu
2 hours ago
Is there a question in here?
– ilkkachu
2 hours ago
The only problems I found with your code that it prints an error message if the file does not exist and that it prints
yes
if the file is empty.– Bodo
1 hour ago
The only problems I found with your code that it prints an error message if the file does not exist and that it prints
yes
if the file is empty.– Bodo
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
Testing your code on another file, it does actually work, even if it's a bit inefficient. Assuming that the /root/Turkiye.txt
file is a non-empty file that you have access to, the true
and false
calls, since they are the last commands to be executed in the function, sets the exit status of the function to either zero or non-zero.
It may simply be that you are entering strings into your script that can't be found as separate lines in the file you are reading. It's unfortunately unclear in the question what the file you are parsing looks like, what you are entering into your script and what you expect to get as a response from the script when doing so.
Note also that while read line
does not necessarily read lines from the input file. See e.g. "Understanding "IFS= read -r line"", and also that within [ ... ]
the operator that compares two strings is =
, not ==
.
You also need to double quote $keyboard
in your call to check
to avoid word splitting and filename generation (globbing).
It seems as if you want to use your function to determine whether the string read from the user occurs as a line in a particular file.
This could be done easier with
grep -qxF -e "$keyboard" /root/Turkiye.txt
This grep
command would search for the string in the given file and return a zero exit status (true) if a line is found that is exactly identical, and will otherwise return a non-zero exit status (false). The options used here are -q
to make grep
not output anything, -x
to compare full lines (not substrings) and -F
to do string comparisons (not regular expression matching). The -e
tells grep
that the next argument is the search pattern (an initial -
in the pattern would otherwise have been taken as introducing a command line option).
Your script, modified:
printf 'Please enter the value: ' >&2
IFS= read -r keyboard
if grep -qxF -e "$keyboard" /root/Turkiye.txt; then
echo 'yes'
else
echo 'no'
fi
Note that I output the prompt for the user to enter a string to the standard error stream. This is by convention and allows the output of the script to be redirected and used without it containing the prompt.
In bash
, you would have used
IFS= read -r -p 'Please enter the value: ' keyboard
(Note that bash
would output the prompt string to the standard error stream too when you use read -p
.)
If you would want to use the grep
in a function:
check () {
grep -qxF -e "$1" /root/Turkiye.txt
}
Then use
if check "$keyboard"; then ...; fi
add a comment |
true
and false
are commands that exit with success and failure exit codes, respectively. They do not make your function return those values, to do that use the return
command, and integer values corresponding to success (0) or failure (anything other than 0). Also, I'm pretty sure you don't want to return failure for the first line that doesn't match, just if no line matched:
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
return 0 # 0 = success ("true"); break is not needed because return exits the function
fi
done < $file
# If a line matched, it won't get here (it will have returned out of the middle
# of the loop). Therefore, if it gets here, there must not have been a match.
return 1 # 1 = failure ("false")
}
But there's a much simpler way to do this. Use grep
-- its job is to search files for matching lines. You want grep -Fxq
-- -F
means search for fixed strings (not regular expression patterns), -x
means require the whole line to match, not just part of it, and -q
means don't bother printing the result, just exit with success status if a match was found, failure otherwise. And you don't even need an explicit return
command, since the function will implicitly return the status of the last command in it:
check(){
file=/root/Turkiye.txt
local funkx=$1
grep -Fxq "$funkx" "$file"
}
Or even simpler:
check(){
grep -Fxq "$1" /root/Turkiye.txt
}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Oğuz is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503511%2fbash-script-function-return-true-false%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Testing your code on another file, it does actually work, even if it's a bit inefficient. Assuming that the /root/Turkiye.txt
file is a non-empty file that you have access to, the true
and false
calls, since they are the last commands to be executed in the function, sets the exit status of the function to either zero or non-zero.
It may simply be that you are entering strings into your script that can't be found as separate lines in the file you are reading. It's unfortunately unclear in the question what the file you are parsing looks like, what you are entering into your script and what you expect to get as a response from the script when doing so.
Note also that while read line
does not necessarily read lines from the input file. See e.g. "Understanding "IFS= read -r line"", and also that within [ ... ]
the operator that compares two strings is =
, not ==
.
You also need to double quote $keyboard
in your call to check
to avoid word splitting and filename generation (globbing).
It seems as if you want to use your function to determine whether the string read from the user occurs as a line in a particular file.
This could be done easier with
grep -qxF -e "$keyboard" /root/Turkiye.txt
This grep
command would search for the string in the given file and return a zero exit status (true) if a line is found that is exactly identical, and will otherwise return a non-zero exit status (false). The options used here are -q
to make grep
not output anything, -x
to compare full lines (not substrings) and -F
to do string comparisons (not regular expression matching). The -e
tells grep
that the next argument is the search pattern (an initial -
in the pattern would otherwise have been taken as introducing a command line option).
Your script, modified:
printf 'Please enter the value: ' >&2
IFS= read -r keyboard
if grep -qxF -e "$keyboard" /root/Turkiye.txt; then
echo 'yes'
else
echo 'no'
fi
Note that I output the prompt for the user to enter a string to the standard error stream. This is by convention and allows the output of the script to be redirected and used without it containing the prompt.
In bash
, you would have used
IFS= read -r -p 'Please enter the value: ' keyboard
(Note that bash
would output the prompt string to the standard error stream too when you use read -p
.)
If you would want to use the grep
in a function:
check () {
grep -qxF -e "$1" /root/Turkiye.txt
}
Then use
if check "$keyboard"; then ...; fi
add a comment |
Testing your code on another file, it does actually work, even if it's a bit inefficient. Assuming that the /root/Turkiye.txt
file is a non-empty file that you have access to, the true
and false
calls, since they are the last commands to be executed in the function, sets the exit status of the function to either zero or non-zero.
It may simply be that you are entering strings into your script that can't be found as separate lines in the file you are reading. It's unfortunately unclear in the question what the file you are parsing looks like, what you are entering into your script and what you expect to get as a response from the script when doing so.
Note also that while read line
does not necessarily read lines from the input file. See e.g. "Understanding "IFS= read -r line"", and also that within [ ... ]
the operator that compares two strings is =
, not ==
.
You also need to double quote $keyboard
in your call to check
to avoid word splitting and filename generation (globbing).
It seems as if you want to use your function to determine whether the string read from the user occurs as a line in a particular file.
This could be done easier with
grep -qxF -e "$keyboard" /root/Turkiye.txt
This grep
command would search for the string in the given file and return a zero exit status (true) if a line is found that is exactly identical, and will otherwise return a non-zero exit status (false). The options used here are -q
to make grep
not output anything, -x
to compare full lines (not substrings) and -F
to do string comparisons (not regular expression matching). The -e
tells grep
that the next argument is the search pattern (an initial -
in the pattern would otherwise have been taken as introducing a command line option).
Your script, modified:
printf 'Please enter the value: ' >&2
IFS= read -r keyboard
if grep -qxF -e "$keyboard" /root/Turkiye.txt; then
echo 'yes'
else
echo 'no'
fi
Note that I output the prompt for the user to enter a string to the standard error stream. This is by convention and allows the output of the script to be redirected and used without it containing the prompt.
In bash
, you would have used
IFS= read -r -p 'Please enter the value: ' keyboard
(Note that bash
would output the prompt string to the standard error stream too when you use read -p
.)
If you would want to use the grep
in a function:
check () {
grep -qxF -e "$1" /root/Turkiye.txt
}
Then use
if check "$keyboard"; then ...; fi
add a comment |
Testing your code on another file, it does actually work, even if it's a bit inefficient. Assuming that the /root/Turkiye.txt
file is a non-empty file that you have access to, the true
and false
calls, since they are the last commands to be executed in the function, sets the exit status of the function to either zero or non-zero.
It may simply be that you are entering strings into your script that can't be found as separate lines in the file you are reading. It's unfortunately unclear in the question what the file you are parsing looks like, what you are entering into your script and what you expect to get as a response from the script when doing so.
Note also that while read line
does not necessarily read lines from the input file. See e.g. "Understanding "IFS= read -r line"", and also that within [ ... ]
the operator that compares two strings is =
, not ==
.
You also need to double quote $keyboard
in your call to check
to avoid word splitting and filename generation (globbing).
It seems as if you want to use your function to determine whether the string read from the user occurs as a line in a particular file.
This could be done easier with
grep -qxF -e "$keyboard" /root/Turkiye.txt
This grep
command would search for the string in the given file and return a zero exit status (true) if a line is found that is exactly identical, and will otherwise return a non-zero exit status (false). The options used here are -q
to make grep
not output anything, -x
to compare full lines (not substrings) and -F
to do string comparisons (not regular expression matching). The -e
tells grep
that the next argument is the search pattern (an initial -
in the pattern would otherwise have been taken as introducing a command line option).
Your script, modified:
printf 'Please enter the value: ' >&2
IFS= read -r keyboard
if grep -qxF -e "$keyboard" /root/Turkiye.txt; then
echo 'yes'
else
echo 'no'
fi
Note that I output the prompt for the user to enter a string to the standard error stream. This is by convention and allows the output of the script to be redirected and used without it containing the prompt.
In bash
, you would have used
IFS= read -r -p 'Please enter the value: ' keyboard
(Note that bash
would output the prompt string to the standard error stream too when you use read -p
.)
If you would want to use the grep
in a function:
check () {
grep -qxF -e "$1" /root/Turkiye.txt
}
Then use
if check "$keyboard"; then ...; fi
Testing your code on another file, it does actually work, even if it's a bit inefficient. Assuming that the /root/Turkiye.txt
file is a non-empty file that you have access to, the true
and false
calls, since they are the last commands to be executed in the function, sets the exit status of the function to either zero or non-zero.
It may simply be that you are entering strings into your script that can't be found as separate lines in the file you are reading. It's unfortunately unclear in the question what the file you are parsing looks like, what you are entering into your script and what you expect to get as a response from the script when doing so.
Note also that while read line
does not necessarily read lines from the input file. See e.g. "Understanding "IFS= read -r line"", and also that within [ ... ]
the operator that compares two strings is =
, not ==
.
You also need to double quote $keyboard
in your call to check
to avoid word splitting and filename generation (globbing).
It seems as if you want to use your function to determine whether the string read from the user occurs as a line in a particular file.
This could be done easier with
grep -qxF -e "$keyboard" /root/Turkiye.txt
This grep
command would search for the string in the given file and return a zero exit status (true) if a line is found that is exactly identical, and will otherwise return a non-zero exit status (false). The options used here are -q
to make grep
not output anything, -x
to compare full lines (not substrings) and -F
to do string comparisons (not regular expression matching). The -e
tells grep
that the next argument is the search pattern (an initial -
in the pattern would otherwise have been taken as introducing a command line option).
Your script, modified:
printf 'Please enter the value: ' >&2
IFS= read -r keyboard
if grep -qxF -e "$keyboard" /root/Turkiye.txt; then
echo 'yes'
else
echo 'no'
fi
Note that I output the prompt for the user to enter a string to the standard error stream. This is by convention and allows the output of the script to be redirected and used without it containing the prompt.
In bash
, you would have used
IFS= read -r -p 'Please enter the value: ' keyboard
(Note that bash
would output the prompt string to the standard error stream too when you use read -p
.)
If you would want to use the grep
in a function:
check () {
grep -qxF -e "$1" /root/Turkiye.txt
}
Then use
if check "$keyboard"; then ...; fi
edited 34 mins ago
answered 1 hour ago
KusalanandaKusalananda
133k17253416
133k17253416
add a comment |
add a comment |
true
and false
are commands that exit with success and failure exit codes, respectively. They do not make your function return those values, to do that use the return
command, and integer values corresponding to success (0) or failure (anything other than 0). Also, I'm pretty sure you don't want to return failure for the first line that doesn't match, just if no line matched:
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
return 0 # 0 = success ("true"); break is not needed because return exits the function
fi
done < $file
# If a line matched, it won't get here (it will have returned out of the middle
# of the loop). Therefore, if it gets here, there must not have been a match.
return 1 # 1 = failure ("false")
}
But there's a much simpler way to do this. Use grep
-- its job is to search files for matching lines. You want grep -Fxq
-- -F
means search for fixed strings (not regular expression patterns), -x
means require the whole line to match, not just part of it, and -q
means don't bother printing the result, just exit with success status if a match was found, failure otherwise. And you don't even need an explicit return
command, since the function will implicitly return the status of the last command in it:
check(){
file=/root/Turkiye.txt
local funkx=$1
grep -Fxq "$funkx" "$file"
}
Or even simpler:
check(){
grep -Fxq "$1" /root/Turkiye.txt
}
add a comment |
true
and false
are commands that exit with success and failure exit codes, respectively. They do not make your function return those values, to do that use the return
command, and integer values corresponding to success (0) or failure (anything other than 0). Also, I'm pretty sure you don't want to return failure for the first line that doesn't match, just if no line matched:
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
return 0 # 0 = success ("true"); break is not needed because return exits the function
fi
done < $file
# If a line matched, it won't get here (it will have returned out of the middle
# of the loop). Therefore, if it gets here, there must not have been a match.
return 1 # 1 = failure ("false")
}
But there's a much simpler way to do this. Use grep
-- its job is to search files for matching lines. You want grep -Fxq
-- -F
means search for fixed strings (not regular expression patterns), -x
means require the whole line to match, not just part of it, and -q
means don't bother printing the result, just exit with success status if a match was found, failure otherwise. And you don't even need an explicit return
command, since the function will implicitly return the status of the last command in it:
check(){
file=/root/Turkiye.txt
local funkx=$1
grep -Fxq "$funkx" "$file"
}
Or even simpler:
check(){
grep -Fxq "$1" /root/Turkiye.txt
}
add a comment |
true
and false
are commands that exit with success and failure exit codes, respectively. They do not make your function return those values, to do that use the return
command, and integer values corresponding to success (0) or failure (anything other than 0). Also, I'm pretty sure you don't want to return failure for the first line that doesn't match, just if no line matched:
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
return 0 # 0 = success ("true"); break is not needed because return exits the function
fi
done < $file
# If a line matched, it won't get here (it will have returned out of the middle
# of the loop). Therefore, if it gets here, there must not have been a match.
return 1 # 1 = failure ("false")
}
But there's a much simpler way to do this. Use grep
-- its job is to search files for matching lines. You want grep -Fxq
-- -F
means search for fixed strings (not regular expression patterns), -x
means require the whole line to match, not just part of it, and -q
means don't bother printing the result, just exit with success status if a match was found, failure otherwise. And you don't even need an explicit return
command, since the function will implicitly return the status of the last command in it:
check(){
file=/root/Turkiye.txt
local funkx=$1
grep -Fxq "$funkx" "$file"
}
Or even simpler:
check(){
grep -Fxq "$1" /root/Turkiye.txt
}
true
and false
are commands that exit with success and failure exit codes, respectively. They do not make your function return those values, to do that use the return
command, and integer values corresponding to success (0) or failure (anything other than 0). Also, I'm pretty sure you don't want to return failure for the first line that doesn't match, just if no line matched:
check(){
file=/root/Turkiye.txt
local funkx=$1
while read line; do
if [ "$line" == "$funkx" ]
then
return 0 # 0 = success ("true"); break is not needed because return exits the function
fi
done < $file
# If a line matched, it won't get here (it will have returned out of the middle
# of the loop). Therefore, if it gets here, there must not have been a match.
return 1 # 1 = failure ("false")
}
But there's a much simpler way to do this. Use grep
-- its job is to search files for matching lines. You want grep -Fxq
-- -F
means search for fixed strings (not regular expression patterns), -x
means require the whole line to match, not just part of it, and -q
means don't bother printing the result, just exit with success status if a match was found, failure otherwise. And you don't even need an explicit return
command, since the function will implicitly return the status of the last command in it:
check(){
file=/root/Turkiye.txt
local funkx=$1
grep -Fxq "$funkx" "$file"
}
Or even simpler:
check(){
grep -Fxq "$1" /root/Turkiye.txt
}
answered 1 hour ago
Gordon DavissonGordon Davisson
1,30165
1,30165
add a comment |
add a comment |
Oğuz is a new contributor. Be nice, and check out our Code of Conduct.
Oğuz is a new contributor. Be nice, and check out our Code of Conduct.
Oğuz is a new contributor. Be nice, and check out our Code of Conduct.
Oğuz is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503511%2fbash-script-function-return-true-false%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Is there a question in here?
– ilkkachu
2 hours ago
The only problems I found with your code that it prints an error message if the file does not exist and that it prints
yes
if the file is empty.– Bodo
1 hour ago