-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek5.4
More file actions
30 lines (26 loc) · 702 Bytes
/
week5.4
File metadata and controls
30 lines (26 loc) · 702 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
4. Write a shell script that, given a file name as theargument, will write the even-numbered line to a file with the name evenfile and odd-numbered lines to a file called oddfile.
if [ $# -eq 0 ]; then
echo "No file name provided as argument."
exit 1
fi
Check if the file exists
if [ ! -f $1 ]; then
echo "File not found."
exit 1
fi
Initialize line counter
count=1
Loop through the file
while read line; do
# Determine if line is even or odd
if [ $((count%2)) -eq 0 ]; then
# Write even line to evenfile
echo $line >> evenfile
else
# Write odd line to oddfile
echo $line >> oddfile
fi
# Increment line counter
count=$((count+1))
done < $1
echo "Lines written to oddfile and evenfile successfully."