Di bagian pertama artikel, kami melihat shell, profil, sinonim, dan perintah pertama. Di bawah spoiler, saya juga menjelaskan cara menggunakan mesin virtual uji.
Pada bagian ini, kita akan berbicara tentang file skrip, parameternya, dan hak aksesnya. Saya juga akan berbicara tentang eksekusi bersyarat, seleksi dan pernyataan perulangan.
Skrip
Lebih mudah menggunakan skrip untuk menjalankan beberapa perintah dalam satu panggilan. Skrip adalah file teks yang berisi perintah shell. Ini bisa berupa perintah shell internal dan panggilan ke file eksternal yang dapat dieksekusi.
Biasanya, nama file skrip diakhiri dengan .sh, tetapi ini bukan persyaratan wajib dan hanya digunakan untuk memudahkan pengguna menavigasi menurut nama file. Bagi interpreter, isi file lebih penting, begitu pula hak aksesnya.
Mari masuk ke direktori home dengan perintah cd ~
dan buat nano script.sh
file di dalamnya menggunakan editor nano ( ), yang berisi 2 baris:
#!/bin/bash
echo Hello!
Untuk keluar dari editor nano setelah mengetik skrip, Anda perlu menekan Ctrl + X, lalu ke pertanyaan "Simpan buffer yang diubah?" tekan Y, lalu atas permintaan "Nama File untuk Menulis:" tekan Enter. Anda dapat menggunakan editor teks lain jika Anda mau.
Skrip dijalankan dengan perintah ./<_>
, mis. ./
sebelum nama file menunjukkan bahwa skrip atau file yang dapat dijalankan yang terletak di direktori saat ini harus dijalankan. Jika Anda menjalankan perintah script.sh
, kesalahan akan dikeluarkan, karena shell akan mencari file di direktori yang ditentukan dalam variabel lingkungan PATH, serta di antara perintah bawaan (seperti, misalnya, pwd):
test@osboxes:~$ script.sh
script.sh: command not found
, , : /home/user/script.sh
. :
test@osboxes:~$ ./script.sh
-bash: ./script.sh: Permission denied
:
test@osboxes:~$ ls -l script.sh
-rw-rw-r-- 1 test test 22 Nov 9 05:27 script.sh
ls
, . :
: , ; , ; . r, w x , .
(test) , , โ . , , umask -S
. , umask ( ~/.profile), ( /etc/profile).
, , chmod <> <_>
. , , :
test@osboxes:~$ chmod a+x script.sh
:
test@osboxes:~$ chmod ug+rx script.sh
( ) :
test@osboxes:~$ chmod a-w script.sh
. , , , , , โ , :
test@osboxes:~$ chmod 754 script.sh
-rwxr-xr--
:
test@osboxes:~$ ls -la script.sh
-rwxr-xr-- 1 test test 22 Nov 9 05:27 script.sh
3 , . , , . :
โ
(โ
, d
โ , l
โ , c
โ , b
โ , . .). , :
|
|
|
|
0 |
000 |
|
1 |
001 |
(x) |
2 |
010 |
(w) |
3 |
011 |
(wx) |
4 |
100 |
(r) |
5 |
101 |
(rx) |
6 |
110 |
(rw) |
7 |
111 |
, (rwx) |
, :
test@osboxes:~$ ./script.sh
Hello!
#!/bin/bash
. #!
ฬ (. shebang) , . , .
#!/bin/sh
. , , /bin/sh shell, /bin/sh /bin/dash, . echo Hello!
, .
, , . : <_> <1> <2> โฆ
, ./script1.sh Moscow Russia
.
, , $1
, - $2
, .. , :
$0
โ
$#
โ
$$
โ PID() ,
$?
โ
script1.sh :
#!/bin/bash
echo Hello, $USER!
printf "Specified City is: %s, Country is: %s\n" $1 $2
:
test@osboxes:~$ chmod u+x script1.sh
test@osboxes:~$ ./script1.sh Moscow Russia
Hello, test!
Specified City is: Moscow, Country is: Russia
2 , , , , printf. Hello USER.
, , ( ), :
test@osboxes:~$ ./script1.sh "San Francisco" "United States"
Hello, test!
Specified City is: San Francisco, Country is: United States
, printf :
printf "Specified City is: %s, Country is: %s\n" "$1" "$2"
, $. , :
COUNTRY=RUSSIA
echo $COUNTRY
,
, , bash โ . , โ . .
:
if [ <> ]
then
<1>
else
<2>
fi
, (, ), (.. ) 8 :
#!/bin/bash
echo Hello, $USER!
echo -n "Enter string: "
read str
if [ ${#str} -lt 8 ]
then
echo String is too short
else
echo String is ok
fi
2 , 5 8 :
test@osboxes:~$ ./script2.sh
Hello, test!
Enter string: abcde
String is too short
test@osboxes:~$ ./script2.sh
Hello, test!
Enter string: abcdefgh
String is ok
read str
, str. ${#str}
str 8. , 8 (-lt 8
), ยซString is too shortยป, โ ยซString is okยป.
, , , 8 16 , [ ${#str} -lt 8 ] || [ ${#str} -gt 16 ]
. ||
"", "" bash &&
.
:
#!/bin/bash
echo Hello, $USER!
echo -n "Enter string: "
read str
if [ ${#str} -lt 8 ]
then
echo String is too short
else
if [ ${#str} -gt 16 ]
then
echo String is too long
else
echo String is ok
fi
fi
, 8 , , "String is too short", . ( 8 ) - ( else) , 16 . - "String is too long", ( else) - "String is ok".
:
test@osboxes:~$ ./script2.sh
Hello, test!
Enter string: abcdef
String is too short
test@osboxes:~$ ./script2.sh
Hello, test!
Enter string: abcdefghijklmnopqrstuv
String is too long
test@osboxes:~$ ./script2.sh
Hello, test!
Enter string: abcdefghijkl
String is ok
:
case "$" in
"$1" )
<1>;;
"$2" )
<2>;;
esac
, :
#!/bin/bash
echo -n "Enter the name of planet: "
read PLANET
echo -n "The $PLANET has "
case $PLANET in
Mercury | Venus ) echo -n "no";;
Earth ) echo -n "one";;
Mars ) echo -n "two";;
Jupiter ) echo -n "79";;
*) echo -n "an unknown number of";;
esac
echo " satellite(s)."
:
test@osboxes:~$ ./script3.sh
Enter the name of planet: Mercury
The Mercury has no satellite(s).
test@osboxes:~$ ./script3.sh
Enter the name of planet: Venus
The Venus has no satellite(s).
test@osboxes:~$ ./script3.sh
Enter the name of planet: Earth
The Earth has one satellite(s).
test@osboxes:~$ ./script3.sh
Enter the name of planet: Mars
The Mars has two satellite(s).
test@osboxes:~$ ./script3.sh
Enter the name of planet: Jupiter
The Jupiter has 79 satellite(s).
test@osboxes:~$ ./script3.sh
Enter the name of planet: Alpha555
The Alpha555 has an unknown number of satellite(s).
.
case Mercury | Venus
, |
"" ( if, ||
), "no" , . case []
. , :
#!/bin/bash
echo -n "Enter key: "
read -n 1 key
echo
case "$key" in
[a-z] ) echo "Lowercase";;
[A-Z] ) echo "Uppercase";;
[0-9] ) echo "Digit";;
* ) echo "Something else";;
esac
( , , , ). :
test@osboxes:~$ ./a.sh
Enter key: t
Lowercase
test@osboxes:~$ ./a.sh
Enter key: P
Uppercase
test@osboxes:~$ ./a.sh
Enter key: 5
Digit
test@osboxes:~$ ./a.sh
Enter key: @
Something else
:
( ):
for [ <> ] do <> done
, :
while [ <> ] do <> done
, :
until [ <> ] do <> done
while , EXIT
#!/bin/bash
PLANET="-"
while [ $PLANET != "EXIT" ]
do
echo -n "Enter the name of planet: "
read PLANET
if [ $PLANET != "EXIT" ]
then.
echo -n "The $PLANET has "
case $PLANET in
Mercury | Venus ) echo -n "no";;
Earth ) echo -n "one";;
Mars ) echo -n "two";;
Jupiter ) echo -n "79";;
*) echo -n "an unknown number of";;
esac
echo " satellite(s)."
fi
done
, , EXIT. , , EXIT:
test@osboxes:~$ ./script4.sh
Enter the name of planet: Earth
The Earth has one satellite(s).
Enter the name of planet: Jupiter
The Jupiter has 79 satellite(s).
Enter the name of planet: Planet123
The Planet123 has an unknown number of satellite(s).
Enter the name of planet: EXIT
, while [ $PLANET != "EXIT" ]
until [ $PLANET == "EXIT" ]
. ==
"", !=
" ".
:
#!/bin/bash
rm *.dat
echo -n "File count: "
read count
for (( i=1; i<=$count; i++ ))
do
head -c ${i}M </dev/urandom >myfile${i}mb.dat
done
ls -l *.dat
echo -n "Delete file greater than (mb): "
read maxsize
for f in *.dat
do
size=$(( $(stat -c %s $f) /1024/1024))
if [ $size -gt $maxsize ]
then.
rm $f
echo Deleted file $f
fi
done
ls -l *.dat
read
, (read count
).
(for (( i=1; i<=$count; i++ ))
) , count, . head
, /dev/random, .
<
(/dev/urandom) head
.
>
( head -c ${i}M
) , (myfile${i}mb.dat
).
, .
(for f in *.dat
) .dat , . , , .
.dat, (ls -l *.dat
). :
test@osboxes:~$ ./script5.sh
File count: 10
-rw-rw-r-- 1 test test 10485760 Nov 9 08:48 myfile10mb.dat
-rw-rw-r-- 1 test test 1048576 Nov 9 08:48 myfile1mb.dat
-rw-rw-r-- 1 test test 2097152 Nov 9 08:48 myfile2mb.dat
-rw-rw-r-- 1 test test 3145728 Nov 9 08:48 myfile3mb.dat
-rw-rw-r-- 1 test test 4194304 Nov 9 08:48 myfile4mb.dat
-rw-rw-r-- 1 test test 5242880 Nov 9 08:48 myfile5mb.dat
-rw-rw-r-- 1 test test 6291456 Nov 9 08:48 myfile6mb.dat
-rw-rw-r-- 1 test test 7340032 Nov 9 08:48 myfile7mb.dat
-rw-rw-r-- 1 test test 8388608 Nov 9 08:48 myfile8mb.dat
-rw-rw-r-- 1 test test 9437184 Nov 9 08:48 myfile9mb.dat
Delete file greater than (mb): 5
Deleted file myfile10mb.dat
Deleted file myfile6mb.dat
Deleted file myfile7mb.dat
Deleted file myfile8mb.dat
Deleted file myfile9mb.dat
-rw-rw-r-- 1 test test 1048576 Nov 9 08:48 myfile1mb.dat
-rw-rw-r-- 1 test test 2097152 Nov 9 08:48 myfile2mb.dat
-rw-rw-r-- 1 test test 3145728 Nov 9 08:48 myfile3mb.dat
-rw-rw-r-- 1 test test 4194304 Nov 9 08:48 myfile4mb.dat
-rw-rw-r-- 1 test test 5242880 Nov 9 08:48 myfile5mb.dat
10 (myfile1mb.dat .. myfile10mb.dat) 1 10 .dat 5 . (Deleted file myfile10mb.dat
). (myfile1mb.dat .. myfile5mb.dat).
, cron, .