How to give input to command from a file

How to give input to command from a file

< 
A < symbol connects the command’s STDIN to the contents of an existing file.
Example
$ mail -s "Mail test" johndoe < /tmp/mymessage
Read from text file and send as mail

How to store command output in text file

>
Replaces the file’s existing contents
Example
echo "This is a test message." > /tmp/mymessage

To redirect both STDOUT and STDERR to the same place, use the >& symbol.
To redirect STDERR only, use 2>.

Example

$ find / -name core > /tmp/corefiles 2> /dev/null
This command line sends matching paths to /tmp/corefiles, discards errors, and
sends nothing to the terminal window.

How to append to contents of a file

>> 
Appends to the file
echo "This is a test message." >> /tmp/mymessage


How to execute command conditionally on success of command.

$ lpr /tmp/t2 && rm /tmp/t2

To execute a second command only if its precursor completes successfully, you
can separate the commands with an && symbol

cp --preserve --recursive /etc/* /spare/backup \
|| echo "Did NOT make backup"

Conversely, the || symbol executes the following command only if the preceding
command fails (produces a nonzero exit status).

No comments:

Post a Comment

Please share your views and comments below.

Thank You.