Dialog UNIX :! $ # @ * %

Source: Internet
Author: User

Dialog UNIX :! $ # @ * %

Dialog UNIX :! $ # @ * %
Learn more command line skills and operators
Better understanding of UNIX®The "strange" characters entered by the user. Learn how to use pipelines, redirection, operators, and other features in UNIX.

Now, you have®AIX®I have been working for a while. You have learned several basic commands to move, create, and modify files in the directory structure, view running processes, and manage users and systems. This is good, but you want to know about UNIX®What does the command entered by the Administrator mean. These commands contain many strange symbols. In this article|,>,>>,<,<<,[[And]]And other symbols in UNIX and Linux®And how to use&&,||,<,<=And!=Operator.

MPs queue

If you are familiar with UNIXPipe) Is something that is exposed every day. The pipeline was initially developed by Malcolm McIlroy. You can use the pipeline to direct the standard output (stdout) of a command to the standard input (stdin) of the next command ), in this way, the command chain for continuous execution is formed. You can use multiple pipelines on one command line. In many cases, the stdout of a command is used as the stdin of the next command, and the stdout of the second command is redirected to the stdin of another command, and so on.

For example, when troubleshooting or performing routine checks, one of the first tasks that most UNIX administrators do is to view the processes currently running on the system. Listing 1 demonstrates this check.

Listing 1. Routine process check example
# ps –ef     UID     PID    PPID   C    STIME    TTY  TIME CMD    root       1       0   0   Jul 27      -  0:05 /etc/init    root   53442  151674   0   Jul 27      -  0:00 /usr/sbin/syslogd    root   57426       1   0   Jul 27      -  0:00 /usr/lib/errdemon    root   61510       1   0   Jul 27      - 23:55 /usr/sbin/syncd 60    root   65634       1   0   Jul 27      -  0:00 /usr/ccs/bin/shlap64    root   82002  110652   0   Jul 27      -  0:24 /usr/lpp/X11/bin/X -x abx        -x dbe -x GLX -D /usr/lib/X11//rgb -T -force :0 -auth /var/dt/A:0-SfIdMa    root   86102       1   0   Jul 27      -  0:00 /usr/lib/methods/ssa_daemon -l ssa0    root  106538  151674   0   Jul 27      -  0:01 sendmail: accepting connections    root  110652       1   0   Jul 27      -  0:00 /usr/dt/bin/dtlogin -daemon    root  114754  118854   0   Jul 27      - 20:22 dtgreet    root  118854  110652   0   Jul 27      -  0:00 dtlogin <:0>        -daemon    root  131088       1   0   Jul 27      -  0:07 /usr/atria/etc/lockmgr        -a /var/adm/atria/almd -q 1024 -u 256 -f 256    root  147584       1   0   Jul 27      -  0:01 /usr/sbin/cron    root  155816  151674   0   Jul 27      -  0:04 /usr/sbin/portmap    root  163968  151674   0   Jul 27      -  0:00 /usr/sbin/qdaemon    root  168018  151674   0   Jul 27      -  0:00 /usr/sbin/inetd    root  172116  151674   0   Jul 27      -  0:03 /usr/sbin/xntpd    root  180314  151674   0   Jul 27      -  0:19 /usr/sbin/snmpmibd    root  184414  151674   0   Jul 27      -  0:21 /usr/sbin/aixmibd    root  188512  151674   0   Jul 27      -  0:20 /usr/sbin/hostmibd    root  192608  151674   0   Jul 27      -  7:46 /usr/sbin/muxatmd    root  196718  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.mountd    root  200818  151674   0   Jul 27      -  0:00 /usr/sbin/biod 6    root  213108  151674   0   Jul 27      -  0:00 /usr/sbin/nfsd 3891    root  221304  245894   0   Jul 27      -  0:05 /bin/nsrexecd  daemon  225402  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.statd    root  229498  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.lockd    root  241794  151674   0   Jul 27      -  0:51 /usr/lib/netsvc/yp/ypbind    root  245894       1   0   Jul 27      -  0:00 /bin/nsrexecd    root  253960       1   0   Jul 27      -  0:00 ./mflm_manager    root  274568  151674   0   Jul 27      -  0:00 /usr/sbin/sshd -D    root  282766       1   0   Jul 27   lft0  0:00 /usr/sbin/getty /dev/console    root  290958       1   0   Jul 27      -  0:00 /usr/lpp/diagnostics/bin/diagd    root  315646  151674   0   Jul 27      -  0:00 /usr/sbin/lpd    root  319664       1   0   Jul 27      -  0:00 /usr/atria/etc/albd_server    root  340144  168018   0 12:34:56      -  0:00 rpc.ttdbserver 100083 1    root  376846  168018   0   Jul 30      -  0:00 rlogind cormany  409708  569522   0 19:29:27  pts/1  0:00 -ksh    root  569522  168018   0 19:29:26      -  0:00 rlogind cormany  733188  409708   3 19:30:34  pts/1  0:00 ps -ef    root  749668  168018   0   Jul 30      -  0:00 rlogind

