Escape characters, printf, bash script

I need control-u in this script, no online solution works. Control-U deletes to beginning of line. Need help, thanks.

http://www.tldp.org/LDP/abs/html/special-chars.html

#!/bin/bash
#: Title       : scanvirus_test

control_c()
# run if user hits control-c
{
printf 'Exiting...
'
scanvirus_trap_flag='true'
}

#trap keyboard interrupt (control-c)
scanvirus_trap_flag='false'   
trap control_c 2

# set the Internal Field Separator
IFS='/'

#read output of 'clamscan -r' info command_output var array
while read -ra command_output; do

     #control-u doesn't work
     printf "%s %s %s %s %s^U" ${command_output[0]} ${command_output[1]} ${command_output[2]} ${command_output[3]}  ${command_output[4]} 

     #if control-c pressed exit loop
     if  "$scanvirus_trap_flag" = 'true' ]; then
             break
     fi

done < <(clamscan -r)

#remove change to IFS
unset IFS

Hi
Perhaps look at the signal it creates eg ctrl-c is SIGINT or change to ctrl-\ (rather than ctrl-u) which is SIGQUIT?
http://tldp.org/LDP/Bash-Beginners-Guide/html/chap_12.html

I need the escape character inside a string, create the control-u after the string is printed. I think it’s in this format. Control-U is ‘NAK’ in the ASCII table.

‘\x21’ or ‘\21’ I think to put that into a string with the correct number.

printf “%s %s %s %s %s\21” ${command_output[0]} ${command_output[1]} ${command_output[2]} ${command_output[3]} ${command_output[4]}

If open a terminal window, enter a line and press control-u.

> scanvirus -w <press control-u>

That line is deleted.

When I try various methods using any control character, I only get a new line.

http://man7.org/linux/man-pages/man7/ascii.7.html

printf "%s %s %s %s %s^U" ${command_output[0]} ${command_output[1]} ${command_output[2]} ${command_output[3]}  ${command_output[4]}  
printf "this is a line1...\x15"
printf "this is a line1...\21"

^U = plain text ‘^U’
x15= new line
21 = new line

I’m getting nowhere. It looks you can’t use control-u in strings or any other control characters, only ’
’ ??

On Wed 16 Dec 2015 10:16:02 PM CST, lord valarian wrote:

If open a terminal window, enter a line and press control-u.

> scanvirus -w <press control-u>

That line is deleted.

When I try various methods using any control character, I only get a new
line.

ascii(7) - Linux manual page

Code:

printf “%s %s %s %s %s^U” ${command_output[0]} ${command_output[1]}
${command_output[2]} ${command_output[3]} ${command_output[4]} printf
“this is a line1…\x15” printf “this is a line1…\21”


^U = plain text ‘^U’
x15= new line
21 = new line

I’m getting nowhere. It looks you can’t use control-u in strings or any
other control characters, only ’
’ ??

Hi
It’s actually cutting/deleting the line to the clipboard, a built in
function;
http://ss64.com/bash/syntax-keyboard.html


