📜 ⬆️ ⬇️

Basics of BASH. Part 1

Of course, all those who communicate with the Linux OS at least once have dealt (at least heard exactly) with the command shell BASH. But BASH is not only a command shell, it is also an excellent scripting programming language.
The purpose of this article is to familiarize users with bash, talk about the syntax, basic techniques and features of the language, so that even an ordinary user can quickly write a simple script to perform daily (-week, -month) routine work or, say, “on the knee »Scribble for backup directory.

Introduction


BASH - Bourne-Again SHell (which can be translated as “reborn walked”, or “Walked Born again (created by sh)”), the most popular command interpreter on Unix-like systems, especially GNU / Linux. Below is a series of built-in commands that we will use to create our own scripts.

break for, while until
continue for, while until
echo , ,
exit
export
hash , ,
kill
pwd
read .\
return
shift
test
times ,
trap ,
unset
wait .

And of course, in addition to the built-in commands, we will use a whole bunch of external, separate commands-programs, which we will get to know in the process.

What you need to know from the beginning


1. Any bash script should start with the line:
')
#!/bin/bash
in this line after #! The path to the bash interpreter is indicated, so if you have it installed in another place (where you can find out by typing whereis bash), change it to your path.
2. Comments begin with a # character (except the first line).
3. In bash, variables have no type (they will be discussed below)

Script variables and parameters


I will give as an example a small example, which we analyze:

#!/bin/bash
# bash-
parametr1=$1 # parametr1
script_name=$0 # script_name
echo " $script_name $parametr1" # echo , $_.
echo ' $script_name $parametr1' # , , .
exit 0 # 0 ( )


Script execution result:

ite@ite-desktop:~$ ./test.sh qwerty
./test.sh qwerty
$script_name $parametr1


After we have learned how to use variables and pass parameters to a script, it's time to get acquainted with reserved variables:

$DIRSTACK -
$EDITOR -
$EUID - UID. su , UID , ...
$UID - ... , .
$FUNCNAME - .
$GROUPS -
$HOME -
$HOSTNAME - hostname
$HOSTTYPE - .
$LC_CTYPE - ,
$OLDPWD -
$OSTYPE -
$PATH -
$PPID -
$SECONDS - ( .)
$# -
$* - ( )
$@ - , ,
$! - PID
$$ - PID


, , , - . bash . ( ):
#!/bin/bash
source=$1 # source
dest=$2 # dest

if [[ "$source" -eq "$dest" ]] # . -eq - ""
then # ,
echo " $dest $source !" # , .. $source $dest
exit 1 # (1 - )
else #
cp $source $dest # cp:
echo " !"
fi # .

:
ite@ite-desktop:~$ ./primer2.sh 1 1
1 1 !
ite@ite-desktop:~$ ./primer2.sh 1 2
!

if-then-else :
if < (0 1)>
then
< if , >
else
< if , >
[[ , [ , test, (( )) ( ) linux-.
test - . , "]"
[ - test
[[ - "[" ( 2.02)( ), || (), & (). "]]"
(( )) - .
:
if ...
then ....
else
if ....
then....
else ....

, :
if ..
then ...
elif ...
then ...
elif ...

.
- , case.
#!/bin/bash
echo " :"
echo "1 nano"
echo "2 vi"
echo "3 emacs"
echo "4 "
read doing # $doing

case $doing in
1)
/usr/bin/nano # $doing 1, nano
;;
2)
/usr/bin/vi # $doing 2, vi
;;
3)
/usr/bin/emacs # $doing 3, emacs
;;
4)
exit 0
;;
*) # , case , :
echo " "

esac # case.

:
ite@ite-desktop:~$ ./menu2.sh
:
1 nano
2 vi
3 emacs
4

Enter , ( , :) )
, if-then-else-fi:
-z #
-n #
=, (==) #
!= #
-eq #
-ne #
-lt,(< ) #
-le,(<=) #
-gt,(>) #
-ge,(>=) #
! #
-a,(&&) # «»
-o,(||) # «»

, , ( 3). .

UPD:
UPD: if-then-else

unix-admin.su

$DIRSTACK -
$EDITOR -
$EUID - UID. su , UID , ...
$UID - ... , .
$FUNCNAME - .
$GROUPS -
$HOME -
$HOSTNAME - hostname
$HOSTTYPE - .
$LC_CTYPE - ,
$OLDPWD -
$OSTYPE -
$PATH -
$PPID -
$SECONDS - ( .)
$# -
$* - ( )
$@ - , ,
$! - PID
$$ - PID


