-1

I want to set an IP as variable ($a) then subtract -1 from last sector and echo the output in a sentence into a .txt file.

Below is what I had in mind but doesn't quite work.

a=123.123.123.123
echo $a | awk -F"." '{printf "%d.%d.%d.%d", $1, $2, $3, $4 - 2}' > $i
echo -e "stream = http://"$i"/control/faststream.jpg?stream=full&needlength&fps=16.0 \n" | cat - note1.txt > temp && mv temp note1.txt

Also, I don't really care about displaying this output echo $a | awk -F"." '{printf "%d.%d.%d.%d", $1, $2, $3, $4 - 2}' > $i I just want it to be used in the next command.

Thanks in advance.

  • 1
    soo just `i=$(echo ... | awk ....)` ? – KamilCuk Oct 27 '21 at 11:20
  • KamilCuk thank you, you re a life savior – Haris Kontogannis Oct 27 '21 at 11:28
  • BTW, in general, using `echo -e` is a bad idea -- the POSIX standard doesn't require _or allow_ shells to support it, so when bash is in its most POSIX-compliant mode (`set -o posix; shopt -s xpg_echo`), `echo -e` just prints `-e` on output; some other shells do that out-of-the-box as well. – Charles Duffy Oct 27 '21 at 11:44
  • `printf '%s\n' "content here"` will print `content here` with a newline; or if you want two newlines, `printf '%s\n' 'content here' ''` or `printf '%s\n\n' 'content here'` will both do the job. – Charles Duffy Oct 27 '21 at 11:44
  • See https://unix.stackexchange.com/a/65819/3113 for more in-depth analysis of `echo`'s faults and how `printf` corrects them -- or just the APPLICATION USAGE and RATIONALE sections of [the POSIX standard for `echo`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html), which itself recommends using `printf` instead. – Charles Duffy Oct 27 '21 at 11:45
  • Charles Duffy I used echo -e because I want break line in the text added to the file. I 'll check the URL, thanks. Thank you all. – Haris Kontogannis Oct 27 '21 at 11:52

1 Answers1

-1

Use | cut -d"." -f4, and put the value into a variable, then subtract one. That should work.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
hein sat
  • 479
  • 3
  • 4
  • 1
    This doesn't explain _how_ to put the value into a variable, which is what the OP needs assistance with. (Also, note the _Answer Well-Asked Questions_ section of [How to Answer](https://stackoverflow.com/help/how-to-answer), including the bullet point regarding questions that "have been asked and answered many times before"). – Charles Duffy Oct 27 '21 at 11:47