2010-01-18 12 views
6

http://bash.cyberciti.biz/file-management/shell-script-to-simulate-unix-more-command/बैश स्क्रिप्ट में फ़ाइल वर्णनकर्ता 3 से "पढ़ने" का उपयोग करके कैसे पढ़ा जाए?

#!/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 

यह और अधिक आदेश emulates .. मैं इस त्रुटि मिलती है ..

पढ़ने: 26: -u

अवैध विकल्प के नाम दर्ज करना सुनिश्चित करें एक फाइल जिसमें 15 से अधिक लाइनें हैं .. इसके अलावा मैंने "पढ़ा" के मैन पेज को पढ़ा और मुझे "-u" जैसे विकल्प नहीं मिला ..

तो, मैं फाइल डिस्क्रिप्टर 3 (जो stdin की प्रति है) से "पढ़ने" का उपयोग करके कैसे पढ़ूं।

+0

क्या बैश के संस्करण: अगले मुक्त वर्णनकर्ता संख्या 10 उदाहरण के लिए से शुरू आवंटित किया जाएगा? –

उत्तर

11

बस रिकार्ड के लिए

read key <&3 
+0

: ओएमजी: यह आसान है। thanx .. –

+0

मुझे मिलता है: '-बैश: 3: खराब फ़ाइल वर्णनकर्ता' – Timo

0

कोशिश, यहाँ अभी तक एक और अधिक स्क्रिप्ट है:

# 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 

मिली: http://codesnippets.joyent.com/posts/show/1788

5

यह भी करने के लिए फ़ाइल वर्णनकर्ता आवंटित करने के लिए बैश प्राप्त करना संभव है एक परिवर्तनीय;

#!/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 
संबंधित मुद्दे