, , , - . bash . ( ):
#!/bin/bash
source=$1 # source
dest=$2 # dest

if [[ "$source" -eq "$dest" ]] # . -eq - ""
then # ,
echo " $dest $source !" # , .. $source $dest
exit 1 # (1 - )
else #
cp $source $dest # cp:
echo " !"
fi # .

:
ite@ite-desktop:~$ ./primer2.sh 1 1
1 1 !
ite@ite-desktop:~$ ./primer2.sh 1 2
!

if-then-else :
if < (0 1)>
then
< if , >
else
< if , >
[[ , [ , test, (( )) ( ) linux-.
test - . , "]"
[ - test
[[ - "[" ( 2.02)( ), || (), & (). "]]"
(( )) - .
:
if ...
then ....
else
if ....
then....
else ....

, :
if ..
then ...
elif ...
then ...
elif ...

.
- , case.
#!/bin/bash
echo " :"
echo "1 nano"
echo "2 vi"
echo "3 emacs"
echo "4 "
read doing # $doing

case $doing in
1)
/usr/bin/nano # $doing 1, nano
;;
2)
/usr/bin/vi # $doing 2, vi
;;
3)
/usr/bin/emacs # $doing 3, emacs
;;
4)
exit 0
;;
*) # , case , :
echo " "

esac # case.

:
ite@ite-desktop:~$ ./menu2.sh
:
1 nano
2 vi
3 emacs
4

Enter , ( , :) )
, if-then-else-fi:
-z #
-n #
=, (==) #
!= #
-eq #
-ne #
-lt,(< ) #
-le,(<=) #
-gt,(>) #
-ge,(>=) #
! #
-a,(&&) # «»
-o,(||) # «»

, , ( 3). .

UPD:
UPD: if-then-else

unix-admin.su

$DIRSTACK -
$EDITOR -
$EUID - UID. su , UID , ...
$UID - ... , .
$FUNCNAME - .
$GROUPS -
$HOME -
$HOSTNAME - hostname
$HOSTTYPE - .
$LC_CTYPE - ,
$OLDPWD -
$OSTYPE -
$PATH -
$PPID -
$SECONDS - ( .)
$# -
$* - ( )
$@ - , ,
$! - PID
$$ - PID


, , , - . bash . ( ):
#!/bin/bash
source=$1 # source
dest=$2 # dest

if [[ "$source" -eq "$dest" ]] # . -eq - ""
then # ,
echo " $dest $source !" # , .. $source $dest
exit 1 # (1 - )
else #
cp $source $dest # cp:
echo " !"
fi # .

:
ite@ite-desktop:~$ ./primer2.sh 1 1
1 1 !
ite@ite-desktop:~$ ./primer2.sh 1 2
!

if-then-else :
if < (0 1)>
then
< if , >
else
< if , >
[[ , [ , test, (( )) ( ) linux-.
test - . , "]"
[ - test
[[ - "[" ( 2.02)( ), || (), & (). "]]"
(( )) - .
:
if ...
then ....
else
if ....
then....
else ....

, :
if ..
then ...
elif ...
then ...
elif ...

.
- , case.
#!/bin/bash
echo " :"
echo "1 nano"
echo "2 vi"
echo "3 emacs"
echo "4 "
read doing # $doing

case $doing in
1)
/usr/bin/nano # $doing 1, nano
;;
2)
/usr/bin/vi # $doing 2, vi
;;
3)
/usr/bin/emacs # $doing 3, emacs
;;
4)
exit 0
;;
*) # , case , :
echo " "

esac # case.

:
ite@ite-desktop:~$ ./menu2.sh
:
1 nano
2 vi
3 emacs
4

Enter , ( , :) )
, if-then-else-fi:
-z #
-n #
=, (==) #
!= #
-eq #
-ne #
-lt,(< ) #
-le,(<=) #
-gt,(>) #
-ge,(>=) #
! #
-a,(&&) # «»
-o,(||) # «»

, , ( 3). .

UPD:
UPD: if-then-else

unix-admin.su

