Pages

Saturday, January 2, 2016

Understanding standard input and standard output (redirection operator)

Home

To empty a file or truncate a file to 0 size

# > file_name

To replace content of a file

# echo “New file initialized” > file_name

To append content to a file as a new line

# echo “New content with newline” >> file_name

To append content to a file without new line (by continuing last line )

# echo -n “New content without newline” >> file_name

To append content to a file

# echo “New Content” >> file_name

To read first line of a file and store it in a variable

# read variable_name < file_name
# echo $variable_name
# variable1=$(head -1 file)
# echo $variable_name
# variable1=`head -1 file`
Note: Preferred one for variables is with use of $ 

To read a file line by line and store it in a variable (File with single column and default newline separator)

# cat file_name
One
Two
Three
Four
Five
Six
# while read variable_name; do
    echo $variable_name
done < file_name
# cat file_name | while read variable_name1; do 
    echo $variable_name1 
done
# for variable_name2 in $(cat file_name); do
    echo $variable_name2;
done
Note: Preferred one using while loops as for loop can behave differently in case of special characters (e.g. space, TAB) 

To read random line from a file and store it in a variable (File with single column and default newline separator)

# cat file_name
One
Two
Three
Four
Five
Six
# read -r variable_name < <(sort -R file_name)
# echo $variable_name
# variable_name1=$(sort -R file_name | head -1)
# echo $variable_name1
# variable_name1=`sort -R file_name | head -1`
# echo $variable_name2

To read only 3 columns/fields form a file into variables

# while read -r field1 field2 field3 throwaway; do
    echo $field1"---"$field2"---"$field3;
done < file_name
# while read -r -d _ field1 field2 field3 throwaway; do
    echo $field1"---"$field2"---"$field3;
done < file_name
#

To find number of lines, words, characters in a file and store it in a variable

# wc file_name
# read no_lines no_words no_chars throwaway < <(wc file_name)
# read no_lines no_words no_chars _ < <(wc file_name)

To find value in 2nd and 5th column of a one line variable and store it in a variable

# string="one 2 THREE 4 FIVE 6"

Using awk

# sec_var=$(echo $string|awk '{print $2 }')
# echo $sec_var
# five_var=$(echo $string|awk '{print $5 }')
# echo $five_var

Using HERE-STRING Here the <<< is a here-string, which lets you pass strings directly to the standard input of commands.

# read _ read_sec _ _ read_five _ <<< "$string"
# echo $read_sec
# echo $read_five

To find size of file and store it in a variable

# file_size=$(wc -c < file_name)
# file_size=`wc -c < file_name`
Note: wc -c prints the number of chars (bytes) in the file file_name

# file_size_1=$(du -sh file_name)
# file_size_2=$(du -sm file_name)
# file_size_3=$(du –sk file_name)
# file_size_1=`du -sh file_name`
# file_size_2=`du -sm file_name`
# file_size_3=`du –sk file_name`
Note: Preferred one is the one with “$”

To redirect the standard output of a command to a file

# command > file_name

Can be visualized as below,
To redirect the standard error of a command to a file

# command 2> file_name

Can be visualized as below,
To redirect both standard output and error of a command to a file

# command &> file_name
# command >& file_name
# command > file_name 2>&1
Note:- Preferred one are first and third one

Can be visualized as below,
To discard standard output of a command to a file

# command > /dev/null

Can be visualized as below,
To discard standard error of a command to a file

# command 1> file_name 2> /dev/null

Can be visualized as below,
To discard standard output and error of a command to a file

# command &> /dev/null
# command > /dev/null 2>&1

Can be visualized as below,

To redirect contents of a files to the standard input of a command

# command < file_name
# read -r line < file

Can be visualized as below,

To redirect list of strings i.e. multiline to the standard input of a command

# command <<EOL
One
Two
Three
Four
EOL

# sed 's|http://||' <<EOL
http://url1.com
http://url2.com
http://url3.com
EOL

To redirect a single line of text to the standard input of a command

# command <<< "One Two Three"
# echo "Four Five Six" | command

To redirect standard output of all command
# exec 1>file_out
# command1
# command2

To redirect standard error of all command

# exec 2>file_error
# command1 
# command2

To redirect standard output and error of all command

# exec &>file_name
# command1 
# command2

# exec >file_name 2>&1
# command1 
# command2

To use exec command in script to capture all standard error and standard output  in separate file

#!/bin/bash
exec 1> stdout.out 2> stderr.err
ls -lrt /root
ls -lrt /root1
Note: For all command used in script standard output will be captured in stdout.out file and standard error will be captured in stderr.err file respectively 

To send the output from multiple commands to a file

# (command1; command2) >file_name

To prevent overwriting the contents of a file when redirecting output

Open first shell session

# set -o noclobber
# echo "Four Five Six" > file_name
# echo "One Two Three" > file_name
# echo "One Two Three" >| file_name

Open Second shell session

# echo "Four Five Six" > file_name
# echo "One Two Three" > file_name

To redirect standard input to a file and print it to standard output

# command |tee file_name

Can be visualized as below,
To send standard output of one command to standard input of another command
# command1 | command2

Can be visualized as below,
To send standard output and error of one command to standard input of another command
# command1 |& command2
# command1 2>&1 | command2

Can be visualized as below,
How order of redirection matters
# echo hello >/tmp/example
# echo >/tmp/example hello
# >/tmp/example echo hello

To swap standard output and standard error

# command 3>&1 1>&2 2>&3
# command 3>&1 1>&2 2>&3 3>&-

Can be visualized as below,

Next bash setups 3>&1 redirection. This creates file descriptor 3 to be a copy of file descriptor 1:
Next bash setups 1>&2 redirection. This makes file descriptor 1 to be a copy of file descriptor 2:
Next bash setups 2>&3 redirection. This makes file descriptor 2 to be a copy of file descriptor 3:
The file descriptor table then looks like this:

To send standard output to one command and standard error to another command
# command 1> >(command1) 2> >(command2)

Back To Top
Home

No comments:

Post a Comment