The list of currently running processes on the system may be as simple as listing 1; however, most production systems run more processes, which causespsLonger output. To shorten the list to the desired range, you can use pipelinesps –efThe standard output of is redirectedgrepTo search for the expected results. Listing 2 redirects the process list generated by listing 1grep, Search for the string "rpc" and "ksh ".

Listing 2. Redirect the process list to grep
# ps –ef | grep –E "rpc|ksh"    root  196718  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.mountd  daemon  225402  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.statd    root  229498  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.lockd    root  340144  168018   0 12:34:56      -  0:00 rpc.ttdbserver 100083 1 cormany  409708  569522   0 19:29:27  pts/1  0:00 -ksh cormany  733202  409708   0 19:52:20  pts/1  0:00 grep -E rpc|ksh

When stdout is redirected to stdin multiple times, the pipeline usage can be complicated. In the following examplepsAndgrepFor example, redirect its stdout to anothergrepTo exclude strings containing "grep" or "ttdbserver. When the lastgrepWhen the operation is complete, use the pipeline again to redirect stdout toawkStatement, which is used to output all processes whose PID is greater than 200,000:

# ps –ef | grep –E "rpc|ksh" | grep -vE "grep|rpc.ttdbserver" |    awk -v _MAX_PID=200000 '{if ($2 > _MAX_PID) {print "PID for    process",$8,"is greater than", _MAX_PID}}'PID for process /usr/sbin/rpc.statd is greater than 200000PID for process /usr/sbin/rpc.lockd is greater than 200000PID for process -ksh is greater than 200000

Figure 1 graphically describes the sequence in which stdout redirects the command to stdin of subsequent commands.

Figure 1. Pipeline example

Back to Top

Use>,>, <, and <to execute data redirection

Another important aspect of executing commands through the command line interface (CLI) is the ability to write various outputs to one device or read inputs from another device to the command. To write a command output, you must add a greater than sign (> or>) and the target file name or device after the command is executed. If the target file does not exist and you have the write permission on the target directory,> and> Create the file and set the permission based on your umask, then, write the command output to the created file. However, if the file exists,> attempts to open the file and overwrite the entire content. If you want to Append content to this file, you only need to use>. It can be considered to be used to move the output data stream of the left command to the target file on the right (that is<cmd>-><output>-><file>).

The following example executesps –efAnd redirect the output to the file.ps_out:

# ps –ef | grep –E "rpc|ksh" > ps_out

The following code executes the expanded pipeline example and redirects the output to the same file (ps_out), but appends it to the end of the current data:

# ps –ef | grep –E "rpc|ksh" | grep -vE "grep|rpc.ttdbserver" |    awk -v _MAX_PID=200000 '{if ($2 > _MAX_PID) {print "PID for    process",$8,"is greater than", _MAX_PID}}' >> ps_out

Listing 3 shows the output of the first two redirects.

Listing 3. Redirection output
# cat ps_out    root  196718  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.mountd  daemon  225402  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.statd    root  229498  151674   0 11:00:27      -  0:00 /usr/sbin/rpc.lockd    root  340144  168018   0 12:34:56      -  0:00 rpc.ttdbserver 100083 1 cormany  409708  569522   0 19:29:27  pts/1  0:00 -ksh cormany  733202  409708   0 19:52:20  pts/1  0:00 grep -E rpc|kshPID for process /usr/sbin/rpc.statd is greater than 200000PID for process /usr/sbin/rpc.lockd is greater than 200000PID for process -ksh is greater than 200000

If you only use>Only stdout of the command is redirected to the output. However, in addition to stdout, there is also stderr output: the former is represented1The latter is2. There is no difference in output redirection in UNIX. You only need>Add the required output type (for example,1>,2>) To Tell shell where to route the output.

