Likhetho tse ling tsa Bash li tsebahala ebile li sebelisoa hangata. Ka mohlala, batho ba bangata ba ngola qalong ea script
seta -o xtrace
bakeng sa debugging,
seta -o errexit
ho tsoa ka phoso kapa
set -o erruset
ho tsoa haeba phapano e bitswang e sa setwa.
Empa ho na le likhetho tse ling tse ngata. Ka nako e 'ngoe li hlalosoa ka mokhoa o ferekanyang haholo mananeng, kahoo ke bokelletse tse ling tsa bohlokoa ka ho fetisisa mona, ka tlhaloso.
Tlhokomeliso: Li-Mac li kanna tsa ba le mofuta oa khale oa bash (3.x ho fapana le 4.x) moo likhetho tsena kaofela li sa fumaneheng. Tabeng ena, bona
set
kapa shopt
?
Ho na le mekhoa e 'meli ea ho beha likhetho tsa bash: ho tsoa ho script kapa ho tsoa molaong oa taelo. U ka sebelisa litaelo tse hahelletsoeng set
и shopt
. Ka bobeli li fetola boitšoaro ba khetla, li etsa ntho e ts'oanang (ka likhang tse fapaneng), empa li fapane ho tsona set
li futsitsoe kapa li alimiloe ho tsoa ho likhetla tse ling, ha li-parameter shopt
e entsoeng ka bash.
Haeba u batla ho bona likhetho tsa hajoale, tsamaisa:
$ set -o
$ shopt
Ho kenya tshebetsong kgetho ho set
Ho sebelisoa syntax e telele kapa e khuts'oane:
$ set -o errunset
$ set -e
Phello e tšoana.
Ho tima khetho, o hloka ho kenya tlatsetso ho fapana le ho tlosa:
$ set +e
Ka nako e telele ke ne ke sa hopole syntax ena hobane mohopolo o ne o bonahala o fosahetse (letšoao la minus le nolofalletsa khetho, 'me lets'oao le kenyelletsang lea e tima).
В shopt
(ho utloahala haholoanyane) lifolakha li sebelisoa ho nolofalletsa le ho tima likhetho -s
(sete) le -u
(e sa hlophisoa):
$ shopt -s cdspell # <= on
$ shopt -u cdspell # <= off
Ho fetola li-directory
Ho na le likhetho tse 'maloa tse u thusang ho sebetsa le li-directory.
1.cdspell
Ka tlhophiso ena, bash e tla qala ho utloisisa typos mme e tla u isa foldareng eo lebitso la eona u le ngotseng hampe.
$ shopt -s cdspell
$ mkdir abcdefg
$ cd abcdeg
abcdefg
$ cd ..
Ke 'nile ka sebelisa khetho ena ka lilemo tse ngata' me ka seoelo (mohlomong hang ka selemo) e etsa qeto e makatsang haholo. Empa ka matsatsi a mang cdspell
e boloka nako, ka ho toba letsatsi le letsatsi.
2. autocd
Haeba u sa ikemisetsa ho amohela ho se sebetse hantle ha likenyo tse ngata cd
, joale o ka seta khetho ena ho ea foldareng ea X haeba taelo ea X e le sieo.
$ shopt -s autocd
$ abcdefg
$ cd ..
Ha e kopantsoe le autocomplete, sena se u lumella ho tlola kapele lipakeng tsa lifoldara:
$ ./abc[TAB][RETURN]
cd -- ./abcdefg
Se ke oa bolela foldara feela rm -rf *
(e, ka tsela, sena se ka khoneha).
3.atolosa
Ena ke khetho e ntle e holisang mefuta e fapaneng ea tikoloho ka ho tobetsa Tab:
$ shopt -s direxpand
$ ./[TAB] # заменяется на...
$ /full/path/to/current_working_folder
$ ~/[TAB] # заменяется на...
$ /full/path/to/home/folder
$ $HOME/[TAB] # заменяется на...
$ /full/path/to/home/folder
tlhahiso e hloekileng
4. mesebetsi ea lichelete
Khetho ena e emisa ho tsoa sebokeng haeba ho ntse ho na le mesebetsi e sebetsang ka morao.
Ho e-na le ho tsoa, ho bontšoa lethathamo la mesebetsi e sa phethoang. Haeba u ntse u batla ho tsoa, joale kenya hape exit
.
$ shopt -s checkjobs
$ echo $$
68125 # <= ID процесса для оболочки
$ sleep 999 &
$ exit
There are running jobs.
[1]+ Running sleep 999 &
$ echo $$
68125 # <= ID процесса для оболочки тот же
$ exit
There are running jobs.
[1]+ Running sleep 999 &
$ exit
$ echo $$
$ 59316 # <= на этот раз ID процесса изменился
Phatlalatso e matla
5.globstar
Khetho ena e u fa matla a matla a ho nkela sebaka! Haeba u kena:
$ shopt -s globstar
$ ls **
joale khetla e tla bontša li-directory tsohle le li-subdirectories ka ho pheta-pheta.
Hammoho le direxpand
U ka sheba ntho e 'ngoe le e' ngoe e ka tlase ho sehlopha sa maemo ka potlako:
$ shopt -s direxpand
$ ls **[TAB][TAB]
Display all 2033 possibilities? (y or n)
6.extglob
Khetho ena e thusa likarolo tse amanang haholo le lipolelo tse tloaelehileng. Ka linako tse ling sena se thusa haholo:
$ shopt -s extglob
$ touch afile bfile cfile
$ ls
afile bfile cfile
$ ls ?(a*|b*)
afile bfile
$ ls !(a*|b*)
cfile
Mona lipaterone li behiloe ka masakaneng 'me li arotsoe ke bar e emeng. Mona ke basebetsi ba fumanehang:
? = e tsamaisana le zero kapa ketsahalo e le 'ngoe ea lipaterone tse fanoeng! = bonts'a ntho e 'ngoe le e 'ngoe e sa lumellaneng le mekhoa e fanoeng * = zero kapa liketsahalo tse ngata + = ketsahalo e le 'ngoe kapa ho feta @ = ketsahalo e le 'ngoe hantle
Tšireletso ea kotsi
7. histverify
Ho ka ba ntho e tšosang qalong ho sebelisa litaelo tse potlakileng tsa ho qala ho tsoa nalaneng ea khutsufatso !!
и !$
.
Khetho histverify
e etsa hore u bone pele hore na Bash o fetolela taelo joang pele e sebetsa:
$ shopt -s histverify
$ echo !$ # <= По нажатию Enter команда не запускается
$ echo histverify # <= Она сначала демонстрируется на экране,
histverify # <= а потом запускается
8. Noclobber
Hape, ho itšireletsa khahlanong le likotsi, e leng ho hlakola faele e seng e ntse e le teng ka redirect operator (>
). Sena e ka ba koluoa haeba u se na bekapo.
Khetho set -С
e thibela ho ngola holimo ho joalo. Haeba ho hlokahala, o ka feta tšireletso ka ho sebelisa opareitara >|
:
$ touch afile
$ set -C
$ echo something > afile
-bash: afile: cannot overwrite existing file
$ echo something >| afile
$
Source: www.habr.com