I-Bash Script - Ungakufunda Kanjani Ukufaka Komsebenzisi

I-linux funda umyalo usetshenziselwa ukuthatha okokufaka komsebenzisi kulayini womyalo. Lokhu kuyasiza lapho sifuna ukunikeza ukuxhumana komsebenzisi ngesikhathi sokusebenza.

I-syntax efundwayo yile:

read [options] variable_name

Ngemuva kwalokho singasebenzisa $ sayina phambi kwegama eliguqukayo ukufinyelela inani lalo, isb. $variable_name.




Iskripthi seBash sokufunda okokufaka komsebenzisi

Qala ngokwakha ifayela nge .sh isandiso, isb.

touch user_input.sh

Bese uvula bese ufaka kusihleli sakho osithandayo bese uthayipha okulandelayo:


#!/bin/bash echo 'Enter your name:' read name echo 'Enter your age:' read age echo 'Hello' $name, 'you are' $age 'years old'

Umbhalo ongenhla uthatha igama nobudala bomsebenzisi.



Qaphela:Akunasidingo sokucacisa uhlobo lokuguquguqukayo okufundwayo.

Ukuze usebenzise iskripthi esingenhla, vula ukuphela bese uthayipha:

$ sh user_input.sh Enter your name: DevQA Enter your age: 12 Hello DevQA, you are 12 years old

Umlayezo Owenziwe Ngomyalo ofundwayo

Ukuthumela umlayezo ngomyalo ofundwayo, sisebenzisa i- -p inketho.

Ngokwesibonelo:


$ read -p 'Enter your username: ' username

Uma singafuni ukuthi izinhlamvu ziboniswe esikrinini, sidinga ukusebenzisa -s inketho ngomyalo ofundwayo. Lokhu kuyasiza lapho sifunda amaphasiwedi.

Ngokwesibonelo:

$ read -sp 'Enter your password: ' password

Iskripthi sakho se-bash sokufunda okokufaka okungenhla komsebenzisi kuzobukeka:

#!/bin/bash read -p 'Enter your username: ' username read -sp 'Enter your password: ' password echo -e ' Your username is $username and Password is $password'

Umphumela uwukuthi:


$ sh user_input.sh Enter your username: devqa Enter your password: Your username is devqa and Password is secret