Bash file descriptor and write printf to a file in a while loop
up vote
1
down vote
favorite
I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4
to write the output of the lower while loop to a file somefile24.log
but somefile24.log
is empty after I run the code.
Here is the code:
#!/bin/bash
#
# openFile.sh: print the contents of orders.txt
shopt -s -o nounset
declare LINE
exec 3< orders.txt
while read LINE <&3
do
printf "%sn" "$LINE"
done
echo
echo "Here is the new part of the program!"
exec 4> somefile24.log
count=1
while read LINE <&3
do
printf "%s-----count=%dn" "$LINE" "$count" >&4
let count++
done
exit 0
Here is the output:
(something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
$./openFile.sh
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
Here is the new part of the program!
Expected output is:
The script writes the contents of orders.txt to somefile24.log
I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:
$ shellcheck myscript
Line 10:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 22:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 25:
let count++
^-- SC2219: Instead of 'let expr', prefer (( expr )) .
I've tried to make a test on the command line:
(something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
$exec 3>somefile24.log
(something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
$echo testing >&3
(something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
$cat somefile24.log
testing
I know that I can just do:
while
printf "%sn" "$LINE" >> somefile24.log
QUESTION:
Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?
And why is somefile24.log
alway's empty after I run the script?
bash logging file-descriptor io-redirection
add a comment |
up vote
1
down vote
favorite
I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4
to write the output of the lower while loop to a file somefile24.log
but somefile24.log
is empty after I run the code.
Here is the code:
#!/bin/bash
#
# openFile.sh: print the contents of orders.txt
shopt -s -o nounset
declare LINE
exec 3< orders.txt
while read LINE <&3
do
printf "%sn" "$LINE"
done
echo
echo "Here is the new part of the program!"
exec 4> somefile24.log
count=1
while read LINE <&3
do
printf "%s-----count=%dn" "$LINE" "$count" >&4
let count++
done
exit 0
Here is the output:
(something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
$./openFile.sh
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
Here is the new part of the program!
Expected output is:
The script writes the contents of orders.txt to somefile24.log
I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:
$ shellcheck myscript
Line 10:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 22:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 25:
let count++
^-- SC2219: Instead of 'let expr', prefer (( expr )) .
I've tried to make a test on the command line:
(something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
$exec 3>somefile24.log
(something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
$echo testing >&3
(something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
$cat somefile24.log
testing
I know that I can just do:
while
printf "%sn" "$LINE" >> somefile24.log
QUESTION:
Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?
And why is somefile24.log
alway's empty after I run the script?
bash logging file-descriptor io-redirection
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4
to write the output of the lower while loop to a file somefile24.log
but somefile24.log
is empty after I run the code.
Here is the code:
#!/bin/bash
#
# openFile.sh: print the contents of orders.txt
shopt -s -o nounset
declare LINE
exec 3< orders.txt
while read LINE <&3
do
printf "%sn" "$LINE"
done
echo
echo "Here is the new part of the program!"
exec 4> somefile24.log
count=1
while read LINE <&3
do
printf "%s-----count=%dn" "$LINE" "$count" >&4
let count++
done
exit 0
Here is the output:
(something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
$./openFile.sh
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
Here is the new part of the program!
Expected output is:
The script writes the contents of orders.txt to somefile24.log
I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:
$ shellcheck myscript
Line 10:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 22:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 25:
let count++
^-- SC2219: Instead of 'let expr', prefer (( expr )) .
I've tried to make a test on the command line:
(something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
$exec 3>somefile24.log
(something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
$echo testing >&3
(something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
$cat somefile24.log
testing
I know that I can just do:
while
printf "%sn" "$LINE" >> somefile24.log
QUESTION:
Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?
And why is somefile24.log
alway's empty after I run the script?
bash logging file-descriptor io-redirection
I'm learning bash programming through a book and now I'm learning about opening files and file descriptors but I can't get this code to work(see below), I want to use the file descriptor 4
to write the output of the lower while loop to a file somefile24.log
but somefile24.log
is empty after I run the code.
Here is the code:
#!/bin/bash
#
# openFile.sh: print the contents of orders.txt
shopt -s -o nounset
declare LINE
exec 3< orders.txt
while read LINE <&3
do
printf "%sn" "$LINE"
done
echo
echo "Here is the new part of the program!"
exec 4> somefile24.log
count=1
while read LINE <&3
do
printf "%s-----count=%dn" "$LINE" "$count" >&4
let count++
done
exit 0
Here is the output:
(something78@notemDEB78)-(01:45:03)-(~/Bash_Programming_2018)
$./openFile.sh
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
something and something and something
Here is the new part of the program!
Expected output is:
The script writes the contents of orders.txt to somefile24.log
I've tried to check the code in shellcheck but it doesn't see the problem and only gives me this:
$ shellcheck myscript
Line 10:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 22:
while read LINE <&3
^-- SC2162: read without -r will mangle backslashes.
Line 25:
let count++
^-- SC2219: Instead of 'let expr', prefer (( expr )) .
I've tried to make a test on the command line:
(something78@notemDEB78)-(01:59:41)-(~/Bash_Programming_2018)
$exec 3>somefile24.log
(something78@notemDEB78)-(01:59:59)-(~/Bash_Programming_2018)
$echo testing >&3
(something78@notemDEB78)-(02:00:03)-(~/Bash_Programming_2018)
$cat somefile24.log
testing
I know that I can just do:
while
printf "%sn" "$LINE" >> somefile24.log
QUESTION:
Is it possible to use a file descriptor to write to a file like this in a loop and if so how and what am I doing wrong?
And why is somefile24.log
alway's empty after I run the script?
bash logging file-descriptor io-redirection
bash logging file-descriptor io-redirection
edited Nov 11 at 22:59
asked Nov 11 at 2:16
somethingSomething
29321128
29321128
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You already read the contents of the file in your first loop. Hence the file descriptor 3
is already at EOF. You can reset the file descriptor 3
before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt
1
Personally, I prefer to attach the redirect directly to thewhile
loop (rather than usingexec
before the loop), i.e. usewhile read ... done 3<orders.txt
. This idiom automatically solves the problem by reopening the file for each loop.
– Gordon Davisson
Nov 11 at 7:34
I also prefer that. This only answers why he’s not getting the behavior he expects.
– ssemilla
Nov 11 at 7:37
add a comment |
up vote
1
down vote
After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You already read the contents of the file in your first loop. Hence the file descriptor 3
is already at EOF. You can reset the file descriptor 3
before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt
1
Personally, I prefer to attach the redirect directly to thewhile
loop (rather than usingexec
before the loop), i.e. usewhile read ... done 3<orders.txt
. This idiom automatically solves the problem by reopening the file for each loop.
– Gordon Davisson
Nov 11 at 7:34
I also prefer that. This only answers why he’s not getting the behavior he expects.
– ssemilla
Nov 11 at 7:37
add a comment |
up vote
4
down vote
accepted
You already read the contents of the file in your first loop. Hence the file descriptor 3
is already at EOF. You can reset the file descriptor 3
before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt
1
Personally, I prefer to attach the redirect directly to thewhile
loop (rather than usingexec
before the loop), i.e. usewhile read ... done 3<orders.txt
. This idiom automatically solves the problem by reopening the file for each loop.
– Gordon Davisson
Nov 11 at 7:34
I also prefer that. This only answers why he’s not getting the behavior he expects.
– ssemilla
Nov 11 at 7:37
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You already read the contents of the file in your first loop. Hence the file descriptor 3
is already at EOF. You can reset the file descriptor 3
before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt
You already read the contents of the file in your first loop. Hence the file descriptor 3
is already at EOF. You can reset the file descriptor 3
before you read on it again. E.g. before your second loop, run this again exec 3< orders.txt
answered Nov 11 at 3:22
ssemilla
2,362421
2,362421
1
Personally, I prefer to attach the redirect directly to thewhile
loop (rather than usingexec
before the loop), i.e. usewhile read ... done 3<orders.txt
. This idiom automatically solves the problem by reopening the file for each loop.
– Gordon Davisson
Nov 11 at 7:34
I also prefer that. This only answers why he’s not getting the behavior he expects.
– ssemilla
Nov 11 at 7:37
add a comment |
1
Personally, I prefer to attach the redirect directly to thewhile
loop (rather than usingexec
before the loop), i.e. usewhile read ... done 3<orders.txt
. This idiom automatically solves the problem by reopening the file for each loop.
– Gordon Davisson
Nov 11 at 7:34
I also prefer that. This only answers why he’s not getting the behavior he expects.
– ssemilla
Nov 11 at 7:37
1
1
Personally, I prefer to attach the redirect directly to the
while
loop (rather than using exec
before the loop), i.e. use while read ... done 3<orders.txt
. This idiom automatically solves the problem by reopening the file for each loop.– Gordon Davisson
Nov 11 at 7:34
Personally, I prefer to attach the redirect directly to the
while
loop (rather than using exec
before the loop), i.e. use while read ... done 3<orders.txt
. This idiom automatically solves the problem by reopening the file for each loop.– Gordon Davisson
Nov 11 at 7:34
I also prefer that. This only answers why he’s not getting the behavior he expects.
– ssemilla
Nov 11 at 7:37
I also prefer that. This only answers why he’s not getting the behavior he expects.
– ssemilla
Nov 11 at 7:37
add a comment |
up vote
1
down vote
After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.
add a comment |
up vote
1
down vote
After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.
add a comment |
up vote
1
down vote
up vote
1
down vote
After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.
After the first loop the file descriptor is at EOF. So you need to reset the file descriptor before going in to the second loop.
answered Nov 11 at 3:34
Sandalu Kalpanee
266
266
add a comment |
add a comment |
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%2fstackoverflow.com%2fquestions%2f53245279%2fbash-file-descriptor-and-write-printf-to-a-file-in-a-while-loop%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