-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
18-reading-input.sh
81 lines (62 loc) · 1.94 KB
/
18-reading-input.sh
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
echo "
############################
## Example 18.1: #
## reading into a variable #
############################
"
echo -n "What's your name? (type your name and press enter) "
read -r name
echo "Hello, $name!"
echo "
############################
## Example 18.2: #
## read into multiple variables #
############################
"
echo -n "What's your first & last name? "
read -r first last
echo "first: $first, last: $last"
echo "
###################################
## Example 18.3: #
## reading a comma-separated list #
###################################
"
# if we pipe to `read` then we can read input from a file
# instead of having to type in the values to read
echo 'Ramesh,Kumar,22' | ( # This () syntax is a subshell, from page 23
IFS=, # split input on comma
read -r first last age
echo "$first $last is $age years old"
)
# I don't usually read from files in bash (to me that's in the
# "complicated" category I'd use another language for)
# but I think it's kind of fun that you could use this to read a CSV
echo "
######################################
## Example 18.4: #
## read strips whitespace by default #
######################################
"
echo 'type something with a lot of spaces at the beginning'
read -r something
echo "here's what got read: \"$something\""
echo "
#############################################
## Example 18.5: #
## making a for loop read lines from a file #
#############################################
"
echo 'by default, a for loop will read one word at a time from a file'
for i in $(cat files/lines.txt)
do
echo $i
done
echo "but if you set IFS='', it'll read lines from the file instead"
IFS=''
# the code below is exactly the same as the code above, we
# just changed the value of IFS
for i in $(cat files/lines.txt)
do
echo $i
done