-
Notifications
You must be signed in to change notification settings - Fork 5
/
Challenge_10
208 lines (181 loc) · 5.32 KB
/
Challenge_10
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
1. Create a Bash script named script1.sh. Add the shebang.
Define a variable called country that holds the country you are living in.
Print the message: You live in YOUR_COUNTRY.
Sol.
#!/bin/bash
country="Romania"
echo "I live in $country."
2. Create a script and define two string variables.
Add a commented line after the shebang and another inline comment.
Create a new variable by concatenating the two string variables.
Sol.
#!/bin/bash
# This is a very simple Bash script.
var1="Ubuntu "
var2="20.04"
var3="$var1$var2" # concatenating 2 strings
echo $var3
3. Develop a Bash script that creates a new group and a new username. The group will be the user’s secondary group.
The username and the group will be given as input by the user that runs the script.
The script will also display the last 2 lines from the files where the users and the groups are saved in Linux.
Sol.
#!/bin/bash
read -p "Enter the group name to create: " group
read -p "Enter the username to create: " username
groupadd $group
useradd -s /bin/bash -m -G $group $username
tail -n 2 /etc/group
echo
tail -n 2 /etc/passwd
echo
echo "The group $group and the username $username were created!"
4. Create a script called fix_permissions.sh that prompts the user for a directory and changes the permissions of all subdirectories to 755 and to all files to 644 recursively.
Sol.
#!/bin/bash
read -p "Enter directory: " dir
echo -n "Changing files permissions to 644 recursively..."
find $dir -type f -exec chmod 644 {} \;
echo "Done"
echo -n "Changing subdirectories permissions to 755 recursively..."
find $dir -type d -exec chmod 755 {} \;
echo "Done"
5. Create a script that takes an argument and tests if it's a file or not.
Sol.
#!/bin/bash
if [[ -f "$1" ]]
then
echo "The argument is a regular file."
else
echo "The argument is not a regular file."
fi
6. Consider the solution from the previous challenge. Change it so that it tests if the script is run with exactly one argument.
Sol.
#!/bin/bash
if [[ $# -eq 1 ]]
then
if [[ -f "$1" ]]
then
echo "The argument is a regular file."
else
echo "The argument is not a regular file."
fi
else
echo "The script was run with $# arguments. You should run it with exactly one argument."
fi
7. Create a script that clears the contents of a text file which is the script’s argument.
Test that the argument is a file.
Sol.
#!/bin/bash
if [[ -f $1 ]]
then
echo "" > $1
# cat /dev/null > $1
echo "$1 contents was cleared!"
else
echo "The script's argument should be a file!"
fi
8. Develop a script that appends to a file the currently logged in users and the current date and time.
Sol.
#!/bin/bash
users="`who | cut -f1 -d" "`"
date_time="`date +%F\ %H:%M`" #\ to escape the whitespace
echo $date_time >> logged_users.txt
echo "-------------------------" >> logged_users.txt
echo $users >> logged_users.txt
echo >> logged_users.txt
9. Create a script that takes exactly two arguments. The first argument is a shell command and the second argument is a string. The script will check if the string belongs to the
output of the command.
Sol.
#!/bin/bash
if [[ $# -ne 2 ]]
then
echo "Run the script with exactly two arguments."
exit
else
output="`$1`"
if [[ "$output" == *"$2"* ]]
then
echo "$2 belongs to the output of $1"
else
echo "$2 DOES NOT belongs to the output of $1"
fi
fi
10. Create a script that prompts the user for a file. Test that it’s a file.
Prompt the user for 2 positive integers n and m.
Display the lines (from line number n to m, both inclusive) of the text file.
Sol.
#!/bin/bash
read -p "Enter a file: " file
if [[ -f $file ]]
then
read -p "Enter a positive integer n: " n
read -p "Enter a positive integer m: " m
q=$((m-n+1)) # no. of lines to display
tail -n "+$n" $file | head -n $q
else
echo "$file is not a file."
fi
11. Develop a script that prompts the user for a number n.
Add a testing condition so that the user enters a positive integer less than 100
and then it creates a file every 3 seconds and the name of each file will be current_minute_second
Sol.
#!/bin/bash
read -p "Enter the number of files to create:" n
if [[ $n -le 0 ]] || [[ $n -ge 100 ]] # || is the logical OR operator
then
echo "You entered $n. Enter a positive integer less than 100."
exit
fi
i=0
while [[ $i -lt $n ]]
do
minute_second="$(date +%M%S)"
echo "Creating $minute_second"
touch "$minute_second"
((i++))
sleep 3
done
echo "$n files were created!"
12. Create a script that takes 3 arguments: a file extension, a source directory and a destination directory given as absolute or relative paths to the script.
The script will copy all the files with that extension from the source directory to the destination directory.
Sol.
#!/bin/bash
# $1 - file extention (without .)
# $2 - source directory
# $3 - destination directory
for file in $2/*.$1
do
echo "Copying $file to $3"
sleep 1
cp $file $3
done
13. Create a Bash Menu with the following entries:
Display Date and Time
List users logged in
Display disk usage
Change the Menu Prompt to the string Choose:
Sol.
#!/bin/bash
PS3="Choose:"
select MENUITEM in "Display Date and Time" "List users logged in" "Display disk usage" "Quit"
do
case $REPLY in
1)
date
;;
2)
who -aH
;;
3)
df -h
;;
4)
echo "Quitting ..."
sleep 1
exit
;;
*)
echo "Unknown selection."
;;
esac
done