보안 입력 및 여러 변수 읽기
이 마지막 단계에서는 두 가지 더 고급 기술을 배웁니다.
-
화면에 문자를 표시하지 않고 보안 입력 (예: 비밀번호) 을 읽는 방법
-
단일 read 명령으로 여러 변수를 읽는 방법
-
편집을 위해 스크립트를 엽니다.
nano input_reader.sh
-
기존 내용을 다음 코드로 바꿉니다.
#!/bin/bash
## Script demonstrating secure input and multiple variable reading
## Secure input reading with -s flag (no echo)
echo "Secure Input Example:"
read -p "Username: " username
read -s -p "Password: " password
echo ## Add a newline after password input
echo "Username entered: $username"
echo "Password length: ${#password} characters"
## Reading multiple variables at once
echo -e "\nMultiple Variable Example:"
read -p "Enter first name, last name, and age (separated by spaces): " first_name last_name age
echo "First name: $first_name"
echo "Last name: $last_name"
echo "Age: $age"
## Reading with a custom delimiter
echo -e "\nCustom Delimiter Example:"
read -p "Enter comma-separated values: " -d "," value1
echo ## Add a newline
echo "First value before comma: $value1"
echo -e "\nThank you for completing this lab on Linux input reading!"
이 스크립트는 다음과 같습니다.
read와 함께 -s 플래그를 사용하여 입력을 숨깁니다 (비밀번호 또는 기타 민감한 정보에 유용).
read 명령에 여러 변수 이름을 제공하여 단일 입력 줄에서 여러 변수를 읽는 방법을 보여줍니다.
-d 플래그를 사용하여 사용자 지정 구분 기호 (기본 개행 문자 대신) 를 지정하는 방법을 보여줍니다.
-
Ctrl+O를 눌러 파일을 저장한 다음 Enter를 눌러 파일 이름을 확인하고, Ctrl+X를 눌러 nano 를 종료합니다.
-
스크립트를 실행하여 테스트합니다.
./input_reader.sh
예시 출력 (입력은 다를 것입니다):
Secure Input Example:
Username: john_doe
Password:
Username entered: john_doe
Password length: 8 characters
Multiple Variable Example:
Enter first name, last name, and age (separated by spaces): John Doe 30
First name: John
Last name: Doe
Age: 30
Custom Delimiter Example:
Enter comma-separated values: test,
First value before comma: test
Thank you for completing this lab on Linux input reading!
보안은 비밀번호와 같은 민감한 정보를 처리할 때 중요합니다. -s 플래그는 입력된 문자가 화면에 표시되지 않도록 합니다. 비밀번호 예제에서 스크립트는 확인을 위해 실제 비밀번호가 아닌 비밀번호의 길이만 표시합니다.
여러 변수를 한 번에 읽으면 스크립트를 더 효율적이고 사용자 친화적으로 만들 수 있습니다. read 명령에 여러 변수 이름이 제공되면 입력은 IFS(Internal Field Separator, 내부 필드 구분 기호) 환경 변수를 기반으로 분할되며, 기본값은 공백 (공백, 탭 및 개행) 입니다.
-d 플래그를 사용하면 입력의 끝을 알리는 구분 기호를 변경할 수 있습니다. 기본적으로 read는 개행 문자 (Enter 키를 누를 때) 에서 중지되지만, 예제에서 쉼표와 같이 모든 문자로 변경할 수 있습니다.