Listing 4 attempts to list fileA.tar.bz2 and fileC.tar.bz2. However, for example, the First Command (lsThe filec.tar.bz2 does not exist. Fortunately, stdout and stderr can be redirected to ls. out and ls. err respectively to see the error message.

Listing 4. Listing fileA.tar.bz2 and fileC.tar.bz2
# lsfileA.tar.bz2   fileAA.tar.bz2  fileB.tar.bz2   fileBB.tar.bz2# ls fileA.tar.bz2 fileC.tar.bz2 1> ls.out 2> ls.err# cat ls.outfileA.tar.bz2# cat ls.errls: 0653-341 The file fileC.tar.bz2 does not exist.

In AIX, stdout and stderr are used>And>>The same rules are applied. For example, you can use the same output file for future tests, as shown in listing 5.

Listing 5. Use the output file for future tests
# ls fileB.tar.bz2 fileD.tar.bz2 1>> ls.out 2>> ls.err# cat ls.outfileA.tar.bz2fileB.tar.bz2# cat ls.errls: 0653-341 The file fileC.tar.bz2 does not exist.ls: 0653-341 The file fileD.tar.bz2 does not exist.

Sometimes stdout and stderr may need to be written to the same file or device. There are two methods. The first method is1>And2>Redirect to the same file:

# ls fileA.tar.bz2 fileC.tar.bz2 1> ls.out 2> ls.out# cat ls.outfileA.tar.bz2ls: 0653-341 The file fileC.tar.bz2 does not exist.

The second method is simpler and faster. Experienced UNIX users prefer this method:

# ls fileA.tar.bz2 fileC.tar.bz2 > ls.out 2>&1# cat ls.outfileA.tar.bz2ls: 0653-341 The file fileC.tar.bz2 does not exist.

Let's break down this statement. First, executels fileA.tar.bz2 fileC.tar.bz2. Then use> ls.outRedirects stdout to ls. out. Use2>&1Redirects stderr to the stdout (ls. out) to be redirected ).

Remember to redirect the output to files and other devices. Data can be redirected to printers, floppy disks, terminal types (TTY), and various other devices. For example, if you want to send a message to a user in all sessions (or TTY), you only need to process it cyclically.whoAnd redirect a message to TTY (if you have sufficient permissions), see Listing 6.

Listing 6. redirect a message to a TTY
# for _TTY in 'who | grep "cormany" | awk '{print $2}''> do>   _TTY="/dev/${_TTY}">   echo "Sending message to cormany on ${_TTY}">   echo "Test Message to cormany@${_TTY}" > ${_TTY}> doneSending message to cormany on /dev/pts/13Test Message to cormany@/dev/pts/13Sending message to cormany on /dev/pts/14

Back to Top

Stdin instead of stdout

Although use> and> is a relatively easy-to-Master concept for most people, it is often difficult to use minor signs (<and <. Consider> and>, and think they move the output data stream of the left command to the target file on the right, which is the easiest to understand. The same method applies to <and <. When using <, it is essentially to execute a command with a provided stdin. That is to say, use the Command provided to the left as stdin (that is<cmd><-<data>).

For example, assume that you want to send an email containing an ASCII text file to another user. You can use pipelinescatStdout redirectionmailStdin (that iscat mail_file.out | mail –s "Here's your E-mail!" acormany@yahoo.com), You can also redirect the file contentmailStdin of the command:

# mail –s "Here's your E-mail!" acormany@yahoo.com < mail_file.out

Use <(also knownHere-document) Saves the formatting time and makes the command execution easier. By using <, the text string is redirected to the executed command as stdin, but you can continue to enter information until it reaches the end identifier. You only need to enter the command, enter <and termination identifier, enter any content you need, and finally enter the termination identifier on a new line. By using here-document, you can retain spaces, line breaks, and so on.

For example, UNIX must process the following fiveechoStatement:

# echo "Line 1"Line 1# echo "Line 2"Line 2# echo "Line 3"Line 3# echo "Line 4"Line 4# echo "Line 5"Line 5

You can use the following code to replace multipleechoStatement, UNIX only needs to process the execution once:

# cat << EOF> Line 1> Line 2> Line 3> Line 4> Line 5> EOFLine 1Line 2Line 3Line 4Line 5

You can also use tabs to make the content in the shell script more clean. You only need to put a hyphen (-):

# cat <<- ATC>Line 1>Line 2>Line 3>Line 4>Line 5> ATCLine 1Line 2Line 3Line 4Line 5

The example in listing 7 demonstrates how to use the things discussed so far in this article.

Listing 7. Combination of CLI
# cat redirect_example#!/usr/bin/kshcat <<- ATC | sed "s/^/Redirect Example => /g" >> atc.outThis is an example of how to redirectstdout to a file as well as pipe stdout into stdinof another command (i.e. sed), all done insidea here-document.Cool eh?ATC

Now, let's look at the scripts for redirection and pipelines.

# ./redirect_example# cat atc.outRedirect Example => This is an example of how to redirectRedirect Example => stdout to a file as well as pipe stdout into stdinRedirect Example => of another command (i.e. sed), all done insideRedirect Example => a here-document.Redirect Example =>Redirect Example => Cool eh?
Sub-shell

Sometimes, you need to execute several commands together. For example, if you want to perform an operation in another directory, you can use the code in listing 8.

Listing 8. Execute several commands at the same time
# pwd/home/cormany# cd testdir# tar –cf ls_output.tar ls.out?# pwd/home/cormany/testdir

This is valid, but note that after performing these steps, you are no longer in the original directory. By placing these commands in their own sub-shells, they will be executed as sub-shell instances. Listing 9 demonstrates how to use a sub-shell to execute the same code.

Listing 9. Using a sub-shell to execute several commands at the same time
# pwd/home/cormany# (cd testdir ; tar -cf ls_output.tar ls.out?)# pwd/home/cormany 

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.