How to Insert Ctrl+D into My Linux Script

How we can code Ctrl+D in a shell script?

How about checking the return status of read before going into your case?

E.g.:

if [ $? -eq 1 ] ;
...
fi

I don't think you can change the behaviour of read to trap the ctrl-d in the result variable.

how to input Ctrl-D

CTRL+D in Linux results in the operating system sends an 'EOF' signal to the program. In order to input CTRL+D, you need to tell the operating system to see it as normal input. To do that, you can type CTRL+V first, and then type CTRL+D

I tried to press Ctrl D in script and I broke it?

You're looking to break out of the loop on read failure:

read number || break;

When read detects an error, it returns non-zero, which, in the shell, is "false". The || will see the false on the left, and thus proceed to evaluate the right, but when the read succeeds, returning zero, the || will not evaluate the right, allowing the loop to continue.

EoF / Ctrl+D command in Bash + Python

You probably need to send the EOF character, which is typically CONTROL-D, not the three characters E, O, and F.

        self.device.sendline("\x04")

How to automate ctrl+d action in the ssh expect script in bash?

You can simulate pressing Ctrl-D by sending the ^D/ character:

send "\x04"

mail -s waiting for CTRL+D in bash script

This is what every command which reads standard input will do if you do not provide it with standard input from another source. The usual solution is to redirect it to read from /dev/null.

mail -s "Netapp NFS Share is mounted" "$mailto1"</dev/null

However, you probably also want to fix the other antipatterns in your script. See inline comments in this refactored version.

#!/bin/bash

mailto1=abc@test.com
# avoid useless cat; refactor sed into Awk
# pipe to while read instead of using a for loop
awk '$1 !~/#|^$|swap/ && ++c > 7 {print $2}' /etc/fstab |

while read -r FS; do
# avoid useless use of if [ $? -eq 0 ]
# quote variable
if df -hPT | grep -wq "${FS}"; then
# add redirect; quote variable
mail -s "Netapp NFS Share is mounted" "$mailto1" </dev/null
else
# print diagnostics to stderr; include script name
echo "$0: Netapp NFS share is not mounted" >&2
fi
done

Pointers to more information:

  • When to wrap quotes around a shell variable?
  • Useless use of cat?
  • Checking the success of a command in a bash `if [ .. ]` statement
  • https://mywiki.wooledge.org/DontReadLinesWithFor
  • Useless use of grep and sed

The script validation tool at http://shellcheck.net/ can diagnose some, but not all, of these errors and antipatterns.

... As a further refinement, maybe only run df once and pass that to Awk, too:

if  df -hPT |
awk 'BEGIN { r = 1 }
# NR is equal to FNR while we are processing the first input file, i.e. stdin from df
NR==FNR { a[++n] = $0; next }
$1 !~/#|^$|swap/ && ++c > 7 {
for (i=1; i<=n; ++i)
if ($2 ~ a[i]) {
r=0; exit r } }
END { exit r }' - /etc/fstab
then
mail -s "Netapp NFS Share is mounted" abc@test.com </dev/null
else
echo "$0: Netapp NFS share is not mounted" >&2
fi

This also illustrates exactly how the purpose of if is really just to examine the exit status of the pipeline you run as its argument.

If you can be sure that there will only ever be exactly one mounted NFS share, turning the logic around would probably allow you to simplify the final script further.

How to use CTRL+D to end input?

You can use read -r VARNAME for that, e.g.:

#!/bin/bash

sum=0
while read -r n; do
((sum += n))
done
echo "$sum"


Related Topics



Leave a reply



Submit