Консультация № 186195
26.05.2012, 12:22
99.97 руб.
0 2 0
Здравствуйте уважаемые эксперты! Прошу вас о срочной помощи, нужно написать Shell - процедуру, которая выполняет действия:
читает содержимое первого файла, передаваемого в качестве первого параметра;
читает содержимое второго файла, передаваемого в качестве второго параметра;
выводит на экран каждые 7 с попеременно две строки из первого и одну строку из
второго файла, перемещаясь по файлам циклически в обратном порядке.

Прошу всё написать как можно проще (чтобы легко читалась и чтобы результат работы процедуры легко анализировались), желательно с комментариями.
Заранее большое спасибо!
Очень жду ответа!

Обсуждение

Неизвестный
30.05.2012, 08:11
общий
Добрый день! Нашёл похожую программу, только в ней вывод на экран идёт по порядку (с 1-й строки), а не в обратном, прошу помочь исправить на обратный, заранее спасибо :

01.#!/bin/bash
02.
03.# Program cyclically reads and prints $FC1 lines from 'file1' and $FC2 lines from 'file2' every $SEC seconds.
04.
05.
06.FC1=2 # number of lines to read from file1
07.FC2=1 # number of lines to read from file2
08.SEC=7 # for cycle - time interval between each output
09.
10.NORMAL='\033[0m'
11.BRED='\033[1;31m'
12.GREEN='\033[0;32m'
13.WHITE='\033[0;29m'
14.YELLOW='\033[0;33m'
15.BLUE='\033[0;36m'
16.
17.if [ $# -lt 2 ]; then
18. printf "${BRED}Invalid number of arguments.\n${GREEN}Usage: $NORMAL$0 file1 file2\n"
19. exit
20.fi
21.
22.
23.printf "${WHITE}Check whether files exist and are readable$NORMAL\n"
24.f1="$1"
25.if [ ! -f $f1 ]; then
26. printf " ${BRED}File '$f1' does not exists!$NORMAL\n"
27. exit 1
28.elif [ ! -r $f1 ]; then
29. printf " ${BRED}File '$f1' couldn't be read!$NORMAL\n"
30. exit 2
31.fi
32.printf " ${GREEN}File '$f1' OK ${NORMAL}\n"
33.f2="$2"
34.if [ ! -f $f2 ]; then
35. printf " ${BRED}File '$f2' does not exists!$NORMAL\n"
36. exit 1
37.elif [ ! -r $f2 ]; then
38. printf " ${BRED}File '$f2' couldn't be read!$NORMAL\n"
39. exit 2
40.fi
41.printf " ${GREEN}File '$f2' OK ${NORMAL}\n\n"
42.
43.
44.printf "${WHITE}Now we cyclically read and print $YELLOW$FC1 lines from '$f1'${WHITE}
45. then $BLUE$FC2 lines from '$f2'${WHITE} every $GREEN$SEC$WHITE seconds.$NORMAL\n\n"
46.
47.function count(){ echo $(cat $1|wc -l); }
48.
49.function print_lines(){
50. # $1 - file
51. # $2 - lines in file
52. # $3 - current line
53. # $4 - lines to read
54.
55. # if have to read more lines then there are linew from current to end of file
56. let "l = $3 + $4"
57. if [ $l -gt $2 ]; then
58. # 1. print up to the end
59. # lines to end
60. let "lt = $2 - $3"
61. head -$l $1|tail -$lt
62. # 2. print other lines from the beginning of the file
63. # lines to read
64. let "ltr = $4-($2-$3)"
65. print_lines "$1" "$2" "0" "$ltr"
66. else
67. head -$l $1|tail -$4
68. fi
69.}
70.
71.function check_exit(){
72. if [[ $al1 && $al2 ]]; then
73. printf "\n\n${GREEN}All lines from both files are read.\n$NORMAL"
74. exit 0
75. fi
76.}
77.
78.# Current lines in files (that we will read at next operation)
79.cl1=0
80.cl2=0
81.# Number of lines in files
82.l1=$(count "$f1")
83.l2=$(count "$f2")
84.# All lines are read ?
85.let "al1"
86.let "al2"
87.al1=$false
88.al2=$false
89.
90.# infinite cycle
91.# Use Ctrl+C to break execution
92.while [ true ]; do
93. # read lines from first file
94. printf "$YELLOW"
95. print_lines "$f1" "$l1" "$cl1" "$FC1"
96. printf "$NORMAL"
97. let "cl1=(cl1+FC1)"
98. if [ $cl1 -ge $l1 ]; then
99. al1=true
100. fi
101. let "cl1=cl1%l1"
102. check_exit
103.
104. # wait $SEC seconds
105. #sleep $SEC # Uncomment this line if need to print lines from first file, then wait, then print lines from second file
106. # instead of lines from both files together
107.
108. # lines from second file
109. printf "$BLUE"
110. print_lines "$f2" "$l2" "$cl2" "$FC2"
111. printf "$NORMAL"
112. let "cl2=(cl2+FC2)"
113. if [ $cl2 -ge $l2 ]; then
114. al2=true
115. fi
116. let "cl2=cl2%l2"
117. check_exit
118.
119. # wait $SEC seconds
120. sleep $SEC
121.done

Неизвестный
30.05.2012, 13:09
общий
Помогиите, очень надо решить вопрос сегодня...
Форма ответа