Difference between revisions of "Bash:InputOutput"

From wiki
Jump to navigation Jump to search
 
Line 5: Line 5:
 
;hexdump -C <file>
 
;hexdump -C <file>
 
:Print hexdump of <file> next to the 'normal' representation
 
:Print hexdump of <file> next to the 'normal' representation
 +
 +
==Redirecting==
 +
;command > <file>
 +
:Send standard output to new <file> (<file> can be /dev/null if you don't want to see the output)
 +
 +
;command >> <file>
 +
:Append standard output to <file>
 +
 +
;command > <file> 2>&1
 +
:Send erroroutput the same file as standard output
 +
 +
;cmd 2>&1 > <file> | cmd2
 +
:stderr wordt eerst aan stdout gekoppeld (gaat naar pipe) stdout wordt daarna aan file gekoppeld
 +
;echo >&2 “error”
 +
:redirection can be anywhere in the command, this sends “error” to stderr.
 +
;exec > <file>
 +
:redirect output current shell to <file> (eg for logging)
 +
;exec 3>&-
 +
:Close file-descriptor 3
 +
;exec 3>&2; exec 2> <file>; exec 2>&3
 +
:Save stderr in file-descriptor 3, connect stderr to <file> and restore original stderr
 +
;exec 3< <file>
 +
:Open file-descriptor 3 to <file>
 +
;read -u 3 LINE
 +
:Read a line from file-descriptor 3 and store it in variable LINE
 +
;exec < <file> 3<&0
 +
:Redirect stdin to file and connect file-descriptor 3 to stdin
 +
 +
;command << MARKER
 +
;input
 +
;input
 +
;MARKER
 +
:stdin reads until MARKER (EOF is often used as marker)

Latest revision as of 14:04, 3 January 2020

echo -n <string>
Print <string> without newline
hexdump -C <file>
Print hexdump of <file> next to the 'normal' representation

Redirecting

command > <file>
Send standard output to new <file> (<file> can be /dev/null if you don't want to see the output)
command >> <file>
Append standard output to <file>
command > <file> 2>&1
Send erroroutput the same file as standard output
cmd 2>&1 > <file> | cmd2
stderr wordt eerst aan stdout gekoppeld (gaat naar pipe) stdout wordt daarna aan file gekoppeld
echo >&2 “error”
redirection can be anywhere in the command, this sends “error” to stderr.
exec > <file>
redirect output current shell to <file> (eg for logging)
exec 3>&-
Close file-descriptor 3
exec 3>&2; exec 2> <file>; exec 2>&3
Save stderr in file-descriptor 3, connect stderr to <file> and restore original stderr
exec 3< <file>
Open file-descriptor 3 to <file>
read -u 3 LINE
Read a line from file-descriptor 3 and store it in variable LINE
exec < <file> 3<&0
Redirect stdin to file and connect file-descriptor 3 to stdin
command << MARKER
input
input
MARKER
stdin reads until MARKER (EOF is often used as marker)