Skip to content

Commit fae9aa6

Browse files
committed
Adds reading files scripts and update README.md
1 parent 0b46cca commit fae9aa6

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ A collection of simple bash scripts.
1313
2. [Square a number](scripts/squarenumber.sh): A simple function that squares an integer
1414

1515
## $RANDOM
16-
1. [Guess number](scripts/guess-number-game.sh): User should guess a number from 1 to 100
16+
1. [Guess number](scripts/guess-number-game.sh): User should guess a number from 1 to 100
17+
18+
## Reading files
19+
1. [File reader](scripts/read-line-by-line.sh): The script prints the file content line-by-line
20+
2. [Multiple files reader](scripts/read-files.sh): Prints the content of all files given by arguments

scripts/read-files.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
# Lucas Saito, 2021/04/09
3+
# Usage: bash read-files.sh file1 file2 file3 ...
4+
#
5+
# Given "n" arguments(files), the script reads
6+
# each file's content line-by-line.
7+
8+
9+
if [[ $# -eq 0 ]]; then
10+
echo "[read-lines]: No arguments were given"
11+
exit 0
12+
else
13+
counter=1
14+
for file in $@; do
15+
echo "[read-lines]: Reading file [$counter/$#]: $file"
16+
while read line; do
17+
18+
echo $line
19+
done < $file
20+
counter=$((counter+1))
21+
done
22+
fi
23+
exit 0

scripts/read-line-by-line.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
# Lucas Saito, 2021/04/09
3+
# Given a file by command line argument,
4+
# the script will read its content line-by-line
5+
6+
if [[ $# -eq 0 ]]; then
7+
echo "No file was inputted"
8+
exit 0
9+
else
10+
echo "Reading file: $1"
11+
while read line; do
12+
echo $line
13+
done < $1
14+
fi
15+
exit 0

0 commit comments

Comments
 (0)