Bash-Scripting-Grundlagen für Nicht-Programmierer

Der Artikel richtet sich an Personen, die wenig oder keine Erfahrung mit der Unix / Linux-Befehlszeile haben, aber lernen möchten, wie sie effektiv damit interagieren und Skripte entwickeln können, um ihre Aufgaben zu erfüllen. Die angegebenen Beispiele gelten für die Ausführung in der Bash-Shell des Ubuntu / Debian-Betriebssystems, können jedoch unter Berücksichtigung ihrer Besonderheiten in anderen Shells und Betriebssystemen verwendet werden.





1. Befehlshells

Es gibt viele Distributionen (Gabeln) von Betriebssystemen (Linux) der Linux-Familie, die bekanntesten unter ihnen: 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



. , .





Wenn der interne Echo-Befehl nicht in der Shell implementiert ist, ist das Skript mit dem Echo-Aufruf erfolgreich, da Die ausführbare Datei / usr / bin / echo wird zur Verarbeitung des Aufrufs verwendet. Um nach zuvor ausgeführten Befehlen zu suchen, können Sie die Aufwärts- und Abwärts-Tasten verwenden. Befehle aus dem Verlauf können bearbeitet und erneut ausgeführt werden. Am Ende des Artikels finden Sie eine Liste nützlicher Befehle.





Wenn dieses Material interessant ist, werde ich in der Fortsetzung des Artikels über Skripte und ihre Parameter, Dateiberechtigungen, Anweisungen zur bedingten Ausführung, Auswahl und Schleifen, Funktionen und den Taskplaner sprechen.








All Articles