bash 如何在bash脚本中使用“读取”从文件描述符3中读取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2087188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:34:16  来源:igfitidea点击:

How to read using "read" from file descriptor 3 in bash script?

linuxbashshell

提问by Abhijeet Rastogi

http://bash.cyberciti.biz/file-management/shell-script-to-simulate-unix-more-command/

http://bash.cyberciti.biz/file-management/shell-script-to-simulate-unix-more-command/

#!/bin/bash
# Write a shell script like a more command. It asks the user name, the
# name of the file on command prompt and displays only the 15 lines of
# the file at a time.
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------

counter=1
echo -n "Enter a file name : "
read file

if  [ ! -f $file ]
then
    echo "$file not a file!"
    exit 1
fi

# read file line by line
exec 3<&0
while read line
do
       # pause at line no. 15
    if [ $counter -eq 15 ]
    then
        counter=0 # reset counter
        echo " *** Press [Enter] key to continue ..."
        read -u 3 enterKey
    fi
    echo $line
    (( counter++ ))
done < $file

This emulates more command.. I get this error..

这模拟了更多命令..我收到这个错误..

read: 26: Illegal option -u

阅读:26:非法选项 -u

Make sure to enter the name of a file which has more than 15 lines.. Also I read the man page of "read" and i didnt get an option like "-u"..

确保输入超过 15 行的文件名。我还阅读了“read”的手册页,但没有得到“-u”之类的选项。

So, how do i read using "read" from the file descriptor 3 (which is copy of stdin).

那么,我如何使用文件描述符 3(这是标准输入的副本)中的“读取”进行读取。

回答by ghostdog74

try

尝试

read key <&3

回答by Ed Randall

It's also possible to get bash to assign a file descriptor to a variable; The next free descriptor number will be allocated starting from 10. For example:

也可以让 bash 将文件描述符分配给变量;下一个空闲描述符编号将从 10 开始分配。例如:

#!/bin/bash
FILENAME="my_file.txt"
exec {FD}<${FILENAME}     # open file for read, assign descriptor
echo "Opened ${FILENAME} for read using descriptor ${FD}"
while read -u ${FD} LINE
do
    # do something with ${LINE}
    echo ${LINE}
done
exec {FD}<&-    # close file

回答by mirro

Just for the record, here's yet another more script:

只是为了记录,这里还有另一个脚本:

# Author: Steve Stock
# http://www.linuxjournal.com/article/7385 (comments)

shmore() {
LINES=""
while read -d $'\n' line; do
  printf "%s\n" "$line"
  #echo "$line"
  LINES=".${LINES}"
  if [[ "$LINES" == "......................." ]]; then
     echo -n "--More--"
     read < /dev/tty
     LINES=""
  fi
done
return 0
}


shmore < file.txt

found here: http://codesnippets.joyent.com/posts/show/1788

在这里找到:http: //codesnippets.joyent.com/posts/show/1788