Hōtuhi Bash - Me pehea te Panui i te Whakauru a te Kaiwhakamahi

Ko te linux panuihia whakamahia te whakahau ki te tango i te urunga kaiwhakamahi mai i te raina whakahau. He whai hua tenei ina e hiahia ana maatau ki te whakarato i te whakawhitiwhiti a nga kaiwhakamahi i te wa weto.

Ko te wetereo panui ko:

read [options] variable_name

Ka taea e maatau te whakamahi i te $ haina i mua o te ingoa taurangi kia uru ki tona uara, hei tauira $variable_name.

Panui Papaanga hei Panui i te Whakauru Kaiwhakamahi

Timata ma te hanga i tetahi konae ki .sh toronga, hei tauira:

touch user_input.sh

Ka whakatuwherahia ka konae ki to etita tino pai ka paato i nga mea e whai ake nei:

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

Ko te tuhinga o runga ake nei te ingoa me te tau o te kaiwhakamahi.



Panui:Kaore e hiahiatia te tohu i te momo taurangi e panuihia ana.

Hei whakahaere i te tuhinga o runga ake nei, whakatuwherahia te tauranga ka momo:

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

Karere Tere Me Tono Panui

Hei akiaki i tetahi korero me te ota panui, ka whakamahia te -p whiringa.

Hei tauira:

$ read -p 'Enter your username: ' username

Mena kaore maatau e hiahia kia whakaatuhia nga taatai ​​ki runga i te mata, me whakamahi te -s kōwhiringa me te whakahau panui. He pai tenei mo a tatou e panui ana i nga kupuhipa.

Hei tauira:

$ read -sp 'Enter your password: ' password

Ko to tuhinga tuhi mo te panui i nga whakauru a nga kaiwhakamahi i runga ake nei ka penei:

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

Ko te putanga ko:

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