Home tmade.de

Home Wiki


Pipe to a file

(stdin)  0     #Standard input         
(stdout) 1     #Standard output        
(stderr) 2     #Standard error output
>              #redirects standard input
1>             #redirects standard output
2>             #redirects standard error output


ls /opt/ /otherfile > file 2>&1          #Redirect standard error output and standard output and to "file"
ls /opt /otherfile 2> /dev/null          #Redirect standard error output to /dev/null       
programm &> file                         #Redirect stdout AND stderr to "file"
echo "This is a test" 1> /dev/null       #Redirects standard output to /dev/null
ls xyz > info.txt 2> error.txt           #Output of ls will be redirected to "info.txt" (stdout) and if there is an error output (stderr) to "error.txt"
ls /opt /otherfolder 2> /dev/null        #Output Standartoutput - standarterroroutput will be piped to /dev/null
2> /opt/file                             #Redirects standard error output
2>>  /opt/file                           #Redirects standard error output (appends)

Explication check:

man -Len -Pless\ +/^REDIRECTION bash

Execution Dependency

command1 && command2                     #Command2 is only executed if command1 completed without errors
command1 || command2                     #Command2 is only executed if command1 completed with an error
command1 && command2 || command3         #


To check if a process is running/ return code:


(ps -ef | grep $SERVICE_NAME  | grep -v grep && echo "$SERVICE_NAME is running") || echo "$SERVICE_NAME is not running"
ps -ef | grep $SERVICE_NAME | grep -v grep && RESULT=running || RESULT=error

Another example:

innobackupex --apply-log $TARGET_FOLDER && /bin/echo "`/bin/date +'%d-%m-%Y %H:%M:%S'`: Backup successful." >> $BACKUPLOG || /bin/echo "`/bin/date +'%d-%m-%Y %H:%M:%S'`: Backup failed." >> $ERRLOGFILE

cp -a $(ls /var/log/nginx/*access.log.* | grep -E 'access\.log\.[1-3](\.gz)?$') /mnt/serverlogs/lb-web/

Pipe to a programm

Output one command as input to another command:

cat /etc/passwd | cut -d: -f1
cat /etc/group |cut -d: -f1
command1 | command2
ls -l /etc | less

Output to stdout and a file at the same time:

ls -l | tee outputfilename
ls -l | tee -a outputfilename            #Append to file outputfilename
linux/pipe.txt · Last modified: 2020/09/16 11:43 by tmade
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki