Dasar-dasar skrip Bash untuk non-pemrogram

Artikel ini ditujukan bagi mereka yang memiliki sedikit atau tidak memiliki pengalaman dengan baris perintah Unix / Linux, tetapi ingin mempelajari cara berinteraksi secara efektif dengannya dan mengembangkan skrip untuk menyelesaikan tugas mereka. Contoh yang diberikan valid untuk dijalankan di shell bash sistem operasi Ubuntu / Debian, tetapi dapat digunakan di shell dan sistem operasi lain, dengan mempertimbangkan spesifikasinya.





1. Kerang perintah

Ada banyak distribusi (garpu) sistem operasi (OS) dari keluarga Linux, yang paling terkenal di antaranya: 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



. , .





Jadi, jika perintah echo internal tidak diterapkan di shell, skrip yang berisi panggilan echo akan berhasil karena file yang dapat dieksekusi / usr / bin / echo akan digunakan untuk memproses panggilan. Untuk mencari perintah yang dieksekusi sebelumnya, Anda dapat menggunakan tombol Atas dan Bawah, perintah dari riwayat dapat diedit dan dijalankan kembali. Ada daftar perintah yang berguna di akhir artikel.





Jika materi ini menarik, maka dalam kelanjutan artikel saya akan berbicara tentang skrip dan parameternya, izin file, pernyataan eksekusi bersyarat, seleksi dan loop, fungsi, dan penjadwal tugas.








All Articles