posted by 귀염둥이채원 2021. 4. 15. 05:57

---------------------------------------------------------

# 간단한 읽기 명령 예시

---------------------------------------------------------

#!/bin/bash

# echo -n 옵션은 줄바꿈을 하지 않는다.

echo -n "What is your favorite food : "

read answer

echo "Oh! you like $answer!"

 

# echo는 줄바꿈 한다.

echo "What is your favorite food : "

read answer

echo "Oh! you like $answer!"

 

# printf는 줄바꿈을 하지 않는다.

printf "What is your favorite food : "

read answer

echo "Oh! you like $answer!"

 

# printf에서 "\n"을 사용하여 줄바꿈한다.

printf "What is your favorite food : \n"

read answer

echo "Oh! you like $answer!"

 

# output

$ bash test.sh

What is your favorite food : pizza

Oh! you like pizza!

What is your favorite food :

piaaz

Oh! you like piaaz!

What is your favorite food : pizza

Oh! you like pizza!

What is your favorite food :

pizza

Oh! you like pizza!

 

---------------------------------------------------------

# 옵션과 함께 읽기 명령 사용

---------------------------------------------------------

* -p 옵션은 입력과 관련된 사용자에게 유용한 메시지를 표시

* -s 옵션은 사용자가 입력 할 터미널에서 텍스트를 숨기는 데 사용

 

#!/bin/bash

# Type your Login Information

read -p 'Username: ' user

read -sp 'Password: ' pass

 

if (($user == "admin" && $pass == "12345")); then

echo -e "\nSuccessful login"

else

echo -e "\nUnsuccessful login"

fi

 

# output

$ bash test.sh

Username: admin

Password:

Successful login

 

---------------------------------------------------------

# 읽기 명령을 사용하여 여러 입력 가져 오기

---------------------------------------------------------

#!/bin/bash

# Taking multiple inputs

echo -n "Type four names of your favorite programming languages: "

read lan1 lan2 lan3 lan4

echo "$lan1 is your first choice"

echo "$lan2 is your second choice"

echo "$lan3 is your third choice"

echo "$lan4 is your fourth choice"

 

# output

$ bash test.sh

Type four names of your favorite programming languages: python java go javascript

python is your first choice

java is your second choice

go is your third choice

javascript is your fourth choice

 

---------------------------------------------------------

# 시간 제한이있는 읽기 명령 사용

---------------------------------------------------------

* -t옵션을 사용한 사용자에 대한 시간 제한 설정 (초단위)

* 사용자가 5 초 내에 데이터를 입력 할 수 없으면 프로그램은 값없이 종료

#!/bin/bash

read -t 5 -p "Type your favorite color : " color

echo $color

 

# output

$ bash test.sh

Type your favorite color : white

white

 

---------------------------------------------------------

# 간단한 메뉴를 생성하고 선택된 메뉴 항목 에 따라 조치를 취하는 스크립트

---------------------------------------------------------

#!/bin/bash

while :; do

clear

echo "-------------------------------------"

echo " Main Menu "

echo "-------------------------------------"

echo "[1] Show Todays date/time"

echo "[2] Show files in current directory"

echo "[3] Show calendar"

echo "[4] Start editor to write letters"

echo "[5] Exit/Stop"

echo "======================="

echo -n "Enter your menu choice [1-5]: "

read yourch

case $yourch in

1)

echo "Today is $(date) , press a key. . ."

read

;;

2)

echo "Files in $(pwd)"

ls -l

echo "Press a key. . ."

read

;;

3)

cal

echo "Press a key. . ."

read

;;

4) vi ;;

5) exit 0 ;;

*)

echo "Opps!!! Please select choice 1,2,3,4, or 5"

echo "Press a key. . ."

read

;;

esac

done

 

# output

$ bash test.sh

-------------------------------------

Main Menu

-------------------------------------

[1] Show Todays date/time

[2] Show files in current directory

[3] Show calendar

[4] Start editor to write letters

[5] Exit/Stop

=======================

Enter your menu choice [1-5]: 1

Today is 2021년 4월 10일 토요일 03시 58분 48초 KST , press a key. . .

 

---------------------------------------------------------

# 참고 사이트

---------------------------------------------------------

https://linuxhint.com/bash-script-user-input/

'쉘스크립트' 카테고리의 다른 글

[Shell Script] 조건문(case)  (0) 2021.04.15
[Shell Script] 조건문(if-elif-else)  (0) 2021.04.15
[Shell Script] 배열(Array)  (0) 2021.04.15
[Shell Script] 인자(Argument)  (0) 2021.04.15
[Shell Script] 변수(Variable)  (0) 2021.04.15