I/O

grep <str> <file>
Search str in file, or in output of a command (use |)
tail <file>
Show de last 20 lines from <file>
tail -f <file>
Keep file open and show anything added to the file. tail -f
.sh_history (look what a uses is doing on the commandline)
head -X <file>
Show first X regels from <file>.
fold -w 999 <file>
Each line is max 999 characters long
strings <file>
Show only printable characters from <file>
pg <file>
Display <file> page by page, using (-)<no> you can
browse, quit to leave the file immedeatelly.
less <file>
Vi keys keay be used to navigate through the file
tr “[:upper:]” “[:lower:]”
tr “[abc]” “[xyz]”
Transtlate, make all lowercase
replace all a’s with y, all b’s with y and all c’s with z
works from stdin to stdout. All sort of translations can be done
read var
read -p “Enter value for VAR1: ” VAR1
read VAR1 VAR2
Read variabele(s) from stdin (keyboard or pipe), with prompt.
<stat1> | <stat2>
Pipe, stdout from <stat1> is connected to stdin
<stat2>.

Formatting

 

printf “%04dn” 3
Output: “0003”, if the 0 is omitted there are spaces in front
printf “%7.3fn 34.3
Output: “034.200”, the first number specifies the total length of the output string, the second figure the number of digest after the .
 

 

Redirect

<command> > file
stdout for command is file (file is created)
<command> >> file
stdout for command is file (output is appended to file)
<command> > file 2>&1
Connect stderr to stdout, everything goes to file
<command> < file
stdin for command is file (default is keyboard)
<command> << MARKER
input
input
MARKER
stdin reads until MARKER
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
read LINE
Read a line from stdin (connect to file with redirect above) and
store it in LINE

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.