Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

bash

shell

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

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"..

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

like image 910
Abhijeet Rastogi Avatar asked Jan 18 '10 15:01

Abhijeet Rastogi


People also ask

What is >& 2 in shell script?

and >&2 means send the output to STDERR, So it will print the message as an error on the console.

How do I read a string in Bash?

Method 1: Using read command and while loop We can use the read command to read the contents of a file line by line. We use the -r argument to the read command to avoid any backslash-escaped characters.


1 Answers

try

read key <&3
like image 161
ghostdog74 Avatar answered Sep 21 '22 21:09

ghostdog74