Principes de base des scripts Bash pour les non-programmeurs

Cet article est destiné à ceux qui ont peu ou pas d'expérience avec la ligne de commande Unix / Linux, mais qui aimeraient apprendre comment interagir efficacement avec elle et développer des scripts pour accomplir leurs tâches. Les exemples donnés sont valables pour fonctionner dans le shell bash du système d'exploitation Ubuntu / Debian, mais ils peuvent être utilisés dans d'autres shells et systèmes d'exploitation, en tenant compte de leurs spécificités.





1. Commandez les coquilles

Il existe de nombreuses distributions (forks) de systèmes d'exploitation (OS) de la famille Linux, les plus connues d'entre elles: Ubuntu, Debian, CentOS, Red Hat, Fedora, SuSE, FreeBSD, Mint.





Linux.





(shell), (, , , ..), , . , , .





Feature





Bourne





C





TC





Korn





Bash





()

































































































(pushd and popd)













































































































































































cat /etc/shells
      
      



sudo apt install <_>



. , ksh





sudo apt install ksh
      
      



2.

/ Linux, , , . ssh - , shell . , () . (), -, . -:

https://cocalc.com/app?anonymous=terminal

https://www.tutorialspoint.com/execute_bash_online.php

https://rextester.com/l/bash_online_compiler





VirtualBox – , «» . , , . , , Windows Mac. VirtualBox .





Ubuntu . , , VirtualBox (VDI).





. ( C:\vmachines). :





7zip , .7z. VirtualBox « -> …»:





( , ), . , 1 . « », , «», «»:





«»:





«»:





«»:





:





osboxes.org osboxes.org. Ctrl+Alt+T, , :





, . , , « -> -> »:





.





(root). sudo su -



, (osboxes.org). test adduser test



. test , usermod -aG sudo test



:





osboxes@osboxes:~$ sudo su -
[sudo] password for osboxes:
root@osboxes:~# adduser test
Adding user `test' ...
Adding new group `test' (1001) ...
Adding new user `test' (1001) with group `test' ...
The home directory `/home/test' already exists.  Not copying from `/etc/skel'.
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]
root@osboxes:~# usermod -aG sudo test
root@osboxes:~#
      
      



, (Ctrl+Alt+T). ssh (, PuTTY), .





3.

, shell. . (), , hostname



Enter:





test@osboxes:~$ hostname
osboxes
      
      



, , (test@osboxes:~$). (, ), whoami



, id



( ):





test@osboxes:~$ whoami
test
test@osboxes:~$ id
uid=1001(test) gid=1001(test) groups=1001(test),27(sudo)
      
      



, , echo $SHELL



:





test@osboxes:~$ echo $SHELL
/bin/bash
      
      



, . echo



, , $SHELL. $ , , .. $SHELL SHELL, . env



. , , , bash. /etc/passwd, :





test@osboxes:~$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
...
test:x:1001:1001:,,,:/home/test:/bin/dash
      
      



cat ( …). , test /bin/dash. , , (chsh – change shell):





test@osboxes:~$ chsh -s /bin/bash
Password:
      
      



, , pwd



:





test@osboxes:~$ pwd
/home/test
      
      



ls



. , . ls –a. , –l. :





test@osboxes:~$ ls -al
total 36
drwxr-xr-x 5 test test 4096 Nov  9 01:05 .
drwxr-xr-x 5 root root 4096 Nov  8 11:39 ..
-rw------- 1 test test    9 Nov  8 12:28 .bash_history
-rw-r--r-- 1 test test  220 Nov  8 11:39 .bash_logout
-rw-r--r-- 1 test test 3771 Nov  8 11:39 .bashrc
drwxr-xr-x 4 test test 4096 Nov  8 11:40 .cache
drwxr-xr-x 4 test test 4096 Nov  8 11:40 .config
drwxr-xr-x 3 test test 4096 Nov  8 11:40 .local
-rw-r--r-- 1 test test  807 Nov  8 11:39 .profile
-rw-r--r-- 1 test test    0 Nov  9 01:05 .sudo_as_admin_successful
test@osboxes:~$
      
      



ls (alias), . alias



:





test@osboxes:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'
      
      



, ll



ls -alF



. . , ll



ls -alF



, ls



ls --color=auto



. . . , , alias lshome='ls -alF $HOME'



, , :





test@osboxes:~$ cd /tmp
test@osboxes:/tmp$ lshome
total 40
drwxr-xr-x 5 test test 4096 Nov  9 02:29 ./
drwxr-xr-x 5 root root 4096 Nov  8 11:39 ../
-rw------- 1 test test   47 Nov  9 02:36 .bash_history
-rw-r--r-- 1 test test  220 Nov  8 11:39 .bash_logout
-rw-r--r-- 1 test test 3771 Nov  8 11:39 .bashrc
drwxr-xr-x 5 test test 4096 Nov  9 02:29 .cache/
drwxr-xr-x 5 test test 4096 Nov  9 02:29 .config/
drwxr-xr-x 3 test test 4096 Nov  8 11:40 .local/
-rw-r--r-- 1 test test  807 Nov  8 11:39 .profile
-rw-rw-r-- 1 test test   72 Nov  9 02:29 .selected_editor
-rw-r--r-- 1 test test    0 Nov  9 01:05 .sudo_as_admin_successful
      
      



cd /tmp



/tmp, , , lshome



, /home/test. $HOME, .





~/.bash_aliases. (~) . /home/test. ~/.bashrc. .





bash , ( ), : /etc/profile, bash : ~/.bash_profile, ~/.bash_login, ~/.profile. .





, , ~./profile ~/.bashrc, , , , .bash_aliases . /etc/profile .sh, etc/profile.d.





bash , help. help <_>



, :





test@osboxes:~$ help pwd
pwd: pwd [-LP]
    Print the name of the current working directory.

    Options:
      -L        print the value of $PWD if it names the current working
                directory
      -P        print the physical directory, without any symbolic links

    By default, `pwd' behaves as if `-L' were specified.

    Exit Status:
    Returns 0 unless an invalid option is given or the current directory
    cannot be read.
      
      



bash . , , . , /usr/bin/echo, echo. help echo



, /usr/bin/echo –help



. , .





Ainsi, si la commande d'écho interne n'est pas implémentée dans le shell, le script contenant l'appel d'écho réussira car le fichier exécutable / usr / bin / echo sera utilisé pour traiter l'appel. Pour rechercher des commandes précédemment exécutées, vous pouvez utiliser les touches Haut et Bas, les commandes de l'historique peuvent être modifiées et réexécutées. Il y a une liste de commandes utiles à la fin de l'article.





Si ce matériel est intéressant, dans la suite de l'article, je parlerai des scripts et de leurs paramètres, des autorisations de fichier, des instructions d'exécution conditionnelle, de la sélection et des boucles, des fonctions et du planificateur de tâches.








All Articles