overwrite file and echo to stdout

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a simple bash function to increment a number and store it in a file:
get_next_int () tee
regarding this line:
echo "$((++my_num))" > "$my_file" | tee
out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?
this answer about tee doesn't seem to cover my case.
How to redirect output to a file and stdout | Stack Overflow
also, as an aside, regarding this line:
typeset -i my_num=$(cat "$my_file")
that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.
Is there a way to do something like:
typeset -i my_num=$(cat "$my_file" | default "1")
bash shell-script tee
add a comment |Â
up vote
0
down vote
favorite
I have a simple bash function to increment a number and store it in a file:
get_next_int () tee
regarding this line:
echo "$((++my_num))" > "$my_file" | tee
out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?
this answer about tee doesn't seem to cover my case.
How to redirect output to a file and stdout | Stack Overflow
also, as an aside, regarding this line:
typeset -i my_num=$(cat "$my_file")
that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.
Is there a way to do something like:
typeset -i my_num=$(cat "$my_file" | default "1")
bash shell-script tee
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a simple bash function to increment a number and store it in a file:
get_next_int () tee
regarding this line:
echo "$((++my_num))" > "$my_file" | tee
out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?
this answer about tee doesn't seem to cover my case.
How to redirect output to a file and stdout | Stack Overflow
also, as an aside, regarding this line:
typeset -i my_num=$(cat "$my_file")
that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.
Is there a way to do something like:
typeset -i my_num=$(cat "$my_file" | default "1")
bash shell-script tee
I have a simple bash function to increment a number and store it in a file:
get_next_int () tee
regarding this line:
echo "$((++my_num))" > "$my_file" | tee
out of curiousity, is there a way to overwrite the file with the incremented integer and echo it to stdout?
this answer about tee doesn't seem to cover my case.
How to redirect output to a file and stdout | Stack Overflow
also, as an aside, regarding this line:
typeset -i my_num=$(cat "$my_file")
that will grab the integer representation of the string from the file. However if the file is empty, I need to default to 1.
Is there a way to do something like:
typeset -i my_num=$(cat "$my_file" | default "1")
bash shell-script tee
edited Apr 11 at 22:38
Drakonoved
674518
674518
asked Apr 11 at 18:36
Alexander Mills
1,885929
1,885929
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
echo "$(( ++my_num ))" | tee "$my_file"
Tee writes te the named file(s) and to standard output by default.
After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use
my_num=$(( my_num == 0 ? 1 : my_num ))
to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.
I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).
I would probably have written the function as
get_next_int () tee "$HOME/next_int.json"
assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).
does that overwrite the file?
â Alexander Mills
Apr 11 at 18:41
I assume so, and thank you, also if you have thoughts about the aside question, that helps
â Alexander Mills
Apr 11 at 18:42
1
@AlexanderMills It does, unlesstee -ais used for appending.
â Kusalananda
Apr 11 at 18:42
got it, any idea how to use a default string if cat comes up with empty stdout?
â Alexander Mills
Apr 11 at 18:43
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
echo "$(( ++my_num ))" | tee "$my_file"
Tee writes te the named file(s) and to standard output by default.
After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use
my_num=$(( my_num == 0 ? 1 : my_num ))
to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.
I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).
I would probably have written the function as
get_next_int () tee "$HOME/next_int.json"
assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).
does that overwrite the file?
â Alexander Mills
Apr 11 at 18:41
I assume so, and thank you, also if you have thoughts about the aside question, that helps
â Alexander Mills
Apr 11 at 18:42
1
@AlexanderMills It does, unlesstee -ais used for appending.
â Kusalananda
Apr 11 at 18:42
got it, any idea how to use a default string if cat comes up with empty stdout?
â Alexander Mills
Apr 11 at 18:43
add a comment |Â
up vote
3
down vote
accepted
echo "$(( ++my_num ))" | tee "$my_file"
Tee writes te the named file(s) and to standard output by default.
After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use
my_num=$(( my_num == 0 ? 1 : my_num ))
to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.
I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).
I would probably have written the function as
get_next_int () tee "$HOME/next_int.json"
assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).
does that overwrite the file?
â Alexander Mills
Apr 11 at 18:41
I assume so, and thank you, also if you have thoughts about the aside question, that helps
â Alexander Mills
Apr 11 at 18:42
1
@AlexanderMills It does, unlesstee -ais used for appending.
â Kusalananda
Apr 11 at 18:42
got it, any idea how to use a default string if cat comes up with empty stdout?
â Alexander Mills
Apr 11 at 18:43
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
echo "$(( ++my_num ))" | tee "$my_file"
Tee writes te the named file(s) and to standard output by default.
After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use
my_num=$(( my_num == 0 ? 1 : my_num ))
to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.
I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).
I would probably have written the function as
get_next_int () tee "$HOME/next_int.json"
assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).
echo "$(( ++my_num ))" | tee "$my_file"
Tee writes te the named file(s) and to standard output by default.
After typeset -i my_num=$(cat "$my_file") (which could be typeset -i my_num=$(<"$my_file") in bash), you could use
my_num=$(( my_num == 0 ? 1 : my_num ))
to set it to 1 if it's value is zero. Integer variables in bash evaluate to zero if empty.
I was going to suggest my_num=$my_num:-1 but since my_num is an integer variable, it will expand to zero if it's empty, not to the empty string, which means that the $variable:-value parameter substitution would not work (this would otherwise expand to value if the variable was unset or empty).
I would probably have written the function as
get_next_int () tee "$HOME/next_int.json"
assuming "$HOME/next_int.json" always contained a single integer. I've left out the touch because the files modification timestamp would be updated by tee (keep touch if you need the access timestamp updated as well).
edited Apr 11 at 18:58
answered Apr 11 at 18:41
Kusalananda
102k13199316
102k13199316
does that overwrite the file?
â Alexander Mills
Apr 11 at 18:41
I assume so, and thank you, also if you have thoughts about the aside question, that helps
â Alexander Mills
Apr 11 at 18:42
1
@AlexanderMills It does, unlesstee -ais used for appending.
â Kusalananda
Apr 11 at 18:42
got it, any idea how to use a default string if cat comes up with empty stdout?
â Alexander Mills
Apr 11 at 18:43
add a comment |Â
does that overwrite the file?
â Alexander Mills
Apr 11 at 18:41
I assume so, and thank you, also if you have thoughts about the aside question, that helps
â Alexander Mills
Apr 11 at 18:42
1
@AlexanderMills It does, unlesstee -ais used for appending.
â Kusalananda
Apr 11 at 18:42
got it, any idea how to use a default string if cat comes up with empty stdout?
â Alexander Mills
Apr 11 at 18:43
does that overwrite the file?
â Alexander Mills
Apr 11 at 18:41
does that overwrite the file?
â Alexander Mills
Apr 11 at 18:41
I assume so, and thank you, also if you have thoughts about the aside question, that helps
â Alexander Mills
Apr 11 at 18:42
I assume so, and thank you, also if you have thoughts about the aside question, that helps
â Alexander Mills
Apr 11 at 18:42
1
1
@AlexanderMills It does, unless
tee -a is used for appending.â Kusalananda
Apr 11 at 18:42
@AlexanderMills It does, unless
tee -a is used for appending.â Kusalananda
Apr 11 at 18:42
got it, any idea how to use a default string if cat comes up with empty stdout?
â Alexander Mills
Apr 11 at 18:43
got it, any idea how to use a default string if cat comes up with empty stdout?
â Alexander Mills
Apr 11 at 18:43
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f437114%2foverwrite-file-and-echo-to-stdout%23new-answer', 'question_page');
);
Post as a guest
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
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
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