Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Folder full of pertinent coursework

1666 views
1
#!/bin/bash
2
# First line of the script is shebang which tells the system how to execute
3
# the script: http://en.wikipedia.org/wiki/Shebang_(Unix)
4
# As you already figured, comments start with #. Shebang is also a comment.
5
6
# Borrowed from: http://learnxinyminutes.com/docs/bash/
7
# Bash is a name of the unix shell, which is also distributed as the shell for the GNU operating system
8
# and as default shell on Linux and Mac OS X. Nearly all examples below can be a part of a shell script or executed directly in the shell.
9
10
# Simple hello world example:
11
echo Hello world!
12
13
# Each command starts on a new line, or after semicolon:
14
echo 'This is the first line'; echo 'This is the second line'
15
16
# Declaring a variable looks like this:
17
Variable="Some string"
18
19
# But not like this:
20
Variable = "Some string"
21
# Bash will decide that Variable is a command it must execute and give an error
22
# because it can't be found.
23
24
# Or like this:
25
Variable= 'Some string'
26
# Bash will decide that 'Some string' is a command it must execute and give an
27
# error because it can't be found. (In this case the 'Variable=' part is seen
28
# as a variable assignment valid only for the scope of the 'Some string'
29
# command.)
30
31
# Using the variable:
32
echo $Variable
33
echo "$Variable"
34
echo '$Variable'
35
# When you use the variable itself — assign it, export it, or else — you write
36
# its name without $. If you want to use variable's value, you should use $.
37
# Note that ' (single quote) won't expand the variables!
38
39
# String substitution in variables
40
echo ${Variable/Some/A}
41
# This will substitute the first occurrence of "Some" with "A"
42
43
# Substring from a variable
44
Length=7
45
echo ${Variable:0:Length}
46
# This will return only the first 7 characters of the value
47
48
# Default value for variable
49
echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}
50
# This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0.
51
# Note that it only returns default value and doesn't change variable value.
52
53
# Builtin variables:
54
# There are some useful builtin variables, like
55
echo "Last program return value: $?"
56
echo "Script's PID: $$"
57
echo "Number of arguments: $#"
58
echo "Scripts arguments: $@"
59
echo "Scripts arguments separated in different variables: $1 $2..."
60
61
# Reading a value from input:
62
echo "What's your name?"
63
read Name # Note that we didn't need to declare a new variable
64
echo Hello, $Name!
65
66
# We have the usual if structure:
67
# use 'man test' for more info about conditionals
68
if [ $Name -ne $USER ]
69
then
70
echo "Your name isn't your username"
71
else
72
echo "Your name is your username"
73
fi
74
75
# There is also conditional execution
76
echo "Always executed" || echo "Only executed if first command fails"
77
echo "Always executed" && echo "Only executed if first command does NOT fail"
78
79
# To use && and || with if statements, you need multiple pairs of square brackets:
80
if [ $Name == "Steve" ] && [ $Age -eq 15 ]
81
then
82
echo "This will run if $Name is Steve AND $Age is 15."
83
fi
84
85
if [ $Name == "Daniya" ] || [ $Name == "Zach" ]
86
then
87
echo "This will run if $Name is Daniya OR Zach."
88
fi
89
90
# Expressions are denoted with the following format:
91
echo $(( 10 + 5 ))
92
93
# Unlike other programming languages, bash is a shell — so it works in a context
94
# of current directory. You can list files and directories in the current
95
# directory with the ls command:
96
ls
97
98
# These commands have options that control their execution:
99
ls -l # Lists every file and directory on a separate line
100
101
# Results of the previous command can be passed to the next command as input.
102
# grep command filters the input with provided patterns. That's how we can list
103
# .txt files in the current directory:
104
ls -l | grep "\.txt"
105
106
# You can redirect command input and output (stdin, stdout, and stderr).
107
# Read from stdin until ^EOF$ and overwrite hello.py with the lines
108
# between "EOF":
109
cat > hello.py << EOF
110
#!/usr/bin/env python
111
from __future__ import print_function
112
import sys
113
print("#stdout", file=sys.stdout)
114
print("#stderr", file=sys.stderr)
115
for line in sys.stdin:
116
print(line, file=sys.stdout)
117
EOF
118
119
# Run hello.py with various stdin, stdout, and stderr redirections:
120
python hello.py < "input.in"
121
python hello.py > "output.out"
122
python hello.py 2> "error.err"
123
python hello.py > "output-and-error.log" 2>&1
124
python hello.py > /dev/null 2>&1
125
# The output error will overwrite the file if it exists,
126
# if you want to append instead, use ">>":
127
python hello.py >> "output.out" 2>> "error.err"
128
129
# Overwrite output.out, append to error.err, and count lines:
130
info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err
131
wc -l output.out error.err
132
133
# Run a command and print its file descriptor (e.g. /dev/fd/123)
134
# see: man fd
135
echo <(echo "#helloworld")
136
137
# Overwrite output.out with "#helloworld":
138
cat > output.out <(echo "#helloworld")
139
echo "#helloworld" > output.out
140
echo "#helloworld" | cat > output.out
141
echo "#helloworld" | tee output.out >/dev/null
142
143
# Cleanup temporary files verbosely (add '-i' for interactive)
144
rm -v output.out error.err output-and-error.log
145
146
# Commands can be substituted within other commands using $( ):
147
# The following command displays the number of files and directories in the
148
# current directory.
149
echo "There are $(ls | wc -l) items here."
150
151
# The same can be done using backticks `` but they can't be nested - the preferred way
152
# is to use $( ).
153
echo "There are `ls | wc -l` items here."
154
155
# Bash uses a case statement that works similarly to switch in Java and C++:
156
case "$Variable" in
157
#List patterns for the conditions you want to meet
158
0) echo "There is a zero.";;
159
1) echo "There is a one.";;
160
*) echo "It is not null.";;
161
esac
162
163
# for loops iterate for as many arguments given:
164
# The contents of $Variable is printed three times.
165
for Variable in {1..3}
166
do
167
echo "$Variable"
168
done
169
170
# Or write it the "traditional for loop" way:
171
for ((a=1; a <= 3; a++))
172
do
173
echo $a
174
done
175
176
# They can also be used to act on files..
177
# This will run the command 'cat' on file1 and file2
178
for Variable in file1 file2
179
do
180
cat "$Variable"
181
done
182
183
# ..or the output from a command
184
# This will cat the output from ls.
185
for Output in $(ls)
186
do
187
cat "$Output"
188
done
189
190
# while loop:
191
while [ true ]
192
do
193
echo "loop body here..."
194
break
195
done
196
197
# You can also define functions
198
# Definition:
199
function foo ()
200
{
201
echo "Arguments work just like script arguments: $@"
202
echo "And: $1 $2..."
203
echo "This is a function"
204
return 0
205
}
206
207
# or simply
208
bar ()
209
{
210
echo "Another way to declare functions!"
211
return 0
212
}
213
214
# Calling your function
215
foo "My name is" $Name
216
217
# There are a lot of useful commands you should learn:
218
# prints last 10 lines of file.txt
219
tail -n 10 file.txt
220
# prints first 10 lines of file.txt
221
head -n 10 file.txt
222
# sort file.txt's lines
223
sort file.txt
224
# report or omit repeated lines, with -d it reports them
225
uniq -d file.txt
226
# prints only the first column before the ',' character
227
cut -d ',' -f 1 file.txt
228
# replaces every occurrence of 'okay' with 'great' in file.txt, (regex compatible)
229
sed -i 's/okay/great/g' file.txt
230
# print to stdout all lines of file.txt which match some regex
231
# The example prints lines which begin with "foo" and end in "bar"
232
grep "^foo.*bar$" file.txt
233
# pass the option "-c" to instead print the number of lines matching the regex
234
grep -c "^foo.*bar$" file.txt
235
# if you literally want to search for the string,
236
# and not the regex, use fgrep (or grep -F)
237
fgrep "^foo.*bar$" file.txt
238
239
240
# Read Bash shell builtins documentation with the bash 'help' builtin:
241
help
242
help help
243
help for
244
help return
245
help source
246
help .
247
248
# Read Bash manpage documentation with man
249
apropos bash
250
man 1 bash
251
man bash
252
253
# Read info documentation with info (? for help)
254
apropos info | grep '^info.*('
255
man info
256
info info
257
info 5 info
258
259
# Read bash info documentation:
260
info bash
261
info bash 'Bash Features'
262
info bash 6
263
info --apropos bash
264
265