Read, redirect, and file handles and number of process controls in the Linuxshell script

Source: Internet
Author: User
Tags parent directory

1. Redirect as read input

Read reads a line from the standard input and uses the Read mate redirection to enter the file as the read callout.

#!/bin/bash#name:links.sh# revision:1.1# date:2017//# author:yemo# Email: [Email protected]# Website: # Description:print the ip-links# Notes: #----------------------------------------------------------- --------------------# copyright:2017 (c) yemo# License:gpl#while read line;d oecho "line= $line" sleep 2done 

Next Run

[Email protected]:/home/yemo# bash file_des.sh line=#!/bin/bashline= #name: links.shline=# revision:1.1line=# Date : 2017/6/line=# author:yemoline=# Email: [Email protected]line=# website:line=# Description:print the ip-linksline=# notes:line=#-------------------------------------------------------------------------------

Output the contents of my Bash header template file

2. Word File descriptor

When the file is opened, the system assigns each open file a descriptor for maintenance, which usually includes the system open file descriptor table, the process-level file descriptor tables (file operator flags and file handle references), and the file system I-node.

Basic usage

exec fd<>file #创建一个文件句柄 exec fd>&-or Exec-fd<&     ; #关闭文件句柄

Create a new file descriptor in/PROC/PID/FD and view the current Shell's PID through $$.

[Email protected]:/home/yemo# cd/proc/$$/fd [email protected]:/proc/6833/fd# ls0 1 2 255

Create a descriptor for file/home/yemo/model.txt Fd6

[Email protected]:/proc/6833/fd# exec 6<>/home/yemo/model.txt[email protected]:/proc/6833/fd# ls0 1 2 255 6

Open Fd6, view content

[Email protected]:/proc/6833/fd# cat 6#!/bin/bash#name:links.sh# revision:1.1# back pretty long omitted, this is not the point

We can read and write files through Fd6 model.txt

[Email protected]:/proc/6833/fd# echo "Hello Girl" >>6[email protected]:/proc/6833/fd# tail-1 6hello Girl

What happens if I delete a file, Fd6?

[Email protected]:/proc/6833/fd# stat-c%i/home/yemo/model.txt #先查看下inode128531 [email protected]:/proc/6833/fd# stat -C%i 649938[email protected]:/proc/6833/fd# rm/home/yemo/model.txt [email protected]:/proc/6833/fd# ls0 1 2 255 6

Fd6 is shown in red, indicating that the file is no longer present, we open it.

[Email protected]:/proc/6833/fd# cat 6#!/bin/bash#name:links.sh# revision:1.1# Here omit a lot of lines Hello Girl

Can still open, the deletion of the file is just the deletion of the parent directory of the existence of a filename, when the file in use, and will not release the Inode, as long as the block data is not overwritten can be found, where the file is not directly open Fd6 redirect back on the good.

3. Pass the file content to read via FD

[Email protected]:/proc/6833/fd# exec 6>&-#关闭文件句柄 [email protected]:/proc/6833/fd# ls0 1 2 255

New script Test

Use the-u option:

-UFD read from file descriptor FD instead of the standard input

#!/bin/bash#filename:     file_des.sh# revision:    1.1#  Date:        2017-06-09# Author:       yemo# email:       [email protected]# qq :787743742# website:     # description: # notes:        # ------------------------------------------------------------------------- ------# copyright:   2017  (c)  yemo# License:      gplexec 6<>/home/yemo/model.txt                         #创建文件句柄 (FD6) while read -u  6 line ;d o                     #循环读取文Piece echo  "line= $line" sleep 2done exec 6>&-                             #文件用完, close the file handle unset line                           #自定义变量, End release

Execution Result:

[Email protected]:/home/yemo# bash file_des.sh line=#!/bin/bashline= #name: links.shline=# revision:1.1line=# Date: 2017/6/line=# author:yemoline=# Email: [Email protected] #后面的内容就省略了

On the surface, this is the same as using the redirection effect alone, but when the program requires multiple inputs, a separate redirect can only allow read to read a single file and the content cannot be specified for each individual input.

4. Creating multiple processes with file handles

Read-u reading a string from a file descriptor

Sets the number of return numbers in the file descriptor to the number of preset processes

Build multiple processes through loops

#!/bin/bash#filename:     mproc.sh# revision:    1.1#  date:        2017-06-09# author:       yemo# email:       [email protected]# qq:787743742#  website:     # description: this is use for test  shell multiprocess# Notes:       # ------------------- ------------------------------------------------------------# copyright:   2017  (c)  yemo# License:     GPL## This program is free  Software; you can redistribute it and/or# modify it under the  terms of the gnu general public license# as published by  the free software foundation; either version 2# of the license, or  (at  Your option)  any later version.# this program is distributed in  the hope that it will be useful,# but WITHOUT ANY  Warranty; without even the implied warranty# of merchantability or  FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General  Public license for more details.## you should have received a  copy of the gnu general public license## if any changes  are made to this script, please mail me a copy of  the changes# -------------------------------------------------------------------------------# Version 1.0#main_sub () &NBSP;{LOCAL&Nbsp;loop_time=$ ( $1 % 7 ) echo -e  "$i \ t----\ t---$loop _time begin" sleep   $loop _timeecho -e  "$i \ t-----\t$loop_time end"}tmp_file= "/tmp/.fifo-$$" mkfifo  $tmp _ file                                          #创建fifo文件用于进程通信    exec 6<> $tmp _file                                         #创建文件句柄6为管道rm  -f  $tmp _filemy_thread=6                                              #限制进程数runtime =50for  ( i=0 ; i <   $my _thread ; i++ ))  ; doechodone >&6                                               #循环写入回车符到管道for   (( i=0 ; i <  $my _thread ; i++ )  ; do  read -u 6                                       #进程数量控制 {main_sub  $iecho  >&6        #每次函数执行完写入一个回车符 to ensure that the number of processes is specified}&                                          #后台运行donewait                                                 # The parent process waits for the child process to end before exiting Exec 6<&-exit 0




This article is from the "Night Empty Watch Snow" blog, please be sure to keep this source http://12550795.blog.51cto.com/12540795/1934220

Read, redirect, and file handles and number of process controls in the Linuxshell script

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.