비환경 변수를 선언한 후 직접 인쇄

비환경 변수를 선언한 후 직접 인쇄

몇 가지를 선언하고 싶습니다.비환경 변수그런 다음 직접 인쇄하십시오.

예를 들어:

read domain &&
web_application_root="${HOME}/www" &&
domain_dir="${web_application_root}/${domain}/public_html" &&

&&마지막 세 변수 선언의 출력을 인쇄 하려면 세 번째 명령 다음에 어떤 명령을 사용해야 합니까 ?

인쇄물의 목적은 쉽게 읽을 수 있도록 세 가지 명령의 출력을 한 곳에 순차적으로, 아마도 테이블과 같은 방식으로 깔끔하게 표시하는 것입니다( set -x추적보다 훨씬 편안합니다).

답변1

이를 수행하는 함수를 정의할 수 있습니다. 여기서는 bash 기능을 사용하고 있습니다. 다른 쉘을 사용하는 경우 이를 조정해야 할 수도 있습니다.

printVariables() {
    local maxLen=0

    # Figure out the length of the longest variable name
    for i; do
        if ((${#i} > maxLen)); then
            maxLen=${#i}
        fi
    done

    # Make room for the colon
    maxLen=$((maxLen + 1))

    # Print the named variables
    for i; do
        printf "%-${maxLen}s %s\n" "${i}:" "${!i}"
    done
}

그 다음에:

$ read domain &&
web_application_root="${HOME}/www" &&
domain_dir="${web_application_root}/${domain}/public_html" &&
printVariables domain web_application_root domain_dir
example.com

다음과 같은 출력이 생성됩니다.

domain:               example.com
web_application_root: /home/user/www
domain_dir:           /home/user/www/example.com/public_html

관련 정보