Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
153 commits
Select commit Hold shift + click to select a range
1ebc363
initial commit
EwDa291 Aug 8, 2024
34df842
Merge branch 'hpcugent:main' into chatbot_parser
EwDa291 Aug 8, 2024
10edb20
some cleanup
EwDa291 Aug 8, 2024
85a93ec
used jinja to replace macros
EwDa291 Aug 9, 2024
dfff5fa
adapt if-mangler to accommodate for nested if-clauses
EwDa291 Aug 9, 2024
649ddec
adapt the parser to take all files as input, not all files get parsed…
EwDa291 Aug 9, 2024
2116d6e
adapt the parser to take all files as input, not all files get parsed…
EwDa291 Aug 9, 2024
159aa62
small update, not important
EwDa291 Aug 9, 2024
75765e5
change to the templates
EwDa291 Aug 9, 2024
57d9cfe
change to accommodate for more nested if-clauses
EwDa291 Aug 9, 2024
75d345b
Delete scripts/HPC chatbot preprocessor/start_checker.py
EwDa291 Aug 9, 2024
ff7a9fc
make sure files with duplicate names between normal files and linux-t…
EwDa291 Aug 12, 2024
47a33b7
Merge branch 'chatbot_parser' of https://github.com/EwDa291/vsc_user_…
EwDa291 Aug 12, 2024
7d279d6
fixed the problem of some files being written in reST instead of mark…
EwDa291 Aug 12, 2024
8047572
some small fixes
EwDa291 Aug 12, 2024
7d1c5ed
remove try-except-structure
EwDa291 Aug 13, 2024
984b0cd
collapse all code into one file
EwDa291 Aug 13, 2024
8f5eeaa
Rename file
EwDa291 Aug 13, 2024
2b97b7a
cleanup repository
EwDa291 Aug 13, 2024
b595301
Rename directory
EwDa291 Aug 13, 2024
90c8ab7
add a main function
EwDa291 Aug 13, 2024
b8ae706
make file paths non os-specific
EwDa291 Aug 13, 2024
b751497
use docstrings to document the functions
EwDa291 Aug 13, 2024
0f8eb5d
rewrite the if-mangler to make it more readable
EwDa291 Aug 13, 2024
9938e92
got rid of most global variables
EwDa291 Aug 13, 2024
508b22c
fixed some issues with if statements
EwDa291 Aug 13, 2024
a25ce2d
fixed some issues with if statements
EwDa291 Aug 13, 2024
80d0535
got rid of all global variables
EwDa291 Aug 13, 2024
9163a75
small changes to make file more readable
EwDa291 Aug 14, 2024
1dcffc1
codeblocks, tips, warnings and info reformatted
EwDa291 Aug 14, 2024
4d7fbdb
small optimisations
EwDa291 Aug 14, 2024
671f7f3
small optimisations
EwDa291 Aug 14, 2024
e5c39bd
initial commit
EwDa291 Aug 14, 2024
c6492fc
added requirements
EwDa291 Aug 14, 2024
aff8198
added requirements and usage info
EwDa291 Aug 14, 2024
a981002
minor changes to the print statements
EwDa291 Aug 14, 2024
1f3b343
reworked function to take care of html structures
EwDa291 Aug 16, 2024
b6388d3
Merge branch 'hpcugent:main' into chatbot_parser
EwDa291 Aug 16, 2024
48cad97
filter out images
EwDa291 Aug 16, 2024
df58f23
get rid of backquotes, asterisks, pluses and underscores used for for…
EwDa291 Aug 16, 2024
c423e07
dump to json files instead of txt files
EwDa291 Aug 16, 2024
2c333fe
cleaned up parser with macros
EwDa291 Aug 16, 2024
ce52352
cleaned up parser with macros
EwDa291 Aug 16, 2024
5db34af
cleaned up parser with macros
EwDa291 Aug 16, 2024
4226d28
Update README.md
EwDa291 Aug 19, 2024
d730a26
Update README.md
EwDa291 Aug 19, 2024
f3182e3
added section about restrictions on input files
EwDa291 Aug 19, 2024
aee54de
Merge branch 'hpcugent:main' into chatbot_parser
EwDa291 Aug 19, 2024
675bec5
adapted section about restrictions on input files
EwDa291 Aug 19, 2024
f1e58ef
adapted section about restrictions on input files
EwDa291 Aug 19, 2024
2bf1075
Merge branch 'chatbot_parser' of https://github.com/EwDa291/vsc_user_…
EwDa291 Aug 19, 2024
a168509
change variables to be lowercase
EwDa291 Aug 19, 2024
09b86c9
take out some copy pasting
EwDa291 Aug 19, 2024
f95b99e
added warning about long filepaths
EwDa291 Aug 19, 2024
06bb7b9
fixing typos
EwDa291 Aug 19, 2024
2f3e5b3
take out copy pasting
EwDa291 Aug 19, 2024
0c4dbe8
first draft version of the restructured script to accommodate for the…
EwDa291 Aug 20, 2024
38c4572
added support to filter out collapsable admonitions
EwDa291 Aug 20, 2024
5cbd653
attempt at fix for problems with jinja include, not working yet
EwDa291 Aug 20, 2024
0e6f8b2
fixed an issue with jinja templates
EwDa291 Aug 21, 2024
cd77837
added docstrings to new functions
EwDa291 Aug 21, 2024
98eb695
only add necessary if-statements in front of non-if-complete sections
EwDa291 Aug 21, 2024
27457e3
fixed some more jinja problems
EwDa291 Aug 21, 2024
bb72287
implemented extra test to make sure generic files dont accidentally g…
EwDa291 Aug 21, 2024
67cb19e
make sure empty os-specific files are not saved
EwDa291 Aug 21, 2024
cf9834a
clean up unused code
EwDa291 Aug 21, 2024
da32459
introduce more macros
EwDa291 Aug 21, 2024
093200b
reintroduce logic to remove unnecessary directories
EwDa291 Aug 21, 2024
5d0ffe9
added functionality to include links or leave them out
EwDa291 Aug 21, 2024
a3e34a9
added functionality to include links or leave them out
EwDa291 Aug 21, 2024
7c6154b
adapt filenames to allow for splitting on something other than subtitles
EwDa291 Aug 21, 2024
8d5b50d
making some changes to prepare to add paragraph level splitting tomorrow
EwDa291 Aug 21, 2024
0c10376
making some changes to prepare to add paragraph level splitting tomorrow
EwDa291 Aug 21, 2024
f8ee860
making some changes to prepare to add paragraph level splitting tomorrow
EwDa291 Aug 21, 2024
6533733
adapted the parsing script to allow for testing in a semi-efficient way
EwDa291 Aug 21, 2024
2e7a00f
added test for make_valid_title
EwDa291 Aug 21, 2024
f5e0579
removed useless lines from testscript
EwDa291 Aug 21, 2024
6757b4f
First attempt at splitting in paragraphs (need for other fixes for ti…
EwDa291 Aug 22, 2024
6d9558d
make two functions for different ways of dividing the text
EwDa291 Aug 22, 2024
2c7025a
added docstrings to new functions
EwDa291 Aug 22, 2024
ae99bb9
update test for valid titles
EwDa291 Aug 22, 2024
084b421
fixed problem with splitting os-specific text (metadata not fixed yet)
EwDa291 Aug 22, 2024
cf7f5f0
fix for metadata of os-specific sections
EwDa291 Aug 22, 2024
b7c10d3
clean up temporary version
EwDa291 Aug 22, 2024
4a441f3
added command line options for custom macros
EwDa291 Aug 22, 2024
662134f
small fix to macros
EwDa291 Aug 22, 2024
05eab4a
clean up test for valid title
EwDa291 Aug 22, 2024
b85a8fb
add a test for write_metadata
EwDa291 Aug 22, 2024
39a3c99
added functionality to split on paragraphs
EwDa291 Aug 23, 2024
af9e6cc
clean up
EwDa291 Aug 23, 2024
f4163a7
clean up
EwDa291 Aug 23, 2024
833f964
further clean up and added shebang
EwDa291 Aug 23, 2024
79b1a56
clean up
EwDa291 Aug 23, 2024
cec154c
added test for if mangler
EwDa291 Aug 23, 2024
2f4a277
clean up
EwDa291 Aug 23, 2024
cd0c8eb
clean up customizable options
EwDa291 Aug 23, 2024
3be262a
further adapt the script to be able to test it
EwDa291 Aug 26, 2024
1d32aab
make changes to usage in command line to be more intuitive
EwDa291 Aug 26, 2024
5902c96
first revised version of the README
EwDa291 Aug 26, 2024
6f97d5f
Merge branch 'hpcugent:main' into chatbot_parser
EwDa291 Aug 26, 2024
6e48800
added docstring to main function
EwDa291 Aug 26, 2024
0bc440b
include chatbot_prepprocessor
EwDa291 Aug 26, 2024
e6e6023
added options for source and destination directories
EwDa291 Aug 26, 2024
a6d99d9
cleanup
EwDa291 Aug 26, 2024
2be834f
cleanup
EwDa291 Aug 26, 2024
532543a
cleanup
EwDa291 Aug 26, 2024
107464e
relocate test files
EwDa291 Aug 26, 2024
dd64381
update arguments of if mangler
EwDa291 Aug 26, 2024
ef3fd58
relocate full test files
EwDa291 Aug 26, 2024
4d7db8f
Revert "update arguments of if mangler"
EwDa291 Aug 26, 2024
df9bac5
Revert "relocate full test files"
EwDa291 Aug 26, 2024
631d9e9
update test to adapt to new arguments in if mangler
EwDa291 Aug 26, 2024
c6e600d
relocated full test files
EwDa291 Aug 26, 2024
d1c6194
Rename test_paragraph_split_1.md to test_paragraph_split_1_input.md
EwDa291 Aug 26, 2024
695ffd6
Rename test_title_split_1.md to test_title_split_1_input.md
EwDa291 Aug 26, 2024
af4832b
smal fix
EwDa291 Aug 26, 2024
8805c8c
test text for paragraph split
EwDa291 Aug 26, 2024
a265ffd
start of a fix for double title problem, not done yet
EwDa291 Aug 26, 2024
6c2a61c
Fix for double title bug when splitting on paragraph
EwDa291 Aug 27, 2024
ed08879
Fix bug for empty linklist in metadata
EwDa291 Aug 27, 2024
176af13
fix bug where too many directories were sometimes created
EwDa291 Aug 27, 2024
d4ceac8
test of full script, test files not ready to be pushed yet
EwDa291 Aug 27, 2024
815a863
updated requirements.txt
EwDa291 Aug 27, 2024
d15469f
updated docstring in main function
EwDa291 Aug 27, 2024
daa6b36
add support for comments for the bot to be included in the source files
EwDa291 Aug 27, 2024
4c19f44
changed the default for min paragraph length
EwDa291 Aug 27, 2024
9a6ff58
added test files for full script test
EwDa291 Aug 27, 2024
56543f0
small fix for double title bug
EwDa291 Aug 27, 2024
52a3861
added examples of output of the script when splitting on paragraphs w…
EwDa291 Aug 27, 2024
692e77b
fix for issue with html links
EwDa291 Aug 27, 2024
7f493a1
fix for issue with html links
EwDa291 Aug 27, 2024
0e34396
fix for issue with relative links to the same document
EwDa291 Aug 27, 2024
fa00044
added test for replace_markdown_markers
EwDa291 Aug 27, 2024
b3952b2
fix to small inconsistency in metadata
EwDa291 Aug 27, 2024
73072bf
added test for insert_links
EwDa291 Aug 27, 2024
3161309
make sure paragraphs only include full lists
EwDa291 Aug 28, 2024
7d4d7f9
Merge branch 'hpcugent:main' into chatbot_parser
EwDa291 Aug 28, 2024
3407be3
adapted to the new source files
EwDa291 Aug 28, 2024
6d04bbc
add source-directory to metadata and verbose mode
EwDa291 Aug 28, 2024
f33cfb3
added verbose mode
EwDa291 Aug 28, 2024
1c389d7
Merge branch 'hpcugent:main' into chatbot_parser
EwDa291 Aug 28, 2024
3227f19
Added limitation on lists
EwDa291 Aug 29, 2024
67aed53
fix for non os-specific if-statement not being recognised
EwDa291 Aug 29, 2024
9e297b1
new test for links
EwDa291 Aug 29, 2024
b6b8610
new test to make sure lists are kept as one section
EwDa291 Aug 29, 2024
57a2139
updated test_file for list test
EwDa291 Aug 29, 2024
170a10c
dropped <> around links and started new function to calculate length …
EwDa291 Aug 30, 2024
04efff6
removed parsed mds
EwDa291 Aug 30, 2024
1ef1f10
Changed paragraphs to decide length based on tokens instead of charac…
EwDa291 Aug 30, 2024
621c0a3
Changed paragraphs to decide length based on tokens instead of charac…
EwDa291 Aug 30, 2024
adf364d
Changed paragraphs to decide length based on tokens instead of charac…
EwDa291 Aug 30, 2024
e36482e
Added output of chatbot parser script (linux tutorial)
EwDa291 Aug 30, 2024
8f54b86
removing unnecessary files
EwDa291 Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Beyond the basics
Now that you've seen some of the more basic commands, let's take a look
at some of the deeper concepts and commands.
Input/output
To redirect output to files, you can use the redirection operators: >,
>>, &>, and <.
First, it's important to make a distinction between two different output
channels:
1. stdout: standard output channel, for regular output
2. stderr: standard error channel, for errors and warnings
Redirecting stdout
> writes the (stdout) output of a command to a file and overwrites
whatever was in the file before.
$ echo hello > somefile
$ cat somefile
hello
$ echo hello2 > somefile
$ cat somefile
hello2
>> appends the (stdout) output of a command to a file; it does not
clobber whatever was in the file before:
$ echo hello > somefile
$ cat somefile
hello
$ echo hello2 >> somefile
$ cat somefile
hello
hello2
Reading from stdin
< reads a file from standard input (piped or typed input). So you
would use this to simulate typing into a terminal. < somefile.txt is
largely equivalent to cat somefile.txt | .
One common use might be to take the results of a long-running
command and store the results in a file, so you don't have to repeat it
while you refine your command line. For example, if you have a large
directory structure you might save a list of all the files you're
interested in and then reading in the file list when you are done:
$ find . -name .txt > files
$ xargs grep banana < files
Redirecting stderr
To redirect the stderr output (warnings, messages), you can use 2>,
just like >
$ ls one.txt nosuchfile.txt 2> errors.txt
one.txt
$ cat errors.txt
ls: nosuchfile.txt: No such file or directory
Combining stdout and stderr
To combine both output channels (stdout and stderr) and redirect
them to a single file, you can use &>
$ ls one.txt nosuchfile.txt &> ls.out
$ cat ls.out
ls: nosuchfile.txt: No such file or directory
one.txt
Command piping
Part of the power of the command line is to string multiple commands
together to create useful results. The core of these is the pipe: |.
For example, to see the number of files in a directory, we can pipe the
(stdout) output of ls to wc (word count, but can also be used to
count the number of lines with the -l flag).
$ ls | wc -l
42
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"main_title": "beyond_the_basics",
"subtitle": "Command-piping",
"source_file": "../../mkdocs/docs/HPC/linux-tutorial/beyond_the_basics.md",
"title_depth": 2,
"directory": "beyond_the_basics",
"parent_title": "",
"previous_title": null,
"next_title": "beyond_the_basics_paragraph_2",
"OS": "generic",
"reference_link": "https://docs.hpc.ugent.be/linux-tutorial/beyond_the_basics/#command-piping"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
A common pattern is to pipe the output of a command to less so you
can examine or search the output:
$ find . | less
Or to look through your command history:
$ history | less
You can put multiple pipes in the same line. For example, which cp
commands have we run?
$ history | grep cp | less
Shell expansion
The shell will expand certain things, including:
1. wildcard: for example ls ttxt will list all files starting
with 't' and ending in 'txt'
2. tab completion: hit the <tab> key to make the shell complete your
command line; works for completing file names, command names, etc.
3. $... or ${...}: environment variables will be replaced with
their value; example: echo "I am $USER" or echo "I am ${USER}"
4. square brackets can be used to list a number of options for a
particular characters;
example: ls *.[oe][0-9].
This will list all
files starting with whatever characters (*), then a dot (.),
then either an 'o' or an 'e' ([oe]), then a character from '0' to
'9' (so any digit) ([0-9]). So this filename will match:
anything.o5, but this one won't: anything.o52.
Process information
ps and pstree
ps lists processes running. By default, it will only show you the
processes running in the local shell. To see all of your processes
running on the system, use:
$ ps -fu $USER
To see all the processes:
$ ps -elf
To see all the processes in a forest view, use:
$ ps auxf
The last two will spit out a lot of data, so get in the habit of piping
it to less.
pstree is another way to dump a tree/forest view. It looks better than
ps auxf but it has much less information so its value is limited.
pgrep will find all the processes where the name matches the pattern
and print the process IDs (PID). This is used in piping the processes
together as we will see in the next section.
kill
ps isn't very useful unless you can manipulate the processes. We do
this using the kill command. Kill will send a message
(SIGINT) to
the process to ask it to stop.
$ kill 1234
$ kill $(pgrep misbehaving_process)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"main_title": "beyond_the_basics",
"subtitle": "`kill`",
"source_file": "../../mkdocs/docs/HPC/linux-tutorial/beyond_the_basics.md",
"title_depth": 3,
"directory": "beyond_the_basics",
"links": {
"0": "https://en.wikipedia.org/wiki/Unix_signal#POSIX_signals"
},
"parent_title": "",
"previous_title": "beyond_the_basics_paragraph_1",
"next_title": "beyond_the_basics_paragraph_3",
"OS": "generic",
"reference_link": "https://docs.hpc.ugent.be/linux-tutorial/beyond_the_basics/#kill"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Usually, this ends the process, giving it the opportunity to flush data
to files, etc. However, if the process ignored your signal, you can send
it a different message
(SIGKILL)
which the OS will use to unceremoniously terminate the process:
$ kill -9 1234
top
top is a tool to see the current status of the system. You've probably
used something similar in Task Manager on Windows or Activity Monitor in
macOS. top will update every second and has a few interesting
commands.
To see only your processes, type u and your username after starting
top, (you can also do this with
top -u $USER
). The default is to
sort the display by %CPU. To change the sort order, use < and >
like arrow keys.
There are a lot of configuration options in top, but if you're
interested in seeing a nicer view, you can run htop instead. Be aware
that it's not installed everywhere, while top is.
To exit top, use q (for 'quit').
For more information, see [Brendan Gregg's excellent site dedicated to
performance analysis](http://brendangregg.com).
ulimit
ulimit is a utility to get or set user limits on the machine. For
example, you may be limited to a certain number of processes. To see all
the limits that have been set, use:
$ ulimit -a
Counting: wc
To count the number of lines, words, and characters (or bytes) in a file, use wc (word count):
$ wc example.txt
90 468 3189 example.txt
The output indicates that the file named example.txt contains 90
lines, 468 words, and 3189 characters/bytes.
To only count the number of lines, use wc -l:
$ wc -l example.txt
90 example.txt
Searching file contents: grep
grep is an important command. It was originally an abbreviation for
"globally search a regular expression and print" but it's entered the
common computing lexicon and people use 'grep' to mean searching for
anything. To use grep, you give a pattern and a list of files.
$ grep banana fruit.txt
$ grep banana fruit_bowl1.txt fruit_bowl2.txt
$ grep banana fruit*txt
grep also lets you search for [Regular
Expressions](https://en.wikipedia.org/wiki/Regular_expression), but
these are not in scope for this introductory text.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"main_title": "beyond_the_basics",
"subtitle": "Searching-file-contents-`grep`",
"source_file": "../../mkdocs/docs/HPC/linux-tutorial/beyond_the_basics.md",
"title_depth": 2,
"directory": "beyond_the_basics",
"links": {
"0": "https://en.wikipedia.org/wiki/Unix_signal#POSIX_signals"
},
"parent_title": "",
"previous_title": "beyond_the_basics_paragraph_2",
"next_title": "beyond_the_basics_paragraph_4",
"OS": "generic",
"reference_link": "https://docs.hpc.ugent.be/linux-tutorial/beyond_the_basics/#searching-file-contents-grep"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
cut
cut is used to pull fields out of files or pipes streams. It's a
useful glue when you mix it with grep because grep can find the
lines where a string occurs and cut can pull out a particular field.
For example, to pull the first column (-f 1, the first field) from (an
unquoted) CSV (comma-separated values, so -d ',': delimited by ,)
file, you can use the following:
$ cut -f 1 -d ',' mydata.csv
sed
sed is the stream editor. It is used to replace text in a file or
piped stream. In this way, it works like grep, but instead of just
searching, it can also edit files. This is like "Search and Replace" in
a text editor. sed has a lot of features, but almost everyone uses the
extremely basic version of string replacement:
$ sed 's/oldtext/newtext/g' myfile.txt
By default, sed will just print the results. If you want to edit the
file inplace, use -i, but be very careful that the results will be
what you want before you go around destroying your data!
awk
awk is a basic language that builds on sed to do much more advanced
stream editing. Going in depth is far out of scope of this tutorial, but
there are two examples that are worth knowing.
First, cut is very limited in pulling fields apart based on
whitespace. For example, if you have padded fields then
cut -f 4 -d ' ' will almost certainly give you a headache as there
might be an uncertain number of spaces between each field. awk does
better whitespace splitting. So, pulling out the fourth field in a
whitespace delimited file is as follows:
$ awk '{print $4}' mydata.dat
You can use -F ':' to change the delimiter (F for field separator).
The next example is used to sum numbers from a field:
$ awk -F ',' '{sum += $1} END {print sum}' mydata.csv
Basic Shell Scripting
The basic premise of a script is to execute automate the execution of
multiple commands. If you find yourself repeating the same commands over
and over again, you should consider writing one script to do the same. A
script is nothing special, it is just a text file like any other. Any
commands you put in there will be executed from the top to bottom.
However, there are some rules you need to abide by.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"main_title": "beyond_the_basics",
"subtitle": "Basic-Shell-Scripting",
"source_file": "../../mkdocs/docs/HPC/linux-tutorial/beyond_the_basics.md",
"title_depth": 2,
"directory": "beyond_the_basics",
"parent_title": "",
"previous_title": "beyond_the_basics_paragraph_3",
"next_title": "beyond_the_basics_paragraph_5",
"OS": "generic",
"reference_link": "https://docs.hpc.ugent.be/linux-tutorial/beyond_the_basics/#basic-shell-scripting"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Here is a very detailed guide should you
need more information.
Shebang
The first line of the script is the so-called shebang (# is sometimes
called hash and ! is sometimes called bang). This line tells the shell
which command should execute the script. In most cases, this will
simply be the shell itself. The line itself looks a bit weird, but you
can copy-paste this line as you need not worry about it further. It is
however very important this is the very first line of the script! These
are all valid shebangs, but you should only use one of them:
#!/bin/sh
#!/bin/bash
#!/usr/bin/env bash
Conditionals
Sometimes you only want certain commands to be executed when a certain
condition is met. For example, only move files to a directory if that
directory exists. The syntax:
if [ -d directory ] && [ -f file ]
then
mv file directory
fi
Or you only want to do something if a file exists:
if [ -f filename ]
then
echo "it exists"
fi
Or only if a certain variable is bigger than one:
if [ $AMOUNT -gt 1 ]
then
echo "More than one"
# more commands
fi
Several pitfalls exist with this syntax. You need spaces surrounding the
brackets, the then needs to be at the beginning of a line. It is best to just
copy this example and modify it.
In the initial example, we used -d to test if a directory existed.
There are several more checks.
Another useful example, is to test if a variable contains a value (so it's
not empty):
if [ -z $PBS_ARRAYID ]
then
echo "Not an array job, quitting."
exit 1
fi
the -z will check if the length of the variable's value is greater than zero.
Loops
Are you copy-pasting commands? Are you doing the same thing with just different
options? You most likely can simplify your script by using a loop.
Let's look at a simple example:
for i in 1 2 3
do
echo $i
done
Subcommands
Subcommands are used all the time in shell scripts. What they
do is storing the output of a command in a variable. So this can later
be used in a conditional or a loop for example.
CURRENTDIR=`pwd` # using backticks
CURRENTDIR=$(pwd) # recommended (easier to type)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"main_title": "beyond_the_basics",
"subtitle": "Subcommands",
"source_file": "../../mkdocs/docs/HPC/linux-tutorial/beyond_the_basics.md",
"title_depth": 3,
"directory": "beyond_the_basics",
"links": {
"0": "http://www.tldp.org/LDP/Bash-Beginners-Guide/html/",
"1": "http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html"
},
"parent_title": "",
"previous_title": "beyond_the_basics_paragraph_4",
"next_title": "beyond_the_basics_paragraph_6",
"OS": "generic",
"reference_link": "https://docs.hpc.ugent.be/linux-tutorial/beyond_the_basics/#subcommands"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
In the above example you can see the 2 different methods of using a
subcommand. pwd will output the current working directory, and its
output will be stored in the CURRENTDIR variable. The recommended way to
use subcommands is with the $() syntax.
Errors
Sometimes some things go wrong and a command or script you ran causes an
error. How do you properly deal with these situations?
Firstly a useful thing to know for debugging and testing is that you can
run any command like this:
command 2>&1 output.log # one single output file, both output and errors
If you add 2>&1 output.log at the end of any command, it will combine
stdout and stderr, outputting it into a single file named
output.log.
If you want regular and error output separated you can use:
command > output.log 2> output.err # errors in a separate file
this will write regular output to output.log and error output to
output.err.
You can then look for the errors with less or search for specific text
with grep.
In scripts, you can use:
set -e
This will tell the shell to stop executing any subsequent commands when
a single command in the script fails. This is most convenient as most
likely this causes the rest of the script to fail as well.
Advanced error checking
Sometimes you want to control all the error checking yourself, this is
also possible. Everytime you run a command, a special variable $? is
used to denote successful completion of the command. A value other than
zero signifies something went wrong. So an example use case:
command_with_possible_error
exit_code=$? # capture exit code of last command
if [ $exit_code -ne 0 ]
then
echo "something went wrong"
fi
.bashrc login script
[//]: # (sec:bashrc-login-script)
If you have certain commands executed every time you log in (which
includes every time a job starts), you can add them to your
$HOME/.bashrc file. This file is a shell script that gets executed
every time you log in.
Examples include:
- modifying your $PS1 (to tweak your shell prompt)
- printing information about the current/jobs environment (echoing
environment variables, etc.)
- selecting a specific cluster to run on with
module swap cluster/...
Some recommendations:
- Avoid using module load statements in your $HOME/.bashrc file
- Don't directly edit your .bashrc file: if there's an error in your
.bashrc file, you might not be able to log in again. To
prevent that, use another file to test your changes, then copy them
over when you tested the script.
Loading
Loading