Conceptos básicos de bash scripting para no programadores

El artículo está dirigido a aquellos que tienen poca o ninguna experiencia con la línea de comandos de Unix / Linux, pero les gustaría aprender cómo interactuar de manera efectiva con ella y desarrollar scripts para realizar sus tareas. Los ejemplos dados son válidos para ejecutarse en el shell bash del sistema operativo Ubuntu / Debian, pero se pueden usar en otros shells y sistemas operativos, teniendo en cuenta sus especificaciones.





1. Proyectiles de mando

Existen muchas distribuciones (bifurcaciones) de sistemas operativos (SO) de la familia Linux, las más famosas entre ellas: 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



. , .





Por lo tanto, si el comando de eco interno no se implementa en el shell, el script que contiene la llamada de eco tendrá éxito porque el archivo ejecutable / usr / bin / echo se utilizará para procesar la llamada. Para buscar comandos ejecutados anteriormente, puede usar las teclas Arriba y Abajo, los comandos del historial se pueden editar y volver a ejecutar. Hay una lista de comandos útiles al final del artículo.





Si este material es interesante, en la continuación del artículo hablaré sobre los scripts y sus parámetros, permisos de archivo, declaraciones de ejecución condicional, selección y bucles, funciones y el programador de tareas.








All Articles