Bash scripting basics for non-programmers

The article is intended for those who have little or no experience with the Unix / Linux command line, but would like to learn how to effectively interact with it and develop scripts to accomplish their tasks. The examples given are valid for running in the bash shell of the Ubuntu / Debian operating system, but they can be used in other shells and operating systems, taking into account their specifics.





1. Command shells

There are many distributions (forks) of operating systems (OS) of the Linux family, the most famous among them: 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



. , .





Thus, if the internal echo command is not implemented in the shell, the script containing the echo call will succeed because the executable file / usr / bin / echo will be used to process the call. To search for previously executed commands, you can use the Up and Down keys, commands from history can be edited and re-executed. There is a list of useful commands at the end of the article.





If this material is interesting, then in the continuation of the article I will talk about scripts and their parameters, file permissions, conditional execution statements, selection and loops, functions and the task scheduler.








All Articles