0

I just found this programming segment in my son's Bash file. I am quite a newbie and unable to understand the printf syntax. Can someone explain me the COMMENTED printf in the segment below??

#printf "\033[1;34m"
while [ -d /proc/$PROC ]; do
    printf '\033[s\033[u[/] %s\033[u' "$str"; sleep "$delay"
    printf '\033[s\033[u[—] %s\033[u' "$str"; sleep "$delay"
    printf '\033[s\033[u[\] %s\033[u' "$str"; sleep "$delay"
    printf '\033[s\033[u[|] %s\033[u' "$str"; sleep "$delay"
done
#printf '\033[s\033[u%*s\033[u\033[0m' $((${#str}+6)) " "  # return to normal(It disappears.)
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Ramesh
  • 102
  • 10

1 Answers1

0

It's is nothing but a busy/wait spinner and the lines commented do nothing but set a blue foreground color and the last erases the line the spinner and text. \033 is just character 27 which is part of the ANSI escape followed by [. See here Spinner Animation and echo command The 's' saves the cursor position and the 'u' restores the cursor position to where it was last saved -- your fine, nothing nefarious... (I'm fairly sure that is exactly where that code came from)

In more detail:

#printf "\033[1;34m"   /* simply sets a blue foreground color */

Then the final:

#printf '\033[s\033[u%*s\033[u\033[0m' $((${#str}+6)) " "
  1. \033[s save cursor position,

  2. \033[u restore to last saved,

  3. '%*s' normal printf format specifier for a string with the * to note the field width will be specified by the 1st argument,

  4. \033[u restore to last saved position,

  5. \033[0m return the color to default.

The first argument is $((${#str}+6)) the length of the string your printing plus 6-chars for the spinner, e.g. '[ \ ] ' and the the 2nd argument, a space (e.g. " ") for the actual string to overwrite the line with empty-spaces.

It simply erases the line used with the spinner in it.

By commenting the lines, the color is left at the default (the 1st comment) and the final line with the string and the last spinner position is left on screen (2nd commented printf)

Here is an ANSI Escape sequences reference that explains the escapes further...

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Well, I've never seen someone start an answer in the comments then finish it in the answer block. I don't even recall seeing a new site user do it. Perhaps all of the answer should be in the answer block. – jww Nov 14 '17 at 07:04
  • Yes, it happens when I'm first simply going to leave a comment, but then decide the comment needs to be expanded upon, but won't fit in the allotted comment space... – David C. Rankin Nov 14 '17 at 07:05
  • @jww are you the downvote? If so, what is your technical objection to anything contained in the answer. I know what the original code is for -- I wrote it, the ANSI escapes explanation and `printf` explanation is non-objectionable, so what it the basis? If not you, then perhaps the anonymous person will have the integrity to explain. – David C. Rankin Nov 14 '17 at 07:36
  • Well, thank you @DavidC.Rankin for the kind and helpful explanation. This taught me something and enlightens me about certain things. However, call it luck or so, the link you gave me, points to my son's account, his name is Arjun. Thanks for helping me and my son. – Ramesh Nov 15 '17 at 06:06
  • Who downvoted your answer anyway, the answer seems legit. – Ramesh Nov 15 '17 at 06:06
  • @Ramesh, glad to help. I had a fairly long comment back and forth with your son in that question. Very bright boy. Good luck to you both. – David C. Rankin Nov 15 '17 at 06:21