Cheers Malcolm °¿° LFCS, SUSE Knowledge Partner (Linux Counter #276890)
SUSE Linux Enterprise Desktop 12 | GNOME 3.10.1 | 3.12.51-52.31-default
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below… Thanks!

It’s a function built into the terminal, but not bash strings? So, I have do a backspace string length while loop to erase the line.

x=0
while (x != stringlength(line))
do
x=x+1
print backspace
end

I read this thread earlier, but did not understand what you were talking about.

When I do understand it now correcly, you are mixing two things:

  • typing in the terminal and how the terminal handler reacts to different key (combinations);
  • sending output from a program to a terminal, specialy to an VT100/ANSI terminal (and most terminal emulators) that act to special character combinations (most of those that are called “Esc sequences”)

These are quite different subjects.

As I understand you want to send VT100/ANSI Esc sequences to the terminal, maybe this is a good starter: http://www.termsys.demon.co.uk/vtansi.htm . Google may offer you more of these.

When I read this, something like erasing the line the cursor is on might be achieved by Erase Line:

<ESC>2K

You could also look into the ncurses interface. Just a suggestion.

IMO the fundamental issue is your approach… trying to implement an interactive command when you are actually trying to create a non-interactive script.

Google is your friend… use sed
https://www.google.com/search?q=bash+scripting+how+to+delete+line

BTW - To my eye, the script code snippet you posted in the original post includes “escaping” a character… using the back slash, but that has a different effect than what you want… Escaping a character sends a signal that the following character is normally treated as a special character but instead should be treated as a text character. For more examples of this, you can inspect a script I wrote recently, whereas the “$” characters normally signifies a variable, I wanted to pass the character instead as text to be read at a later time(not immediately) as a variable.
https://github.com/putztzu/public_scripts/blob/master/devstack_on_openSUSE.2_docker.sh

HTH,
TSU

http://unix.stackexchange.com/questions/26576/how-to-delete-line-with-echo

printf “%s %s” ${command_output[0]} ${command_output[1]};echo -e “\0332K”

Using tput as suggested later in this thread is more portable.

echo -e 'fooooo\r     \rbar'

Printf “$s” $string; echo -e ‘\r \r’

I print the string no new line then do the above, using a long string of spaces. It’s

My terminal was set to ‘xterm’. I entered ‘export TERM=vt100’. This enabled ANSI support, but I can’t get the esc sequence to work.

| 32 down vote
|
| You’re looking for terminal escapes. In particular, to clear from the cursor position to the beginning of the line:
echo -e “\033[1K” Or everything on the line, regardless of cursor position:
echo -e “\033[2K” And you can do all sorts of other neat tricks with terminal escapes too.

 [TABLE="class: fw"]
 [TR]
 [TD="class: vt"] [share](http://unix.stackexchange.com/a/26592)[improve this answer](http://unix.stackexchange.com/posts/26592/edit)
                |                               answered Dec 11 '11 at 20:50     
          https://forums.opensuse.org/image/svg xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xml%3Aspace%3D%22preserve%22%20height%3D%2224%22%20viewBox%3D%2232.594760696016%2023.027331300934335%20130.75642809632188%20149.89128688648526%22%20width%3D%2221%22%20version%3D%221.1%22%20y%3D%220px%22%20x%3D%220px%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20enable-background%3D%22new%200%200%20200%20200%22%3E%3Cg%20transform%3D%22rotate%28-6%2096%2096%29%20translate%280.8089801436207722%200.8089801436207722%29%20scale%280.9531015755332088%29%22%3E%3Cellipse%20cy%3D%22169.5%22%20cx%3D%2299.16%22%20rx%3D%2229.8%22%20ry%3D%226.717%22%20fill%3D%22%23ED1C24%22%3E%3C%2Fellipse%3E%3Crect%20y%3D%22153%22%20x%3D%2292.28%22%20height%3D%2216.5%22%20width%3D%2213.76%22%20fill%3D%22%239B4205%22%3E%3C%2Frect%3E%3Cpolygon%20points%3D%22100%2035.17%20139%2082.98%2061%2082.98%22%20fill%3D%22%23009444%22%3E%3C%2Fpolygon%3E%3Cpolygon%20points%3D%22123.2%2082.98%2076.83%2082.98%2048%20118.3%20152%20118.3%22%20fill%3D%22%23006838%22%3E%3C%2Fpolygon%3E%3Cpolygon%20points%3D%22136.7%20118.3%2063.26%20118.3%2035%20153%20165%20153%22%20fill%3D%22%23009444%22%3E%3C%2Fpolygon%3E%3Cg%20fill%3D%22%23FBB040%22%3E%3Cpath%20d%3D%22m86.52%20106.6c-9.698%200-18.76-1.545-24.81-4.447-0.862-0.412-1.227-1.449-0.813-2.312s1.45-1.228%202.313-0.813c15.32%207.346%2053%205.275%2064.56-9.801%200.584-0.758%201.672-0.903%202.432-0.321%200.76%200.583%200.904%201.67%200.32%202.43-8%2010.51-27%2015.21-43.98%2015.21z%22%3E%3C%2Fpath%3E%3Cpath%20d%3D%22m88.19%2070c-7.949%200-13.91-1.312-14.44-1.434-0.933-0.212-1.517-1.141-1.305-2.075%200.213-0.933%201.141-1.519%202.074-1.306%200.272%200.062%2027.44%206.012%2043.14-8.222%200.708-0.642%201.807-0.59%202.448%200.12%200.643%200.709%200.591%201.806-0.119%202.448-9.3%208.5-21.99%2010.46-31.71%2010.46z%22%3E%3C%2Fpath%3E%3Cpath%20d%3D%22m89.96%20140.9c-13.64%200-27.37-2.461-38.08-8.088-0.847-0.445-1.173-1.494-0.728-2.34%200.445-0.849%201.494-1.173%202.341-0.729%2026.36%2013.85%2073.78%207.358%2086.02-7.494%200.606-0.737%201.698-0.847%202.439-0.235%200.739%200.606%200.845%201.7%200.235%202.438-8.1%209.9-30%2016.4-52.14%2016.4z%22%3E%3C%2Fpath%3E%3C%2Fg%3E%3Cg%20fill%3D%22%23ED1C24%22%3E%3Ccircle%20cy%3D%2292.35%22%20cx%3D%2289.75%22%20r%3D%225.2%22%3E%3C%2Fcircle%3E%3Ccircle%20cy%3D%2270.21%22%20cx%3D%22111.2%22%20r%3D%225.2%22%3E%3C%2Fcircle%3E%3Ccircle%20cy%3D%2252.68%22%20cx%3D%2289.27%22%20r%3D%225.2%22%3E%3C%2Fcircle%3E%3Cpath%20d%3D%22m121.8%20118.3c0%202.871-2.33%205.201-5.201%205.201s-5.199-2.33-5.199-5.201c0-2.869%202.328-5.199%205.199-5.199s5.2%202.3%205.2%205.2z%22%3E%3C%2Fpath%3E%3Ccircle%20cy%3D%22127.1%22%20cx%3D%2283.09%22%20r%3D%225.2%22%3E%3C%2Fcircle%3E%3Ccircle%20cy%3D%22150.7%22%20cx%3D%2255.8%22%20r%3D%225.2%22%3E%3C%2Fcircle%3E%3Ccircle%20cy%3D%22150.7%22%20cx%3D%22103%22%20r%3D%225.2%22%3E%3C%2Fcircle%3E%3Cpath%20d%3D%22m154.9%20138.6c0%202.871-2.328%205.199-5.199%205.199-2.874%200-5.199-2.328-5.199-5.199%200-2.875%202.325-5.201%205.199-5.201%202.9%200%205.2%202.3%205.2%205.2z%22%3E%3C%2Fpath%3E%3C%2Fg%3E%3Cpolygon%20points%3D%22100%2023.82%20102.3%2027.98%20107%2028.92%20103.8%2032.42%20104.3%2037.16%20100%2035.17%2095.67%2037.16%2096.22%2032.42%2092.99%2028.92%2097.66%2027.98%22%20fill%3D%22%23FBB040%22%3E%3C%2Fpolygon%3E%3C%2Fg%3E%3C%2Fsvg%3E

https://www.gravatar.com/avatar/3bec657e9d57297e9f090b7aed2cda00?s=32&d=identicon&r=PG](User Kevin - Unix & Linux Stack Exchange)
Kevin 16.7k43877

 |

[/TD]
[/TR]
[TR]
[TD=“class: votecell”][/TD]
[TD]
[/TD]
[TD=“class: comment-text”] This assumes a VT100-compatible terminal or emulator (which is a pretty safe assumption these days). – Keith Thompson Dec 12 '11 at 1:46
[/TD]
[/TR]
[TR=“class: comment”]
[TD] | | |
|—|—|

     [/TD]
     [TD="class: comment-text"]                              If you want to erase something you just echoed, your previous echo must have the -n flag, or you'll be sent to a new line                     – [djjeck](http://unix.stackexchange.com/users/37999/djjeck)                 [Nov 20 '14 at 1:45](http://unix.stackexchange.com/questions/26576/how-to-delete-line-with-echo#comment278275_26592)                                                                             
                 [/TD]
 [/TR]
 [TR="class: comment"]
     [TD]             |                                                            |                                                           |

|—|—|

     [/TD]
     [TD="class: comment-text"]                              If you want to write  something else, on the line you're erasing, add a -n flag on the echo  above, or else you'll go to a new line right after deleting it. Also,  append a \r to the string above, or your new content won't go to the  beginning of that line.                     – [djjeck](http://unix.stackexchange.com/users/37999/djjeck)                 [Nov 20 '14 at 1:46](http://unix.stackexchange.com/questions/26576/how-to-delete-line-with-echo#comment278276_26592)                                                                                                  

[/TD]
[/TR]
[/TABLE]

[/TD]
[/TR]
[/TABLE]

printf "$S" $string1; echo -en "\0332K\r"

Is this right?

I’ll check out tput next.

Thanks.

This enables nothing. This tells those programs that care that you think that your terminal should be supporting ANSI; it does not make your terminal magically start to actually support it.

What I do not understand and what makes your postings confusing imho is the mixing of methods in one and the same program line. I programmer should use a style of programming and stick to that. Else he will never deliver neat and understandable code (not understandable even by himself).

Thus why in your

echo -en "\033[2K\r

you use octal for one and special character for the other. Either do

echo -en "\E[2K\r"

or do

echo -en "\033[2K\015"

And why the mix of printf and echo. When you want to test this, it is simple:

henk@boven:~> echo -en aap "\E[2K\r"
henk@boven:~>

Also do not only show what statements you have, but also what the result is. And explain where that result differs from what you want:

henk@boven:~> echo -e 'fooooo\r     \rbar'
bar  o
henk@boven:~>

which is what I would expect:
[ol]
[li]fooooo is send to the terminal and displayed from where the cursor is; [/li][li]CR makes the cursor go back to the begin of the line; [/li][li]5 spaces are written to the line, overwriting the first five characters foooo and leaving the fifth o; [/li][li]CR makes the cursor go back to the begin of the line; [/li][li]the characters bar are written to the line, overwriting the first 3 spaces, leaving two spaces and the o; [/li][li]this being the end of the echo command a LF is written (no -n option on the echo command) and the cursor thus steps down one line; [/li][li]the shell takes over and displays a prompt. [/li][/ol]
And that is what I see. So no complaints or amazement from me. But how about you?

Same for

printf "$S" $string1; echo -en "\033[2K\r"

You only show a statement, but you do not explain what you get. nor what you expected to get. We are no mindreaders! Simply saying “I can’t get the esc sequence to work.” tells us almost nothing.

And of course what avidjaar says: changing a process environment variable does not change your (emulated) hardware.

Try to understand. You have a piece of hardware (a character termminal at the end of an asynchronous line) that is wired to understand and act upon certain character combinations (“Escape sequences”) that are send to it over the line from the computer. You send those sequences and the terminal does what it should do. Only be sure that the terminal is capable to understand these VT100/ANSI codes (and Konsole and most other terminal emulators can do that).

Now these days the terminal has changed to a terminal emulator (e.g. Konsole) and the asynchronous line has shrunken to nothing, but it works still the same:
[ul]
[li]when you send the character A, the terminal will show the shape of an A where the cursor is and step the cursor one step forward; [/li][li]when you send the characters Esc[2K the terminal will put the cursor back to the begin of the line the cursor is on and clear everything on that line; [/li][li]and so on [/li][/ul]
All very basic, existing for over 30 years.

            printf "scanning: %s %s
" $Drive_Label $command_output_scandir
            while read -ra command_output_filescan; do

            printf "%s%s%s" ${command_output_filescan[0]} ${command_output_filescan[1]} ${command_output_filescan[2]}
            
            if  "${command_output_filescan[1]}" != *'OK'* ]; then
                echo -en '                                                                                                                        \r'
            elif  ${command_output_filescan[1]} = Symbolic ]; then
                echo -en '                                                                                                                        \r'
            else
                printf "
"
            fi
            #done < <(clamscan -r $command_output_scandir $Movefile_Parm --follow-dir-symlinks=0 --follow-file-symlinks=0)
            done < <(clamscan -r / --follow-dir-symlinks=0 --follow-file-symlinks=0)

This clip of the code from scanvirus. It now overwrites the lines with ‘ok’ in them. However, ‘Symbolic’ won’t work. I’v tried every combination of string usage including: ‘symbolic’. && on the same line. Nothing will make it work. Need help. Thanks.

I do not quite understand this. Is this an answer to our help posts? Did our posts help? Do you still have questionss about ANSI escape sequences and how aand when they work?

I’m skilled at many programming languages, but new with bash. I’m mixing styles because i’m using book and internet examples directly. I’v managed to get delete line working somewhat with this.

echo -n '\r                                                                                                                                                                                       \r'

Some lines of code I can’t understand. So, it’s a guess. FYI, I’m on public computers and my long message got erased when it forced a log off. I will read comments in a few days. Thanks.

echo -en "\E[2K\r"

Thanks, that worked also. There are many lines in my script code I don’t understand. I have it working.

You only show a statement, but you do not explain what you get. nor what you expected to get. We are not mindreaders! Simply saying “I can’t get the esc sequence to work.” tells us almost nothing.

Sometimes i’m on public computers with limited time and I miss ideas.

Try to understand. You have a piece of hardware (a character termminal at the end of an asynchronous line) that is wired to understand and act upon certain character combinations (“Escape sequences”) that are send to it over the line from the computer. You send those sequences and the terminal does what it should do. Only be sure that the terminal is capable to understand these VT100/ANSI codes (and Konsole and most other terminal emulators can do that).

Now these days the terminal has changed to a terminal emulator (e.g. Konsole) and the asynchronous line has shrunken to nothing, but it works still the same:

[ul]
[li]when you send the character A, the terminal will show the shape of an A where the cursor is and step the cursor one step forward; [/li]> [li]when you send the characters Esc[2K the terminal will put the cursor back to the begin of the line the cursor is on and clear everything on that line; [/li]> [li]and so on [/li]> [/ul]

All very basic, existing for over 30 years.

Thanks to all. I’m learning as I write code with two linux books and web resources. The ANSI code working. Now, I have syntax problems for another topic.