Newer
Older
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Scripting
## Comment
In bash, everything after the `#` special character is a comment and will be skipped.
You can check it by yourself:
```bash
echo hello # how are you? my command ls ; cd
```
When it will come the time to write scripts, you will have to use it extensively to comment your code...
## Variable
A variable is a named storage location that holds a value or information. Think of a variable as a container. The name of the variable allows you to refer to and access the stored value.
In bash any character or string can become a variable storing any value using the = sign as follow:
```bash
v=hello
var=4
echo $v
echo $var
```
!!! question "Try to define a variable `number` and initialize it to 10. Display the value of the variable with `echo`. Change its value to 20 and display it"
??? example "Click to show the solution"
```bash
number=10
echo $number
number=20
echo $number
```
## First script
!!! question "Write a bash script called `count.sh` that allows to count the number of files contained in the directory `training_unix_basics/data`"
??? example "Click to show the solution"
```bash
#!/bin/bash
# directory variable
directory=/home/<user_name>/training_unix_basics/data
# count the number of files
file_count=$(ls -1 "$directory" | wc -l)
# display the result
echo $file_count
```
!!! question "Make the script executable with the `chmod` command and run it"
??? example "Click to show the solution"
```bash
chmod +x count.sh
./count.sh
```
## Read the standard input
`read` is a bash built-in command that reads a line from the standard input (or from the file descriptor) and split the line into words. The first word is assigned to the first variable, the second one to the second variable, and so on.
```bash
read var1 var2
Hello world
echo $var1
echo $var2
```
!!! question "Write a bash script called `count2.sh` that asks for a directory name and count the number of files contained"
??? example "Click to show the solution"
```bash
#!/bin/bash
# Ask the user to enter the name of a directory
echo -n "Enter the path of the directory to analyze : "
read directory
# count the number of files
file_count=$(ls -1 "$directory" | wc -l)
# display the result
echo $file_count
```
## Script with argument
!!! question "Write a bash script called `count3.sh` that takes as an argument a directory name and count the number of files contained, after checking that the directory exists"
??? example "Click to show the solution"
```bash
#!/bin/bash
# directory variable
directory=$1
if [ -d "$directory" ]; then
# count the number of files
file_count=$(ls -1 "$directory" | wc -l)
echo "Directory '$directory' contains $file_count files."
else
echo "Directory '$directory' does not exist."
fi
```
## For loop
!!! question "Write a bash script called `count4.sh` that does the same as previously, and also report the number of characters for each file"
??? example "Click to show the solution"
```bash
#!/bin/bash
# directory variable
directory=$1
if [ -d "$directory" ]; then
# count the number of files
file_count=$(ls -1 "$directory" | wc -l)
echo "Directory '$directory' contains $file_count files."
for file in `ls $directory`
do
if [ -f "$directory/$file" ]; then
number_lines=$(cat "$directory/$file" | wc -c)
echo $file $number_lines
fi
done
else
echo "Directory '$directory' does not exist."
fi
```
## Increment a variable
!!! question "Write a bash script called `count5.sh` that does the same as previously, and also report the total number of characters contained in all files"