Iskripthi seBash-Ufunda njani igalelo lomsebenzisi

I-linux funda Umyalelo usetyenziselwa ukuthatha igalelo lomsebenzisi kwilayini yomyalelo. Oku kuluncedo xa sifuna ukubonelela ngokudibana komsebenzisi ngexesha lokubaleka.

Is syntax efundwayo yile:

read [options] variable_name

Singasebenzisa $ sayina phambi kwegama eliguqukayo ukufikelela kwixabiso lalo, o.k.t. $variable_name.




Iskripthi seBash sokuFunda iGalelo loMsebenzisi

Qala ngokwenza ifayile nge .sh ulwandiso, o.k.t.

touch user_input.sh

Emva koko vula emva koko ufake ifayile kumhleli wakho owuthandayo kwaye uchwetheze oku kulandelayo:


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

Iskripthi esingentla sithatha igama lomsebenzisi kunye nobudala.

Phawula:Akukho sidingo sokuchaza uhlobo lokwahluka okufundwayo.

Ukuqhuba iskripthi esingentla, vula itheminali kunye nohlobo:

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

Umyalezo okhawulezileyo Ngokufundwa komyalelo

Ukuhambisa umyalezo ngomyalelo ofundwayo, sisebenzisa -p ukhetho.

Umzekelo:


$ read -p 'Enter your username: ' username

Ukuba asifuni ukuba abalinganiswa baboniswe kwiscreen, kufuneka sisebenzise -s ukhetho ngomyalelo ofundwayo. Oku kuluncedo xa sifunda ipassword.

Umzekelo:

$ read -sp 'Enter your password: ' password

Isicatshulwa sakho se-bash sokufunda ungeniso lomsebenzisi olungentla luya kubonakala ngathi:

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

Iziphumo zezi:


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