cat and line

1. cat

This command used to dispay or create file.

(1) cat [enter]

cat with no input parameters, that mean it will use standard input and standard output. So when you type some characters and enter, it will echo the same content in statndard input. Example:

$ cat

first

first       /* system echo */

sendary

sendary    /* system echo */

third

third      /* system echo */

^C$        /* ctr+c to end cat command */

(2) cat >> filename

Used to append all the standard input into the given file. Example:

$ cat >> test4.txt

hello cat

used >>

end

^C$

If there no file names test4.txt, this command will create a new one, and put all the messages from standard input to teset4.txt.

(3) cat >> filename<<!   /* character ! signal the command is finished */

Been used to append all the messages from standard input to the given file, and end the command with given character( is this exampe we use ! as end character). Example:

$ cat >> test5.txt <<!   /* tell system the ! is end character */

> hello cat       /* it will appear prompt > %/

> finished

> !                /* when ! appears, the command is finished */

(4) cat filename

Been used to display the specified file on standard output ( screen).

(5) cat file1 file2 >> file3

Append all the txt in file1 and file2 to file3.

2. line

Read one line from standard input and display it on the standard output. It be used to get message from user terminal, and it often be used in loop.

(1) line [enter]

$ line

1    /* input value */

1    /* output value */

$

It look like as cat command, they are both get value from standard input and write value on the starndard output. And the difference is the cat command will never be finished until user type ctr+c, but line command will be finished after it get a line from standard inpput. So cat command with no parameters just like line command in loop:

while line    /* here we can use >> to redirect output to a file

do

    true

done

We can test this use shell script. Vi test1.sh and type the message before. And execute test1.sh

$ sh test1.sh

1

1               /* standard output */

2

2               /* standard output */

3

3               /* standard output */

^C$

$ cat

1

1

2

2

3

3

^C$

You can see they behavior are same.

We can also assign the output of line command to a variable, and get read one line from not standard input but a pipe. For example:

$ ls | cut -c 1-3|while job=`line`

> do

>  if [ "${job}" = "wz_" ]

> then

> echo ${job}

> fi

> done

    In this sample, command line read one line from pipe that generated by cut command, and output one line to variable job. So we can use variable job in the later statement.

Maybe line can read one line from four way:

    1) standard input (user termianl)

    2) data buffer     (mentioned in another article)

3) pipe    for example:

4)  file

---------------------------------------------------------------------

 $ ls | cut -c 1-3|while job=`line`

> do

>  if [ "${job}" = "wz_" ]

> then

> echo ${job}

> fi

> done

---------------------------------------------------------------------

In this example line command read lines from a pipe and output the lines to the variable job one by one.

Another example shown as belown:

ls -l |awk '{if(($6=="Aug")&&($7=="1")) print $9}'| while  job=`line`

do

  grep AMATPS "${job}">>/dev/null     

//if you will search two words, just quote them in a double quote

  echo $? ${job}|awk '{if($1=="0") print($0)}'

done

Read data from a file:

---------------------------------------------------------------------

$ while a=$(line)

> do

> echo $a

> done <top.txt

---------------------------------------------------------------------

Attantion:

In above two exmaples, we use `line` and $(line), they are same thing, they are both used to exec command line.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章