1
0
mirror of https://github.com/danog/learn-bash.git synced 2024-11-26 20:04:39 +01:00
learn-bash/learn.sh

257 lines
6.2 KiB
Bash
Raw Normal View History

2015-05-29 17:47:35 +02:00
#!/bin/bash
# Learn shell scripting script.
# Created by Daniil Gentili.
# This project is licensed under the GPLv3 license.
# Thanks to The Linux Documentation Project and to everyone @ #openshells @freenode.
2015-05-29 17:47:35 +02:00
# 1.0
2015-05-29 18:01:17 +02:00
2015-05-30 18:13:58 +02:00
# First checks.
[ -f /bin/bash ] && bash="/bin/bash" || bash="$(which -a bash | tail -1)"
2015-05-30 18:31:11 +02:00
2015-05-29 18:27:59 +02:00
which wget &>/dev/null
if [ "$?" = 0 ];then dl() {
wget $1 -O $2 $3
}
Q="-q"
else dl() {
curl $1 -o $2 $3
}
Q="-s"
fi
2015-05-30 18:13:58 +02:00
echo -n "Self-updating script..." && dl http://daniilgentili.magix.net/learn.sh $0 $Q 2>/dev/null;chmod +x $0 &>/dev/null; echo -en "\r\033[K"
2015-05-29 18:01:17 +02:00
2015-05-29 17:47:35 +02:00
clear
press() {
echo
read -s -p "Press enter to continue: "; echo -en "\r\033[K"
}
r() {
2015-05-30 18:36:50 +02:00
eval $current
2015-05-29 17:47:35 +02:00
}
s() {
try=y
exit
}
export -f s r
2015-05-29 17:47:35 +02:00
learn_vi() {
echo "Vi:
For now I will just show you the basics of vi.
To create/open a text file using vi you have to type the following command:
vi name_of_the_file_you_want_to_create
"
press
echo "Vi has 2 modes: command mode and text mode."
press
echo "In the text mode you can edit the text of
any text file.
In the command mode you can do things like
copy, paste, undo,
save and close
the file you are editing.
Since vi starts automatically
in command mode,
to switch to to text mode
you will have to type:
i
When you open/create a file."
press
echo "To save the file press ESC to switch to command mode and then type
:wq
To save the file and close vi or
:w
Only to save the file without exiting vi.
"
}
learn_nano() {
echo "Nano:
For now I will just show you the basics of nano.
To create and open a text file using nano you have to use
the following command:
nano name_of_the_file_you_want_to_create
"
press
echo "Once you have written what you need, press CTRL+X and y to save and close the file or just CTRL+O only to save the file."
}
learn_2_lesson() {
which vi &>/dev/null && vi="available" || vi="unavailable "
which nano &>/dev/null && nano="available " || nano="unavailable"
echo "First of all, the script creation.
A script should have an sh extension (Note 1.) and no spaces in the filename.
"
press
echo "Note 1: Actually any extension will do,
but it's more recognizable this way.
"
press
echo "Sublesson 1: She-bang and permissions.
2015-05-29 17:47:35 +02:00
Every bash script can be executed
simply by typing
bash script.sh
but to execute it with a simple
./script.sh
you then have to give execution permissions
to the script with chmod:
chmod +x script.sh
means: add (+) execute (x) permissions to script.sh
"
press
echo "and add a shebang at the beginning of the file.
The she-bang is a character (#!) followed by the
path of the bash binary,
usually /bin/bash (in this case matches the
current location of the binary,
2015-05-30 18:13:58 +02:00
which is $bash).
2015-05-29 17:47:35 +02:00
She-bang to use on this system:
2015-05-30 18:13:58 +02:00
#!"$bash"
2015-05-29 17:47:35 +02:00
"
press
echo "To create a file, we can use lots of editors,
but I have provided
instructions only for the commonest
vi and nano: they should be available
on most Linux systems.
You can also use echo to create scripts.
"
press
until [ "$editor" = "c" -o "$editor" = "3" ]; do
clear
echo -n "Sublesson 2: Editors.
1. Nano (currently $nano on your system)
2. Vi (currently $vi on your system)
3. Echo (currently available on your system)
Your selection (number, c to continue): "
read editor
clear
[ "$editor" = "1" ] && learn_nano
[ "$editor" = "2" ] && learn_vi
done
set +H
2015-05-29 17:47:35 +02:00
echo "Sublesson 3: echo.
Echo is one of the basic tools that are most commonly used in bash scripting: it prints out a message to the user. This very message is printed using echo.
The usage of this command is very simple:
echo Words to print.
"
press
2015-05-29 17:47:35 +02:00
echo "If your phrase include newlines or special characters such as ', ;, :, &, !, and so on, you should enclose everything in quotes, like this:
echo \"This is something,
this is something else;
and this is just a random set of special chars:
&@%# :)\"
"
press
echo "If your phrase also includes one of the following special characters, apart from being quoted they also have to be escaped using a backslash (\\):
\\\"
2015-05-30 18:17:04 +02:00
\\' if everything is enclosed in single quotes
2015-05-29 17:47:35 +02:00
\\\$ if followed by a letter or number
\\\`
Example:
echo This will print \\\"echo \\\` it works! \\\` \\\\ \\\"
Will print:
`echo This will print \\\"echo \\\` it works! \\\` \\\\ \\\"`
"
press
echo "To create a file using echo the usage is also very simple:
echo \"This and
this text will go to a text file.\" > file.txt
Of course, you can easily change the extension from txt to sh to create a shell script (just don't forget the shebang).
"
press
2015-05-29 17:47:35 +02:00
}
learn_1() {
echo "The shell is a command interpreter.
More than just the insulating layer between
the operating system kernel and the user, it's also
a fairly powerful programming language.
A shell program, called a  script, is an easy-to-use
tool for building applications by \"gluing together\"
system calls, tools, utilities, and compiled binaries.
Virtually the entire repertoire of UNIX commands,
utilities, and tools is available
for invocation by a shell script.
If that were not enough, internal shell commands,
such as testing and loop constructs,
lend additional power and flexibility to scripts.
Shell scripts are especially well suited for
administrative system tasks and other routine
repetitive tasks not requiring the bells and
whistles of a full-blown tightly structured
programming language."
press
}
learn_2() {
2015-05-30 18:36:50 +02:00
current="learn_2_lesson"
set +H
2015-05-29 17:47:35 +02:00
learn_2_lesson
until [ "$try" = "y" ]; do
[ "$try" = "n" ] && echo "Try again (I know you can do it!)!"
echo "
Task:
Create a script named first_script.sh that prints
\"Hello World! :)\"
2015-05-30 18:19:34 +02:00
(with the quotes) when you execute it. Use vi, nano or echo, type exit when you're done,
r to repeat the lesson and s to skip the exercise."
2015-05-29 17:47:35 +02:00
bash
2015-05-30 18:23:32 +02:00
./first_script.sh 2>&1 | grep -q "^\"Hello World! :)\"$" && try=y && done=y && ./first_script.sh || try=n
2015-05-29 17:47:35 +02:00
clear
done
2015-05-30 18:23:32 +02:00
[ "$done" = "y" ] && echo "Good Job!" || echo "Come back soon!"
2015-05-29 17:47:35 +02:00
sleep 2
}
2015-05-30 18:40:29 +02:00
until [ "$n" = q ]; do
2015-05-29 18:01:17 +02:00
echo -n "This project is licensed under the GPLv3 license.
Welcome to the Learn shell scripting script!
2015-05-29 17:47:35 +02:00
This script can be of great help
if you want to learn shell scripting
and how to use Linux in general.
2015-05-29 18:01:17 +02:00
Created by Daniil Gentili.
2015-05-29 17:47:35 +02:00
Thanks to the Linux Documentation Project
for the introduction.
2015-05-29 18:01:17 +02:00
2015-05-29 17:47:35 +02:00
To start, here are the lessons:
1. Introduction.
2. My first Hello World!
2015-05-30 18:40:29 +02:00
Your selection (number, enter q to exit.): "
2015-05-30 18:48:12 +02:00
try=
2015-05-29 17:47:35 +02:00
read n
clear
eval learn_$n
clear
done