3

I have a text file a.txt whose content is a number between 0 and 100. I need a single command which modifies a.txt by increasing by 10 the number, but under the constraint that the final result has to be less than 100 (i.e. if the number reaches a value larger than 100 (e.g. 95+10=105), it just stays on 100).

I need also a command to decrease by at most 10 but in such a way that the final result is greater or equal than 0, it should be analogous.

What I can do, is to modify the file directly with echo, but in this way I can just replace the number with something else (and not adding to the previous number).

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Reyx_0
  • 941
  • Sounds like home work... What did you do so far? Selected a weapon of choice? – joepd Dec 16 '15 at 19:15
  • 1
    not homework XD just trying to adjust the backlight with a custom command – Reyx_0 Dec 16 '15 at 19:16
  • The only solution which makes the fn keys work, conflicts with the shutdown process, so I have to go for a homemade one – Reyx_0 Dec 16 '15 at 19:19
  • Ha—I read the question and I thought, "Sounds like he's adjusting his screen brightness manually." Then I came to the comments. :) I actually wrote a script for this when I first learned bash; it's far from my standards now but it worked well enough. It's on a computer at home, though, so I'll have to post it later. – Wildcard Dec 16 '15 at 19:36
  • yes, I was about to write it in the post, but then I thought I would have got a lot of people asking to change the grub file instead XD – Reyx_0 Dec 16 '15 at 19:52

2 Answers2

5

This will add 10:

echo "$(awk '{x=$0+10;if(x>100)x=100; print x}' a.txt)" > a.txt

This will subtract 10:

echo "$(awk '{x=$0-10;if(x<0)x=0; print x}' a.txt)" > a.txt

$0 is the value of the current line being read in the file, so it add/subtract 10 from it, and then check if it is within the constraints.

Thanks to the great comment Wildcard made, you can do this without using a temporary file.

Kira
  • 4,807
  • 2
    One note about editing files in place: You can do this without a temp file like so: echo "$(awk '{...}' a.txt)" > a.txt It's a handy way to use bash internals (command substitution) instead of messing with temp file creation. Credit to http://serverfault.com/a/547331/313521 – Wildcard Dec 16 '15 at 19:40
  • 1
    Also note that using tmp as a tempfile is a nice security hole. If a symlink called tmp is created to point to /etc/passwd and you run your script privileged...nasty things happen. If you do want to use a temp file rather than command substitution as I describe above, use the mktemp utility: tmp="$(mktemp)"; awk '...' a.txt > "$tmp" && mv "$tmp" a.txt though this won't preserve ACLs, extended attributes, hard links, etc. etc. etc. – Wildcard Dec 16 '15 at 20:00
  • Wow, thanks a lot for this hint! I was very upset for using this temporary file, but I didn't know how to do it without it. I'll update my answer. – Kira Dec 16 '15 at 20:11
  • 1
    Sure. :) Note that I just learned (in the comments on http://unix.stackexchange.com/a/249807/135943) that any trailing newlines will be dropped in command substitution. In this use case where the file only contains a single line (and it most use cases) it won't matter at all, but it's worth noting. – Wildcard Dec 16 '15 at 20:22
  • An issue: how do I modify a protected text file with that? strangely enough, sudo echo does not work – Reyx_0 Dec 16 '15 at 20:32
  • Can you still modify it manually like echo 42 > a.txt? – Kira Dec 16 '15 at 20:42
  • 1
    @Reyx_0: See http://unix.stackexchange.com/q/1416/135943 – Wildcard Dec 16 '15 at 20:47
1
read bl <a.txt; echo "$((bl<90?bl+10:100))" >a.txt

...should work if there's only a single line in a.txt which works out to a number less than or equal to 100. it will also work if a.txt is empty in most shells to set it to a value of 10. The reverse logic works:

 read bl <a.txt; echo "$((bl<10?0:bl-10))"  >a.txt
mikeserv
  • 58,310