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

时间:2021-08-10 00:09:09

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行的文件的名称..另外我读了“阅读”的手册页,我没有得到像“-u”这样的选项..

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

那么,我如何使用文件描述符3(它是stdin的副本)中的“read”来读取。

3 个解决方案

#1


11  

try

read key <&3

#2


9  

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

#3


0  

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

#1


11  

try

read key <&3

#2


9  

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

#3


0  

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