$DIRSTACK -
$EDITOR -
$EUID - UID. su , UID , ...
$UID - ... , .
$FUNCNAME - .
$GROUPS -
$HOME -
$HOSTNAME - hostname
$HOSTTYPE - .
$LC_CTYPE - ,
$OLDPWD -
$OSTYPE -
$PATH -
$PPID -
$SECONDS - ( .)
$# -
$* - ( )
$@ - , ,
$! - PID
$$ - PID


, , , - . bash . ( ):
#!/bin/bash
source=$1 # source
dest=$2 # dest

if [[ "$source" -eq "$dest" ]] # . -eq - ""
then # ,
echo " $dest $source !" # , .. $source $dest
exit 1 # (1 - )
else #
cp $source $dest # cp:
echo " !"
fi # .

:
ite@ite-desktop:~$ ./primer2.sh 1 1
1 1 !
ite@ite-desktop:~$ ./primer2.sh 1 2
!

if-then-else :
if < (0 1)>
then
< if , >
else
< if , >
[[ , [ , test, (( )) ( ) linux-.
test - . , "]"
[ - test
[[ - "[" ( 2.02)( ), || (), & (). "]]"
(( )) - .
:
if ...
then ....
else
if ....
then....
else ....

, :
if ..
then ...
elif ...
then ...
elif ...

.
- , case.
#!/bin/bash
echo " :"
echo "1 nano"
echo "2 vi"
echo "3 emacs"
echo "4 "
read doing # $doing

case $doing in
1)
/usr/bin/nano # $doing 1, nano
;;
2)
/usr/bin/vi # $doing 2, vi
;;
3)
/usr/bin/emacs # $doing 3, emacs
;;
4)
exit 0
;;
*) # , case , :
echo " "

esac # case.

:
ite@ite-desktop:~$ ./menu2.sh
:
1 nano
2 vi
3 emacs
4

Enter , ( , :) )
, if-then-else-fi:
-z #
-n #
=, (==) #
!= #
-eq #
-ne #
-lt,(< ) #
-le,(<=) #
-gt,(>) #
-ge,(>=) #
! #
-a,(&&) # «»
-o,(||) # «»

, , ( 3). .

UPD:
UPD: if-then-else

unix-admin.su

$DIRSTACK -
$EDITOR -
$EUID - UID. su , UID , ...
$UID - ... , .
$FUNCNAME - .
$GROUPS -
$HOME -
$HOSTNAME - hostname
$HOSTTYPE - .
$LC_CTYPE - ,
$OLDPWD -
$OSTYPE -
$PATH -
$PPID -
$SECONDS - ( .)
$# -
$* - ( )
$@ - , ,
$! - PID
$$ - PID


, , , - . bash . ( ):
#!/bin/bash
source=$1 # source
dest=$2 # dest

if [[ "$source" -eq "$dest" ]] # . -eq - ""
then # ,
echo " $dest $source !" # , .. $source $dest
exit 1 # (1 - )
else #
cp $source $dest # cp:
echo " !"
fi # .

:
ite@ite-desktop:~$ ./primer2.sh 1 1
1 1 !
ite@ite-desktop:~$ ./primer2.sh 1 2
!

if-then-else :
if < (0 1)>
then
< if , >
else
< if , >
[[ , [ , test, (( )) ( ) linux-.
test - . , "]"
[ - test
[[ - "[" ( 2.02)( ), || (), & (). "]]"
(( )) - .
:
if ...
then ....
else
if ....
then....
else ....

, :
if ..
then ...
elif ...
then ...
elif ...

.
- , case.
#!/bin/bash
echo " :"
echo "1 nano"
echo "2 vi"
echo "3 emacs"
echo "4 "
read doing # $doing

case $doing in
1)
/usr/bin/nano # $doing 1, nano
;;
2)
/usr/bin/vi # $doing 2, vi
;;
3)
/usr/bin/emacs # $doing 3, emacs
;;
4)
exit 0
;;
*) # , case , :
echo " "

esac # case.

:
ite@ite-desktop:~$ ./menu2.sh
:
1 nano
2 vi
3 emacs
4

Enter , ( , :) )
, if-then-else-fi:
-z #
-n #
=, (==) #
!= #
-eq #
-ne #
-lt,(< ) #
-le,(<=) #
-gt,(>) #
-ge,(>=) #
! #
-a,(&&) # «»
-o,(||) # «»

, , ( 3). .

UPD:
UPD: if-then-else

unix-admin.su

Source: https://habr.com/ru/post/47163/


All Articles